Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed backwards compatibility workarounds

  • Loading branch information...
commit 6d962285d5092a0c0b51a9f387fecbcd3d99d354 1 parent 8850dca
Stefan Henzen authored
View
8 lib/ancestry/class_methods.rb
@@ -74,7 +74,7 @@ def check_ancestry_integrity! options = {}
parents = {}
exceptions = [] if options[:report] == :list
- self.base_class.send(:with_exclusive_scope) do
+ self.base_class.unscoped do
# For each node ...
self.base_class.find_each do |node|
begin
@@ -112,7 +112,7 @@ def restore_ancestry_integrity!
parents = {}
# Wrap the whole thing in a transaction ...
self.base_class.transaction do
- self.base_class.send(:with_exclusive_scope) do
+ self.base_class.unscoped do
# For each node ...
self.base_class.find_each do |node|
# ... set its ancestry to nil if invalid
@@ -149,7 +149,7 @@ def restore_ancestry_integrity!
# Build ancestry from parent id's for migration purposes
def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
- self.base_class.send(:with_exclusive_scope) do
+ self.base_class.unscoped do
self.base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
@@ -163,7 +163,7 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
def rebuild_depth_cache!
raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
- self.base_class.send(:with_exclusive_scope) do
+ self.base_class.unscoped do
self.base_class.find_each do |node|
node.update_attribute depth_cache_column, node.depth
end
View
25 lib/ancestry/has_ancestry.rb
@@ -32,22 +32,15 @@ def has_ancestry options = {}
# Validate that the ancestor ids don't include own id
validate :ancestry_exclude_self
- # Save ActiveRecord version
- self.cattr_accessor :rails_3
- self.rails_3 = defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
-
- # Workaround to support Rails 2
- scope_method = if rails_3 then :scope else :named_scope end
-
# Named scopes
- send scope_method, :roots, :conditions => {ancestry_column => nil}
- send scope_method, :ancestors_of, lambda { |object| {:conditions => to_node(object).ancestor_conditions} }
- send scope_method, :children_of, lambda { |object| {:conditions => to_node(object).child_conditions} }
- send scope_method, :descendants_of, lambda { |object| {:conditions => to_node(object).descendant_conditions} }
- send scope_method, :subtree_of, lambda { |object| {:conditions => to_node(object).subtree_conditions} }
- send scope_method, :siblings_of, lambda { |object| {:conditions => to_node(object).sibling_conditions} }
- send scope_method, :ordered_by_ancestry, reorder("(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}")
- send scope_method, :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}") }
+ 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 :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
before_save :update_descendants_with_new_ancestry
@@ -70,7 +63,7 @@ def has_ancestry options = {}
# Create named scopes for depth
{:before_depth => '<', :to_depth => '<=', :at_depth => '=', :from_depth => '>=', :after_depth => '>'}.each do |scope_name, operator|
- send scope_method, scope_name, lambda { |depth|
+ scope scope_name, lambda { |depth|
raise Ancestry::AncestryException.new("Named scope '#{scope_name}' is only available when depth caching is enabled.") unless options[:cache_depth]
{:conditions => ["#{depth_cache_column} #{operator} ?", depth]}
}
View
11 lib/ancestry/instance_methods.rb
@@ -2,7 +2,7 @@ module Ancestry
module InstanceMethods
# Validate that the ancestors don't include itself
def ancestry_exclude_self
- add_error_to_base "#{self.class.name.humanize} cannot be a descendant of itself." if ancestor_ids.include? self.id
+ errors.add(:base, "#{self.class.name.humanize} cannot be a descendant of itself.") if ancestor_ids.include? self.id
end
# Update descendants with new ancestry
@@ -208,15 +208,6 @@ def ancestry_callbacks_disabled?
private
- # Workaround to support Rails 2
- def add_error_to_base error
- if rails_3
- errors[:base] << error
- else
- errors.add_to_base error
- end
- end
-
def cast_primary_key(key)
if primary_key_type == :string
key
Please sign in to comment.
Something went wrong with that request. Please try again.