Permalink
Browse files

Fix remaining Rails 4 deprecation warnings

All tests are now passing on Rails 3.0 through 4.0.0.beta1 without
deprecation warnings.
  • Loading branch information...
1 parent f7329f1 commit 66fee13cd155c31d6b04909bb6565f9bdd7a597e @adammck adammck committed Apr 9, 2013
@@ -36,7 +36,7 @@ def arrange options = {}
self.base_class.ordered_by_ancestry_and options.delete(:order)
end
# Get all nodes ordered by ancestry and start sorting them into an empty hash
- arrange_nodes scope.all(options)
+ arrange_nodes scope.where(options)
end
# Arrange array of nodes into a nested hash of the form
@@ -150,7 +150,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.unscoped do
- self.base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
+ self.base_class.where(:parent_id => parent_id).find_each do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
end
@@ -65,7 +65,7 @@ def has_ancestry options = {}
{:before_depth => '<', :to_depth => '<=', :at_depth => '=', :from_depth => '>=', :after_depth => '>'}.each do |scope_name, operator|
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]}
+ where("#{depth_cache_column} #{operator} ?", depth)
}
end
end
@@ -50,7 +50,7 @@ def apply_orphan_strategy
end
# ... make child elements of this node, child of its parent if orphan strategy is adopt
elsif self.base_class.orphan_strategy == :adopt
- descendants.all.each do |descendant|
+ descendants.each do |descendant|
descendant.without_ancestry_callbacks do
new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
View
@@ -14,7 +14,8 @@
class AncestryTestDatabase
def self.setup
- ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('log/test.log')
+ logger = ActiveSupport.const_defined?(:Logger) ? :Logger : :BufferedLogger
+ ActiveRecord::Base.logger = ActiveSupport.const_get(logger).new('log/test.log')
ActiveRecord::Base.establish_connection YAML.load(File.open(File.expand_path('../database.yml', __FILE__)).read)[ENV['db'] || 'sqlite3']
end
@@ -40,7 +41,17 @@ def self.with_model options = {}
const_set model_name, model
model.table_name = 'test_nodes'
- model.send :default_scope, default_scope_params if default_scope_params.present?
+
+ if default_scope_params.present?
+
+ # Rails < 3.1 doesn't support lambda default_scopes (only hashes)
+ # But Rails >= 4 logs deprecation warnings for hash default_scopes
+ if ActiveRecord::VERSION::STRING < "3.1"
+ model.send :default_scope, { :conditions => default_scope_params }
+ else
+ model.send :default_scope, lambda { model.where(default_scope_params) }
+ end
+ end
model.has_ancestry options unless options.delete(:skip_ancestry)
View
@@ -201,24 +201,24 @@ def test_tree_navigation
def test_scopes
AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
# Roots assertion
- assert_equal roots.map(&:first), model.roots.all
+ assert_equal roots.map(&:first), model.roots.to_a
model.all.each do |test_node|
# Assertions for ancestors_of named scope
- assert_equal test_node.ancestors.all, model.ancestors_of(test_node).all
- assert_equal test_node.ancestors.all, model.ancestors_of(test_node.id).all
+ assert_equal test_node.ancestors.to_a, model.ancestors_of(test_node).to_a
+ assert_equal test_node.ancestors.to_a, model.ancestors_of(test_node.id).to_a
# Assertions for children_of named scope
- assert_equal test_node.children.all, model.children_of(test_node).all
- assert_equal test_node.children.all, model.children_of(test_node.id).all
+ assert_equal test_node.children.to_a, model.children_of(test_node).to_a
+ assert_equal test_node.children.to_a, model.children_of(test_node.id).to_a
# Assertions for descendants_of named scope
- assert_equal test_node.descendants.all, model.descendants_of(test_node).all
- assert_equal test_node.descendants.all, model.descendants_of(test_node.id).all
+ assert_equal test_node.descendants.to_a, model.descendants_of(test_node).to_a
+ assert_equal test_node.descendants.to_a, model.descendants_of(test_node.id).to_a
# Assertions for subtree_of named scope
- assert_equal test_node.subtree.all, model.subtree_of(test_node).all
- assert_equal test_node.subtree.all, model.subtree_of(test_node.id).all
+ assert_equal test_node.subtree.to_a, model.subtree_of(test_node).to_a
+ assert_equal test_node.subtree.to_a, model.subtree_of(test_node.id).to_a
# Assertions for siblings_of named scope
- assert_equal test_node.siblings.all, model.siblings_of(test_node).all
- assert_equal test_node.siblings.all, model.siblings_of(test_node.id).all
+ assert_equal test_node.siblings.to_a, model.siblings_of(test_node).to_a
+ assert_equal test_node.siblings.to_a, model.siblings_of(test_node.id).to_a
end
end
end
@@ -267,7 +267,7 @@ def test_orphan_rootify_strategy
AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
model.orphan_strategy = :rootify
root = roots.first.first
- children = root.children.all
+ children = root.children.to_a
root.destroy
children.each do |child|
child.reload
@@ -530,7 +530,7 @@ def test_build_ancestry_from_parent_ids
model.check_ancestry_integrity!
end
- roots = model.roots.all
+ roots = model.roots.to_a
# Assert single root node
assert_equal 1, roots.size
@@ -706,10 +706,11 @@ def test_sort_by_ancestry
def test_node_excluded_by_default_scope_should_still_move_with_parent
AncestryTestDatabase.with_model(
:width => 3, :depth => 3, :extra_columns => {:deleted_at => :datetime},
- :default_scope_params => {:conditions => {:deleted_at => nil}}
+ :default_scope_params => {:deleted_at => nil}
) do |model, roots|
- grandparent = model.roots.all[0]
- new_grandparent = model.roots.all[1]
+ roots = model.roots.to_a
+ grandparent = roots[0]
+ new_grandparent = roots[1]
parent = grandparent.children.first
child = parent.children.first
@@ -724,7 +725,7 @@ def test_node_excluded_by_default_scope_should_still_move_with_parent
def test_node_excluded_by_default_scope_should_be_destroyed_with_parent
AncestryTestDatabase.with_model(
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
- :default_scope_params => {:conditions => {:deleted_at => nil}},
+ :default_scope_params => {:deleted_at => nil},
:orphan_strategy => :destroy
) do |model, roots|
parent = model.roots.first
@@ -741,7 +742,7 @@ def test_node_excluded_by_default_scope_should_be_destroyed_with_parent
def test_node_excluded_by_default_scope_should_be_rootified
AncestryTestDatabase.with_model(
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
- :default_scope_params => {:conditions => {:deleted_at => nil}},
+ :default_scope_params => {:deleted_at => nil},
:orphan_strategy => :rootify
) do |model, roots|
parent = model.roots.first
@@ -757,7 +758,14 @@ def test_node_excluded_by_default_scope_should_be_rootified
def test_arrangement_nesting
AncestryTestDatabase.with_model :extra_columns => {:name => :string} do |model|
- model.send :default_scope, model.order('name')
+
+ # Rails < 3.1 doesn't support lambda default_scopes (only hashes)
+ # But Rails >= 4 logs deprecation warnings for hash default_scopes
+ if ActiveRecord::VERSION::STRING < "3.1"
+ model.send :default_scope, model.order('name')
+ else
+ model.send :default_scope, lambda { model.order('name') }
+ end
model.create!(:name => 'Linux').children.create! :name => 'Debian'

0 comments on commit 66fee13

Please sign in to comment.