diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index b0bc5b1..b6c115c 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) # tests fail if you include Model before HierarchyMaintenance wtf include ClosureTree::HierarchyMaintenance 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