Use lambda for scope in order to be compatible with rails 4 #117

Merged
merged 4 commits into from Apr 3, 2013
View
14 lib/ancestry/has_ancestry.rb
@@ -33,13 +33,13 @@ def has_ancestry options = {}
validate :ancestry_exclude_self
# Named scopes
- scope :roots, :conditions => {ancestry_column => nil}
- scope :ancestors_of, lambda { |object| {:conditions => to_node(object).ancestor_conditions} }
- scope :children_of, lambda { |object| {:conditions => to_node(object).child_conditions} }
- scope :descendants_of, lambda { |object| {:conditions => to_node(object).descendant_conditions} }
- scope :subtree_of, lambda { |object| {:conditions => to_node(object).subtree_conditions} }
- scope :siblings_of, lambda { |object| {:conditions => to_node(object).sibling_conditions} }
- scope :ordered_by_ancestry, reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}")
+ scope :roots, lambda { where(ancestry_column => nil) }
+ scope :ancestors_of, lambda { |object| where(to_node(object).ancestor_conditions) }
+ scope :children_of, lambda { |object| where(to_node(object).child_conditions) }
+ scope :descendants_of, lambda { |object| where(to_node(object).descendant_conditions) }
+ scope :subtree_of, lambda { |object| where(to_node(object).subtree_conditions) }
+ scope :siblings_of, lambda { |object| where(to_node(object).sibling_conditions) }
+ scope :ordered_by_ancestry, lambda { reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}") }
scope :ordered_by_ancestry_and, lambda { |order| reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}, #{order}") }
# Update descendants with new ancestry before save
View
12 lib/ancestry/instance_methods.rb
@@ -82,7 +82,7 @@ def ancestor_conditions
end
def ancestors depth_options = {}
- self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => ancestor_conditions
+ self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where ancestor_conditions
end
def path_ids
@@ -94,7 +94,7 @@ def path_conditions
end
def path depth_options = {}
- self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => path_conditions
+ self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where path_conditions
end
def depth
@@ -141,7 +141,7 @@ def child_conditions
end
def children
- self.base_class.scoped :conditions => child_conditions
+ self.base_class.where child_conditions
end
def child_ids
@@ -162,7 +162,7 @@ def sibling_conditions
end
def siblings
- self.base_class.scoped :conditions => sibling_conditions
+ self.base_class.where sibling_conditions
end
def sibling_ids
@@ -183,7 +183,7 @@ def descendant_conditions
end
def descendants depth_options = {}
- self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => descendant_conditions
+ self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where descendant_conditions
end
def descendant_ids depth_options = {}
@@ -196,7 +196,7 @@ def subtree_conditions
end
def subtree depth_options = {}
- self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => subtree_conditions
+ self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).where subtree_conditions
end
def subtree_ids depth_options = {}