lib/nested_set/base.rb in nested_set-1.6.8 vs lib/nested_set/base.rb in nested_set-1.7.0
- old
+ new
@@ -47,11 +47,11 @@
:primary_key_column => self.primary_key,
:parent_column => 'parent_id',
:left_column => 'lft',
:right_column => 'rgt',
:depth_column => 'depth',
- :dependent => :delete_all, # or :destroy
+ :dependent => :delete_all
}.merge(options)
if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/
options[:scope] = "#{options[:scope]}_id".intern
end
@@ -171,11 +171,11 @@
"#{quoted_table_name}.#{quoted_right_column_name} >= parent.#{quoted_right_column_name}))"
).exists? == false
end
def no_duplicates_for_columns?
- scope_string = Array(acts_as_nested_set_options[:scope]).map do |c|
+ scope_string = scope_column_names.map do |c|
connection.quote_column_name(c)
end.push(nil).join(", ")
[quoted_left_column_name, quoted_right_column_name].all? do |column|
# No duplicates
unscoped.first(
@@ -343,10 +343,14 @@
end
def quoted_depth_column_name
connection.quote_column_name(depth_column_name)
end
+
+ def quoted_primary_key_column_name
+ connection.quote_column_name(primary_key_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.
#
# category.self_and_descendants.count
@@ -399,47 +403,47 @@
self_and_ancestors.first
end
# Returns the array of all children and self
def self_and_children
- nested_set_scope.scoped.where("#{q_parent} = ? or id = ?", id, id)
+ nested_set_scope.where("#{q_parent} = :id or #{q_primary_key} = :id", :id => self)
end
# Returns the array of all parents and self
def self_and_ancestors
- nested_set_scope.scoped.where("#{q_left} <= ? AND #{q_right} >= ?", left, right)
+ nested_set_scope.where("#{q_left} <= ? AND #{q_right} >= ?", left, right)
end
# Returns an array of all parents
def ancestors
without_self self_and_ancestors
end
# Returns the array of all children of the parent, including self
def self_and_siblings
- nested_set_scope.scoped.where(parent_column_name => parent_id)
+ nested_set_scope.where(parent_column_name => parent_id)
end
# Returns the array of all children of the parent, except self
def siblings
without_self self_and_siblings
end
# Returns a set of all of its nested children which do not have children
def leaves
- descendants.scoped.where("#{q_right} - #{q_left} = 1")
+ descendants.where("#{q_right} - #{q_left} = 1")
end
# Returns the level of this object in the tree
# root level is 0
def level
parent_id.nil? ? 0 : ancestors.count
end
# Returns a set of itself and all of its nested children
def self_and_descendants
- nested_set_scope.scoped.where("#{q_left} >= ? AND #{q_right} <= ?", left, right)
+ nested_set_scope.where("#{q_left} >= ? AND #{q_right} <= ?", left, right)
end
# Returns a set of all of its children and nested children
def descendants
without_self self_and_descendants
@@ -461,18 +465,18 @@
self.left <= other.left && other.left < self.right && same_scope?(other)
end
# Check if other model is in the same scope
def same_scope?(other)
- Array(acts_as_nested_set_options[:scope]).all? do |attr|
+ scope_column_names.all? do |attr|
self.send(attr) == other.send(attr)
end
end
# Find the first sibling to the left
def left_sibling
- siblings.where("#{q_left} < ?", left).reverse_order.first
+ siblings.where("#{q_left} < ?", left).last
end
# Find the first sibling to the right
def right_sibling
siblings.where("#{q_left} > ?", left).first
@@ -539,19 +543,23 @@
def q_parent
"#{self.class.quoted_table_name}.#{quoted_parent_column_name}"
end
+ def q_primary_key
+ "#{self.class.quoted_table_name}.#{quoted_primary_key_column_name}"
+ end
+
def without_self(scope)
- scope.where("#{self.class.quoted_table_name}.#{self.class.primary_key} != ?", self)
+ scope.where("#{q_primary_key} != ?", self)
end
# All nested set queries should use this nested_set_scope, which performs finds on
# the base ActiveRecord class, using the :scope declared in the acts_as_nested_set
# declaration.
def nested_set_scope
- conditions = Array(acts_as_nested_set_options[:scope]).inject({}) do |cnd, attr|
+ conditions = scope_column_names.inject({}) do |cnd, attr|
cnd.merge attr => self[attr]
end
self.class.base_class.order(q_left).where(conditions)
end
@@ -691,10 +699,10 @@
{:a => a, :b => b, :c => c, :d => d, :id => self.id, :new_parent => new_parent}
])
end
target.reload_nested_set if target
self.reload_nested_set
- self.update_depth if depth?
+ self.update_depth if has_depth_column?
end
end
end
end # Base
end # NestedSet