From e8312269a4154aa82606fe240c4c9029e60b92b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Norl=C3=A9n?= Date: Tue, 18 Feb 2020 12:55:04 +0100 Subject: [PATCH 1/5] Fix undefined method arel_table --- app/models/alchemy/attachment.rb | 2 +- app/models/alchemy/content.rb | 2 +- app/models/alchemy/element.rb | 2 +- app/models/alchemy/folded_page.rb | 2 +- app/models/alchemy/node.rb | 2 +- app/models/alchemy/picture.rb | 2 +- lib/alchemy/essence.rb | 2 +- lib/alchemy/userstamp.rb | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/alchemy/attachment.rb b/app/models/alchemy/attachment.rb index 9a51accb20..4fad542b3d 100644 --- a/app/models/alchemy/attachment.rb +++ b/app/models/alchemy/attachment.rb @@ -28,7 +28,7 @@ class Attachment < BaseRecord after_assign { |f| write_attribute(:file_mime_type, f.mime_type) } end - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" has_many :essence_files, class_name: 'Alchemy::EssenceFile', foreign_key: 'attachment_id' has_many :contents, through: :essence_files diff --git a/app/models/alchemy/content.rb b/app/models/alchemy/content.rb index 622fd96d14..5af9170580 100644 --- a/app/models/alchemy/content.rb +++ b/app/models/alchemy/content.rb @@ -28,7 +28,7 @@ class Content < BaseRecord belongs_to :element, touch: true, inverse_of: :contents has_one :page, through: :element - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" acts_as_list diff --git a/app/models/alchemy/element.rb b/app/models/alchemy/element.rb index 07fc56e3e2..9280beb629 100644 --- a/app/models/alchemy/element.rb +++ b/app/models/alchemy/element.rb @@ -58,7 +58,7 @@ class Element < BaseRecord # acts_as_list scope: [:page_id, :fixed, :parent_element_id] - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" # Content positions are scoped by their essence_type, so positions can be the same for different contents. # In order to get contents in creation order we also order them by id. diff --git a/app/models/alchemy/folded_page.rb b/app/models/alchemy/folded_page.rb index 9e7a41d109..2b515419ea 100644 --- a/app/models/alchemy/folded_page.rb +++ b/app/models/alchemy/folded_page.rb @@ -13,7 +13,7 @@ module Alchemy class FoldedPage < BaseRecord belongs_to :page, inverse_of: :folded_pages - belongs_to :user, inverse_of: :folded_pages, class_name: Alchemy.user_class_name + belongs_to :user, inverse_of: :folded_pages, class_name: "::#{Alchemy.user_class_name}" def self.folded_for_user(user) return none unless Alchemy.user_class < ActiveRecord::Base diff --git a/app/models/alchemy/node.rb b/app/models/alchemy/node.rb index bb482dafcb..a7bf210fb9 100644 --- a/app/models/alchemy/node.rb +++ b/app/models/alchemy/node.rb @@ -5,7 +5,7 @@ class Node < BaseRecord VALID_URL_REGEX = /\A(\/|\D[a-z\+\d\.\-]+:)/ acts_as_nested_set scope: 'language_id', touch: true - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" belongs_to :language, class_name: 'Alchemy::Language' belongs_to :page, class_name: 'Alchemy::Page', optional: true, inverse_of: :nodes diff --git a/app/models/alchemy/picture.rb b/app/models/alchemy/picture.rb index c9c51644e9..69f7e10a4c 100644 --- a/app/models/alchemy/picture.rb +++ b/app/models/alchemy/picture.rb @@ -74,7 +74,7 @@ def allowed_filetypes case_sensitive: false, message: Alchemy.t("not a valid image") - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" scope :named, ->(name) { where("#{table_name}.name LIKE ?", "%#{name}%") diff --git a/lib/alchemy/essence.rb b/lib/alchemy/essence.rb index 92e0c6e655..057c89d91a 100644 --- a/lib/alchemy/essence.rb +++ b/lib/alchemy/essence.rb @@ -48,7 +48,7 @@ def acts_as_essence(options = {}) class_eval <<-RUBY, __FILE__, __LINE__ + 1 attr_writer :validation_errors include Alchemy::Essence::InstanceMethods - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" validate :validate_ingredient, on: :update, if: -> { validations.any? } has_one :content, as: :essence, class_name: "Alchemy::Content", inverse_of: :essence diff --git a/lib/alchemy/userstamp.rb b/lib/alchemy/userstamp.rb index f372448261..b8bafc2d45 100644 --- a/lib/alchemy/userstamp.rb +++ b/lib/alchemy/userstamp.rb @@ -7,6 +7,6 @@ if Alchemy.user_class < ActiveRecord::Base Alchemy.user_class.class_eval do model_stamper - stampable stamper_class_name: Alchemy.user_class_name + stampable stamper_class_name: "::#{Alchemy.user_class_name}" end end From 22b917315c121e6f6dc62b04c8d108108d3c6c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Norl=C3=A9n?= Date: Fri, 21 Feb 2020 12:53:59 +0100 Subject: [PATCH 2/5] Mv change to auth_accessors.rb --- app/models/alchemy/attachment.rb | 2 +- app/models/alchemy/content.rb | 2 +- app/models/alchemy/element.rb | 2 +- app/models/alchemy/folded_page.rb | 2 +- app/models/alchemy/node.rb | 2 +- app/models/alchemy/picture.rb | 2 +- lib/alchemy/auth_accessors.rb | 13 +++++++++++-- lib/alchemy/essence.rb | 2 +- lib/alchemy/userstamp.rb | 2 +- 9 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/models/alchemy/attachment.rb b/app/models/alchemy/attachment.rb index 4fad542b3d..9a51accb20 100644 --- a/app/models/alchemy/attachment.rb +++ b/app/models/alchemy/attachment.rb @@ -28,7 +28,7 @@ class Attachment < BaseRecord after_assign { |f| write_attribute(:file_mime_type, f.mime_type) } end - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name has_many :essence_files, class_name: 'Alchemy::EssenceFile', foreign_key: 'attachment_id' has_many :contents, through: :essence_files diff --git a/app/models/alchemy/content.rb b/app/models/alchemy/content.rb index 5af9170580..622fd96d14 100644 --- a/app/models/alchemy/content.rb +++ b/app/models/alchemy/content.rb @@ -28,7 +28,7 @@ class Content < BaseRecord belongs_to :element, touch: true, inverse_of: :contents has_one :page, through: :element - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name acts_as_list diff --git a/app/models/alchemy/element.rb b/app/models/alchemy/element.rb index 9280beb629..07fc56e3e2 100644 --- a/app/models/alchemy/element.rb +++ b/app/models/alchemy/element.rb @@ -58,7 +58,7 @@ class Element < BaseRecord # acts_as_list scope: [:page_id, :fixed, :parent_element_id] - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name # Content positions are scoped by their essence_type, so positions can be the same for different contents. # In order to get contents in creation order we also order them by id. diff --git a/app/models/alchemy/folded_page.rb b/app/models/alchemy/folded_page.rb index 2b515419ea..9e7a41d109 100644 --- a/app/models/alchemy/folded_page.rb +++ b/app/models/alchemy/folded_page.rb @@ -13,7 +13,7 @@ module Alchemy class FoldedPage < BaseRecord belongs_to :page, inverse_of: :folded_pages - belongs_to :user, inverse_of: :folded_pages, class_name: "::#{Alchemy.user_class_name}" + belongs_to :user, inverse_of: :folded_pages, class_name: Alchemy.user_class_name def self.folded_for_user(user) return none unless Alchemy.user_class < ActiveRecord::Base diff --git a/app/models/alchemy/node.rb b/app/models/alchemy/node.rb index a7bf210fb9..bb482dafcb 100644 --- a/app/models/alchemy/node.rb +++ b/app/models/alchemy/node.rb @@ -5,7 +5,7 @@ class Node < BaseRecord VALID_URL_REGEX = /\A(\/|\D[a-z\+\d\.\-]+:)/ acts_as_nested_set scope: 'language_id', touch: true - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name belongs_to :language, class_name: 'Alchemy::Language' belongs_to :page, class_name: 'Alchemy::Page', optional: true, inverse_of: :nodes diff --git a/app/models/alchemy/picture.rb b/app/models/alchemy/picture.rb index 69f7e10a4c..c9c51644e9 100644 --- a/app/models/alchemy/picture.rb +++ b/app/models/alchemy/picture.rb @@ -74,7 +74,7 @@ def allowed_filetypes case_sensitive: false, message: Alchemy.t("not a valid image") - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name scope :named, ->(name) { where("#{table_name}.name LIKE ?", "%#{name}%") diff --git a/lib/alchemy/auth_accessors.rb b/lib/alchemy/auth_accessors.rb index 21d187231f..1ded0b2bb1 100644 --- a/lib/alchemy/auth_accessors.rb +++ b/lib/alchemy/auth_accessors.rb @@ -44,8 +44,7 @@ # Alchemy.register_ability MyCustom::Ability # module Alchemy - mattr_accessor :user_class_name, - :user_class_primary_key, + mattr_accessor :user_class_primary_key, :current_user_method, :signup_path, :login_path, @@ -73,6 +72,16 @@ module Alchemy # # config/initializers/alchemy.rb # Alchemy.user_class_name = 'Admin' # + + # Prefix with :: when getting to avoid constant name conflicts + def self.user_class_name + "::#{@@user_class_name}" + end + + def self.user_class_name=(user_class_name) + @@user_class_name = user_class_name + end + def self.user_class @@user_class ||= begin if @@user_class_name.is_a?(String) diff --git a/lib/alchemy/essence.rb b/lib/alchemy/essence.rb index 057c89d91a..92e0c6e655 100644 --- a/lib/alchemy/essence.rb +++ b/lib/alchemy/essence.rb @@ -48,7 +48,7 @@ def acts_as_essence(options = {}) class_eval <<-RUBY, __FILE__, __LINE__ + 1 attr_writer :validation_errors include Alchemy::Essence::InstanceMethods - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name validate :validate_ingredient, on: :update, if: -> { validations.any? } has_one :content, as: :essence, class_name: "Alchemy::Content", inverse_of: :essence diff --git a/lib/alchemy/userstamp.rb b/lib/alchemy/userstamp.rb index b8bafc2d45..f372448261 100644 --- a/lib/alchemy/userstamp.rb +++ b/lib/alchemy/userstamp.rb @@ -7,6 +7,6 @@ if Alchemy.user_class < ActiveRecord::Base Alchemy.user_class.class_eval do model_stamper - stampable stamper_class_name: "::#{Alchemy.user_class_name}" + stampable stamper_class_name: Alchemy.user_class_name end end From 95c73f60926b53715a16f2794d281511a062618a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Norl=C3=A9n?= Date: Sun, 23 Feb 2020 13:00:31 +0100 Subject: [PATCH 3/5] Add tests --- lib/alchemy/auth_accessors.rb | 19 ++++++++----------- spec/libraries/auth_accessors_spec.rb | 14 +++++++++----- spec/libraries/userstamp_spec.rb | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/alchemy/auth_accessors.rb b/lib/alchemy/auth_accessors.rb index 1ded0b2bb1..6f032224b5 100644 --- a/lib/alchemy/auth_accessors.rb +++ b/lib/alchemy/auth_accessors.rb @@ -75,7 +75,10 @@ module Alchemy # Prefix with :: when getting to avoid constant name conflicts def self.user_class_name - "::#{@@user_class_name}" + if !@@user_class_name.is_a?(String) + raise TypeError, 'Alchemy.user_class_name must be a String, not a Class.' + end + "::#{@@user_class_name}".freeze end def self.user_class_name=(user_class_name) @@ -83,16 +86,10 @@ def self.user_class_name=(user_class_name) end def self.user_class - @@user_class ||= begin - if @@user_class_name.is_a?(String) - @@user_class_name.constantize - else - raise TypeError, 'Alchemy.user_class_name must be a String, not a Class.' - end - end - rescue NameError => e - if e.message =~ /#{Regexp.escape(@@user_class_name)}/ - abort <<-MSG.strip_heredoc + @@user_class ||= @@user_class_name.constantize + rescue NameError => e + if e.message =~ /#{Regexp.escape(@@user_class_name)}/ + abort <<-MSG.strip_heredoc AlchemyCMS cannot find any user class! diff --git a/spec/libraries/auth_accessors_spec.rb b/spec/libraries/auth_accessors_spec.rb index 1382f3d2c4..7913194844 100644 --- a/spec/libraries/auth_accessors_spec.rb +++ b/spec/libraries/auth_accessors_spec.rb @@ -7,21 +7,25 @@ class MyCustomUser end describe 'AuthAccessors' do - describe '.user_class' do + describe '.user_class_name' do before do # prevent memoization - Alchemy.class_variable_set('@@user_class', nil) + Alchemy.user_class_name = 'DummyClassName' end it "raises error if user_class_name is not a String" do - Alchemy.user_class_name = MyCustomUser + Alchemy.user_class_name = DummyUser expect { - Alchemy.user_class + Alchemy.user_class_name }.to raise_error(TypeError) end + it "returns user_class_name with :: prefix" do + expect(Alchemy.user_class_name).to eq("::DummyClassName") + end + after do - Alchemy.user_class_name = 'DummyUser' + Alchemy.user_class_name = 'DummyClassName' end end diff --git a/spec/libraries/userstamp_spec.rb b/spec/libraries/userstamp_spec.rb index 20de1bdf34..73dae06c16 100644 --- a/spec/libraries/userstamp_spec.rb +++ b/spec/libraries/userstamp_spec.rb @@ -6,7 +6,7 @@ describe '.user_class' do it "injects userstamp class methods" do expect(Alchemy.user_class).to respond_to(:stamper_class_name) - expect(Alchemy.user_class.stamper_class_name).to eq(:DummyUser) + expect(Alchemy.user_class.stamper_class_name).to eq(:"::DummyUser") end end end From 0648087a6eee730953f287de9ba0e9c34a6dc42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Norl=C3=A9n?= Date: Sun, 23 Feb 2020 13:05:51 +0100 Subject: [PATCH 4/5] Fix code align, remove freeze --- lib/alchemy/auth_accessors.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/alchemy/auth_accessors.rb b/lib/alchemy/auth_accessors.rb index 6f032224b5..11939213a6 100644 --- a/lib/alchemy/auth_accessors.rb +++ b/lib/alchemy/auth_accessors.rb @@ -78,7 +78,7 @@ def self.user_class_name if !@@user_class_name.is_a?(String) raise TypeError, 'Alchemy.user_class_name must be a String, not a Class.' end - "::#{@@user_class_name}".freeze + "::#{@@user_class_name}" end def self.user_class_name=(user_class_name) @@ -98,10 +98,10 @@ def self.user_class bundle add alchemy-devise - MSG - else - raise e - end + MSG + else + raise e + end end # Register a CanCan Ability class From da80e4dd815f281f7168225afb9128e094bedaf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20Norl=C3=A9n?= Date: Sun, 23 Feb 2020 13:14:48 +0100 Subject: [PATCH 5/5] Fix missing end --- lib/alchemy/auth_accessors.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/alchemy/auth_accessors.rb b/lib/alchemy/auth_accessors.rb index 11939213a6..1de619d8a6 100644 --- a/lib/alchemy/auth_accessors.rb +++ b/lib/alchemy/auth_accessors.rb @@ -86,7 +86,8 @@ def self.user_class_name=(user_class_name) end def self.user_class - @@user_class ||= @@user_class_name.constantize + @@user_class ||= begin + @@user_class_name.constantize rescue NameError => e if e.message =~ /#{Regexp.escape(@@user_class_name)}/ abort <<-MSG.strip_heredoc @@ -102,6 +103,7 @@ def self.user_class else raise e end + end end # Register a CanCan Ability class