allows joining multiple tables of awesome_nested_set structured models while using nested_set methods

When joining issues and projects both tables come with their set of nested_set attributes. The attributes are named equally in both tables. This leads to ambiguous references when calling e.g. Issue.find(1).visible.descendants.

This fix scopes the nested_set to reference the left attribute fully qualified.

Adds additional methods that can be applied should the need arise.

We should reevaluate why nested set is vendored as the problem would not have arisen when using the gem.
pull/28/head
Jens Ulferts 12 years ago committed by Christian Ratz
parent f1c2656517
commit cad2d044cd
  1. 15
      vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb

@ -93,6 +93,7 @@ module CollectiveIdea #:nodoc:
named_scope :roots, :conditions => {parent_column_name => nil}, :order => quoted_left_column_name
named_scope :leaves, :conditions => "#{quoted_right_column_name} - #{quoted_left_column_name} = 1", :order => quoted_left_column_name
if self.respond_to?(:define_callbacks)
define_callbacks("before_move", "after_move")
end
@ -229,6 +230,18 @@ module CollectiveIdea #:nodoc:
def quoted_scope_column_names
scope_column_names.collect {|column_name| connection.quote_column_name(column_name) }
end
def quoted_left_column_full_name
"#{self.class.table_name}.#{quoted_left_column_name}"
end
def quoted_right_column_full_name
"#{self.class.table_name}.#{quoted_right_column_name}"
end
def quoted_parent_column_full_name
"#{self.class.table_name}.#{quoted_parent_column_name}"
end
end
# Any instance method that returns a collection makes use of Rails 2.1's named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.
@ -426,7 +439,7 @@ module CollectiveIdea #:nodoc:
# the base ActiveRecord class, using the :scope declared in the acts_as_nested_set
# declaration.
def nested_set_scope
options = {:order => quoted_left_column_name}
options = {:order => quoted_left_column_full_name}
scopes = Array(acts_as_nested_set_options[:scope])
options[:conditions] = scopes.inject({}) do |conditions,attr|
conditions.merge attr => self[attr]

Loading…
Cancel
Save