From efa39ea93aaeb659ede8c319833c4b905293251d Mon Sep 17 00:00:00 2001 From: Ricky Winowiecki Date: Thu, 3 Feb 2022 13:59:51 -0500 Subject: [PATCH 1/3] Update support.rb to take in a database option --- lib/closure_tree/support.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 04cf06b..9e4760e 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -31,13 +31,14 @@ def initialize(model_class, options) end end - def hierarchy_class_for_model + def hierarchy_class_for_model(options={}) 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(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 + connects_to database: options[:database] if options[:database].present? 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 3628e64dccaefcc9784a3ab4933a93606dc458da Mon Sep 17 00:00:00 2001 From: Ricky Winowiecki Date: Thu, 3 Feb 2022 14:00:57 -0500 Subject: [PATCH 2/3] Allow for database to be an option on has_closure_tree --- lib/closure_tree/has_closure_tree.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index b0bc5b1..c7f5732 100644 --- a/lib/closure_tree/has_closure_tree.rb +++ b/lib/closure_tree/has_closure_tree.rb @@ -11,7 +11,8 @@ def has_closure_tree(options = {}) :dont_order_roots, :numeric_order, :touch, - :with_advisory_lock + :with_advisory_lock, + :database ) class_attribute :_ct @@ -20,7 +21,7 @@ def has_closure_tree(options = {}) # Auto-inject the hierarchy table # See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb class_attribute :hierarchy_class - self.hierarchy_class = _ct.hierarchy_class_for_model + self.hierarchy_class = _ct.hierarchy_class_for_model(options[:database]) # tests fail if you include Model before HierarchyMaintenance wtf include ClosureTree::HierarchyMaintenance From 3b7ac512d9c7c6be0b472848b964d53e17a6c403 Mon Sep 17 00:00:00 2001 From: Ricky Winowiecki Date: Thu, 3 Feb 2022 14:04:48 -0500 Subject: [PATCH 3/3] Update has_closure_tree.rb --- lib/closure_tree/has_closure_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index c7f5732..b6c115c 100644 --- a/lib/closure_tree/has_closure_tree.rb +++ b/lib/closure_tree/has_closure_tree.rb @@ -21,7 +21,7 @@ def has_closure_tree(options = {}) # Auto-inject the hierarchy table # See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb class_attribute :hierarchy_class - self.hierarchy_class = _ct.hierarchy_class_for_model(options[:database]) + self.hierarchy_class = _ct.hierarchy_class_for_model(options) # tests fail if you include Model before HierarchyMaintenance wtf include ClosureTree::HierarchyMaintenance