From 68526549e1a03b8bfe6bed8de16140e6d599ad1b Mon Sep 17 00:00:00 2001 From: xiaoyu Date: Thu, 16 Sep 2021 01:48:50 +0800 Subject: [PATCH 1/3] fix: hierarchy class with namespace should inherit from the Namespace::ApplicationRecord --- lib/closure_tree/support.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 75108d79..381e437b 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -33,7 +33,9 @@ def initialize(model_class, options) def hierarchy_class_for_model parent_class = ActiveSupport::VERSION::MAJOR >= 6 ? model_class.module_parent : model_class.parent - hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(ActiveRecord::Base)) + temp_target_class = ActiveSupport::VERSION::MAJOR >= 5 ? ApplicationRecord : ActiveRecord::Base + target_class = parent_class.const_get(temp_target_class.to_s) + hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(target_class)) use_attr_accessible = use_attr_accessible? include_forbidden_attributes_protection = include_forbidden_attributes_protection? model_class_name = model_class.to_s From af61b79a8ffc9c5b82751cc3247623f7b53242fa Mon Sep 17 00:00:00 2001 From: xiaoyu Date: Thu, 16 Sep 2021 17:32:04 +0800 Subject: [PATCH 2/3] fix: hierarchy class with namespace should connect to the database of parent class --- lib/closure_tree/support.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 381e437b..2a44c059 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -33,13 +33,14 @@ def initialize(model_class, options) def hierarchy_class_for_model parent_class = ActiveSupport::VERSION::MAJOR >= 6 ? model_class.module_parent : model_class.parent - temp_target_class = ActiveSupport::VERSION::MAJOR >= 5 ? ApplicationRecord : ActiveRecord::Base - target_class = parent_class.const_get(temp_target_class.to_s) - hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(target_class)) + temp_database_config = model_class.connection_config + model_class_database_config = temp_database_config.clone + hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(ActiveRecord::Base)) use_attr_accessible = use_attr_accessible? include_forbidden_attributes_protection = include_forbidden_attributes_protection? model_class_name = model_class.to_s hierarchy_class.class_eval do + establish_connection model_class_database_config include ActiveModel::ForbiddenAttributesProtection if include_forbidden_attributes_protection belongs_to :ancestor, class_name: model_class_name belongs_to :descendant, class_name: model_class_name From 6f9546369478524b640fb6709d6cce1c3bbf3d8c Mon Sep 17 00:00:00 2001 From: xiaoyu Date: Fri, 17 Sep 2021 01:45:43 +0800 Subject: [PATCH 3/3] fix: hierarchy class inherit from the superclass of model class --- lib/closure_tree/support.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 2a44c059..04cf06b4 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -33,14 +33,11 @@ def initialize(model_class, options) def hierarchy_class_for_model parent_class = ActiveSupport::VERSION::MAJOR >= 6 ? model_class.module_parent : model_class.parent - temp_database_config = model_class.connection_config - model_class_database_config = temp_database_config.clone - hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(ActiveRecord::Base)) + hierarchy_class = parent_class.const_set(short_hierarchy_class_name, Class.new(model_class.superclass)) use_attr_accessible = use_attr_accessible? include_forbidden_attributes_protection = include_forbidden_attributes_protection? model_class_name = model_class.to_s hierarchy_class.class_eval do - establish_connection model_class_database_config include ActiveModel::ForbiddenAttributesProtection if include_forbidden_attributes_protection belongs_to :ancestor, class_name: model_class_name belongs_to :descendant, class_name: model_class_name