Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Databases #394

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down