aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/test/cases/associations/eager_test.rb33
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb8
-rw-r--r--activerecord/test/cases/base_test.rb78
-rw-r--r--activerecord/test/cases/calculations_test.rb6
-rw-r--r--activerecord/test/cases/finder_test.rb14
-rw-r--r--activerecord/test/cases/locking_test.rb11
-rw-r--r--activerecord/test/cases/method_scoping_test.rb558
-rw-r--r--activerecord/test/cases/readonly_test.rb8
-rw-r--r--activerecord/test/cases/relation_scoping_test.rb22
-rw-r--r--activerecord/test/cases/relations_test.rb1
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb4
-rw-r--r--activerecord/test/cases/validations_test.rb24
-rw-r--r--activerecord/test/models/developer.rb6
13 files changed, 6 insertions, 767 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 196de34094..d700554af7 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -667,39 +667,6 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
- def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
- Post.send(:with_scope, :find => { :conditions => "1=1" }) do
- posts =
- authors(:david).posts
- .includes(:comments)
- .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
- .references(:comments)
- .limit(2)
- .to_a
- assert_equal 2, posts.size
-
- count = Post.includes(:comments, :author)
- .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
- .references(:authors, :comments)
- .limit(2)
- .count
- assert_equal count, posts.size
- end
- end
-
- def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
- posts_with_explicit_order =
- Post.where('comments.id is not null').references(:comments)
- .includes(:comments).order('posts.id DESC').limit(2)
-
- posts_with_scoped_order = Post.order('posts.id DESC').scoping do
- Post.where('comments.id is not null').references(:comments)
- .includes(:comments).limit(2)
- end
-
- assert_equal posts_with_explicit_order, posts_with_scoped_order
- end
-
def test_eager_association_loading_with_habtm
posts = Post.find(:all, :include => :categories, :order => "posts.id")
assert_equal 2, posts[0].categories.size
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index dde5b26a88..d5d1d3405d 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -327,14 +327,6 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
assert_equal [authors(:david), authors(:bob)], posts(:thinking).authors_using_custom_pk.order('authors.id')
end
- def test_both_scoped_and_explicit_joins_should_be_respected
- assert_nothing_raised do
- Post.send(:with_scope, :find => {:joins => "left outer join comments on comments.id = posts.id"}) do
- Post.find :all, :select => "comments.id, authors.id", :joins => "left outer join authors on authors.id = posts.author_id"
- end
- end
- end
-
def test_belongs_to_polymorphic_with_counter_cache
assert_equal 1, posts(:welcome)[:taggings_count]
tagging = posts(:welcome).taggings.create(:tag => tags(:general))
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 024225b813..eadc7e1701 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1599,83 +1599,12 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal res6, res7
end
- def test_scoped_find_conditions
- scoped_developers = Developer.send(:with_scope, :find => { :conditions => 'salary > 90000' }) do
- Developer.find(:all, :conditions => 'id < 5')
- end
- assert !scoped_developers.include?(developers(:david)) # David's salary is less than 90,000
- assert_equal 3, scoped_developers.size
- end
-
def test_no_limit_offset
assert_nothing_raised do
Developer.find(:all, :offset => 2)
end
end
- def test_scoped_find_limit_offset
- scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :offset => 2 }) do
- Developer.find(:all, :order => 'id')
- end
- assert !scoped_developers.include?(developers(:david))
- assert !scoped_developers.include?(developers(:jamis))
- assert_equal 3, scoped_developers.size
-
- # Test without scoped find conditions to ensure we get the whole thing
- developers = Developer.find(:all, :order => 'id')
- assert_equal Developer.count, developers.size
- end
-
- def test_scoped_find_order
- # Test order in scope
- scoped_developers = Developer.send(:with_scope, :find => { :limit => 1, :order => 'salary DESC' }) do
- Developer.find(:all)
- end
- assert_equal 'Jamis', scoped_developers.first.name
- assert scoped_developers.include?(developers(:jamis))
- # Test scope without order and order in find
- scoped_developers = Developer.send(:with_scope, :find => { :limit => 1 }) do
- Developer.find(:all, :order => 'salary DESC')
- end
- # Test scope order + find order, order has priority
- scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :order => 'id DESC' }) do
- Developer.find(:all, :order => 'salary ASC')
- end
- assert scoped_developers.include?(developers(:poor_jamis))
- assert ! scoped_developers.include?(developers(:david))
- assert ! scoped_developers.include?(developers(:jamis))
- assert_equal 3, scoped_developers.size
-
- # Test without scoped find conditions to ensure we get the right thing
- assert ! scoped_developers.include?(Developer.find(1))
- assert scoped_developers.include?(Developer.find(11))
- end
-
- def test_scoped_find_limit_offset_including_has_many_association
- topics = Topic.send(:with_scope, :find => {:limit => 1, :offset => 1, :include => :replies}) do
- Topic.find(:all, :order => "topics.id")
- end
- assert_equal 1, topics.size
- assert_equal 2, topics.first.id
- end
-
- def test_scoped_find_order_including_has_many_association
- developers = Developer.send(:with_scope, :find => { :order => 'developers.salary DESC', :include => :projects }) do
- Developer.find(:all)
- end
- assert developers.size >= 2
- (1...developers.size).each do |i|
- assert developers[i-1].salary >= developers[i].salary
- end
- end
-
- def test_scoped_find_with_group_and_having
- developers = Developer.send(:with_scope, :find => { :group => 'developers.salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
- Developer.find(:all)
- end
- assert_equal 3, developers.size
- end
-
def test_find_last
last = Developer.find :last
assert_equal last, Developer.find(:first, :order => 'id desc')
@@ -1725,13 +1654,6 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal last, Developer.find(:all, :order => :salary).last
end
- def test_find_scoped_ordered_last
- last_developer = Developer.send(:with_scope, :find => { :order => 'developers.salary ASC' }) do
- Developer.find(:last)
- end
- assert_equal last_developer, Developer.find(:all, :order => 'developers.salary ASC').last
- end
-
def test_abstract_class
assert !ActiveRecord::Base.abstract_class?
assert LoosePerson.abstract_class?
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 48cb05e36e..af1e689eff 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -52,12 +52,6 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
end
- def test_should_get_maximum_of_field_with_scoped_include
- Account.send :with_scope, :find => { :include => :firm } do
- assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).maximum(:credit_limit)
- end
- end
-
def test_should_get_minimum_of_field
assert_equal 50, Account.minimum(:credit_limit)
end
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 70f3c4bcc5..36ec459ca1 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -82,12 +82,6 @@ class FinderTest < ActiveRecord::TestCase
Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
end
- def test_exists_with_scoped_include
- Developer.send(:with_scope, :find => { :include => :projects, :order => "projects.name" }) do
- assert Developer.exists?
- end
- end
-
def test_exists_does_not_instantiate_records
Developer.expects(:instantiate).never
Developer.exists?
@@ -1190,14 +1184,6 @@ class FinderTest < ActiveRecord::TestCase
assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
- def test_finder_with_scoped_from
- all_topics = Topic.find(:all)
-
- Topic.send(:with_scope, :find => { :from => 'fake_topics' }) do
- assert_equal all_topics, Topic.from('topics').to_a
- end
- end
-
def test_find_one_message_with_custom_primary_key
Toy.primary_key = :name
begin
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb
index cc6baa6153..48ce7ea2d3 100644
--- a/activerecord/test/cases/locking_test.rb
+++ b/activerecord/test/cases/locking_test.rb
@@ -363,17 +363,6 @@ unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) || in_memory_db?
end
end
- # Test scoped lock.
- def test_sane_find_with_scoped_lock
- assert_nothing_raised do
- Person.transaction do
- Person.send(:with_scope, :find => { :lock => true }) do
- Person.find 1
- end
- end
- end
- end
-
# PostgreSQL protests SELECT ... FOR UPDATE on an outer join.
unless current_adapter?(:PostgreSQLAdapter)
# Test locked eager find.
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
deleted file mode 100644
index ebf6e26385..0000000000
--- a/activerecord/test/cases/method_scoping_test.rb
+++ /dev/null
@@ -1,558 +0,0 @@
-# This file can be removed once with_exclusive_scope and with_scope are removed.
-# All the tests were already ported to relation_scoping_test.rb when the new
-# relation scoping API was added.
-
-require "cases/helper"
-require 'models/post'
-require 'models/author'
-require 'models/developer'
-require 'models/project'
-require 'models/comment'
-
-class MethodScopingTest < ActiveRecord::TestCase
- fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
-
- def test_set_conditions
- Developer.send(:with_scope, :find => { :conditions => 'just a test...' }) do
- assert_match '(just a test...)', Developer.scoped.to_sql
- end
- end
-
- def test_scoped_find
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- assert_nothing_raised { Developer.find(1) }
- end
- end
-
- def test_scoped_find_first
- Developer.send(:with_scope, :find => { :conditions => "salary = 100000" }) do
- assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
- end
- end
-
- def test_scoped_find_last
- highest_salary = Developer.find(:first, :order => "salary DESC")
-
- Developer.send(:with_scope, :find => { :order => "salary" }) do
- assert_equal highest_salary, Developer.last
- end
- end
-
- def test_scoped_find_last_preserves_scope
- lowest_salary = Developer.find(:first, :order => "salary ASC")
- highest_salary = Developer.find(:first, :order => "salary DESC")
-
- Developer.send(:with_scope, :find => { :order => "salary" }) do
- assert_equal highest_salary, Developer.last
- assert_equal lowest_salary, Developer.first
- end
- end
-
- def test_scoped_find_combines_conditions
- Developer.send(:with_scope, :find => { :conditions => "salary = 9000" }) do
- assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")
- end
- end
-
- def test_scoped_find_sanitizes_conditions
- Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
- assert_equal developers(:poor_jamis), Developer.find(:first)
- end
- end
-
- def test_scoped_find_combines_and_sanitizes_conditions
- Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
- assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
- end
- end
-
- def test_scoped_find_all
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- assert_equal [developers(:david)], Developer.all
- end
- end
-
- def test_scoped_find_select
- Developer.send(:with_scope, :find => { :select => "id, name" }) do
- developer = Developer.find(:first, :conditions => "name = 'David'")
- assert_equal "David", developer.name
- assert !developer.has_attribute?(:salary)
- end
- end
-
- def test_scope_select_concatenates
- Developer.send(:with_scope, :find => { :select => "name" }) do
- developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
- assert_equal 80000, developer.salary
- assert developer.has_attribute?(:id)
- assert developer.has_attribute?(:name)
- assert developer.has_attribute?(:salary)
- end
- end
-
- def test_scoped_count
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- assert_equal 1, Developer.count
- end
-
- Developer.send(:with_scope, :find => { :conditions => 'salary = 100000' }) do
- assert_equal 8, Developer.count
- assert_equal 1, Developer.count(:conditions => "name LIKE 'fixture_1%'")
- end
- end
-
- def test_scoped_find_include
- # with the include, will retrieve only developers for the given project
- scoped_developers = Developer.send(:with_scope, :find => { :include => :projects }) do
- Developer.find(:all, :conditions => { 'projects.id' => 2 })
- end
- assert scoped_developers.include?(developers(:david))
- assert !scoped_developers.include?(developers(:jamis))
- assert_equal 1, scoped_developers.size
- end
-
- def test_scoped_find_joins
- scoped_developers = Developer.send(:with_scope, :find => { :joins => 'JOIN developers_projects ON id = developer_id' } ) do
- Developer.find(:all, :conditions => 'developers_projects.project_id = 2')
- end
- assert scoped_developers.include?(developers(:david))
- assert !scoped_developers.include?(developers(:jamis))
- assert_equal 1, scoped_developers.size
- assert_equal developers(:david).attributes, scoped_developers.first.attributes
- end
-
- def test_scoped_find_using_new_style_joins
- scoped_developers = Developer.send(:with_scope, :find => { :joins => :projects }) do
- Developer.find(:all, :conditions => 'projects.id = 2')
- end
- assert scoped_developers.include?(developers(:david))
- assert !scoped_developers.include?(developers(:jamis))
- assert_equal 1, scoped_developers.size
- assert_equal developers(:david).attributes, scoped_developers.first.attributes
- end
-
- def test_scoped_find_merges_old_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id ' }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_merges_new_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => :comments, :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_merges_new_and_old_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_merges_string_array_style_and_string_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => ["INNER JOIN posts ON posts.author_id = authors.id"]}) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_merges_string_array_style_and_hash_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => :posts}) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN comments ON posts.id = comments.post_id'], :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_merges_joins_and_eliminates_duplicate_string_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON posts.author_id = authors.id'}) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => ["INNER JOIN posts ON posts.author_id = authors.id", "INNER JOIN comments ON posts.id = comments.post_id"], :conditions => 'comments.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_find_strips_spaces_from_string_joins_and_eliminates_duplicate_string_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => ' INNER JOIN posts ON posts.author_id = authors.id '}) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN posts ON posts.author_id = authors.id'], :conditions => 'posts.id = 1')
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_scoped_count_include
- # with the include, will retrieve only developers for the given project
- Developer.send(:with_scope, :find => { :include => :projects }) do
- assert_equal 1, Developer.count(:conditions => { 'projects.id' => 2 })
- end
- end
-
- def test_scope_for_create_only_uses_equal
- table = VerySpecialComment.arel_table
- relation = VerySpecialComment.scoped
- relation.where_values << table[:id].not_eq(1)
- assert_equal({:type => "VerySpecialComment"}, relation.send(:scope_for_create))
- end
-
- def test_scoped_create
- new_comment = nil
-
- VerySpecialComment.send(:with_scope, :create => { :post_id => 1 }) do
- assert_equal({:post_id => 1, :type => 'VerySpecialComment' }, VerySpecialComment.scoped.send(:scope_for_create))
- new_comment = VerySpecialComment.create :body => "Wonderful world"
- end
-
- assert Post.find(1).comments.include?(new_comment)
- end
-
- def test_scoped_create_with_join_and_merge
- Comment.where(:body => "but Who's Buying?").joins(:post).merge(Post.where(:body => 'Peace Sells...')).with_scope do
- assert_equal({:body => "but Who's Buying?"}, Comment.scoped.scope_for_create)
- end
- end
-
- def test_immutable_scope
- options = { :conditions => "name = 'David'" }
- Developer.send(:with_scope, :find => options) do
- assert_equal %w(David), Developer.all.map(&:name)
- options[:conditions] = "name != 'David'"
- assert_equal %w(David), Developer.all.map(&:name)
- end
-
- scope = { :find => { :conditions => "name = 'David'" }}
- Developer.send(:with_scope, scope) do
- assert_equal %w(David), Developer.all.map(&:name)
- scope[:find][:conditions] = "name != 'David'"
- assert_equal %w(David), Developer.all.map(&:name)
- end
- end
-
- def test_scoped_with_duck_typing
- scoping = Struct.new(:current_scope).new(:find => { :conditions => ["name = ?", 'David'] })
- Developer.send(:with_scope, scoping) do
- assert_equal %w(David), Developer.all.map(&:name)
- end
- end
-
- def test_ensure_that_method_scoping_is_correctly_restored
- begin
- Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
- raise "an exception"
- end
- rescue
- end
-
- assert !Developer.scoped.where_values.include?("name = 'Jamis'")
- end
-end
-
-class NestedScopingTest < ActiveRecord::TestCase
- fixtures :authors, :developers, :projects, :comments, :posts
-
- def test_merge_options
- Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
- Developer.send(:with_scope, :find => { :limit => 10 }) do
- devs = Developer.scoped
- assert_match '(salary = 80000)', devs.to_sql
- assert_equal 10, devs.taken
- end
- end
- end
-
- def test_merge_inner_scope_has_priority
- Developer.send(:with_scope, :find => { :limit => 5 }) do
- Developer.send(:with_scope, :find => { :limit => 10 }) do
- assert_equal 10, Developer.scoped.taken
- end
- end
- end
-
- def test_replace_options
- Developer.send(:with_scope, :find => { :conditions => {:name => 'David'} }) do
- Developer.send(:with_exclusive_scope, :find => { :conditions => {:name => 'Jamis'} }) do
- assert_equal 'Jamis', Developer.scoped.send(:scope_for_create)[:name]
- end
-
- assert_equal 'David', Developer.scoped.send(:scope_for_create)[:name]
- end
- end
-
- def test_with_exclusive_scope_with_relation
- assert_raise(ArgumentError) do
- Developer.all_johns
- end
- end
-
- def test_append_conditions
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
- devs = Developer.scoped
- assert_match "(name = 'David') AND (salary = 80000)", devs.to_sql
- assert_equal(1, Developer.count)
- end
- Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
- assert_equal(0, Developer.count)
- end
- end
- end
-
- def test_merge_and_append_options
- Developer.send(:with_scope, :find => { :conditions => 'salary = 80000', :limit => 10 }) do
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- devs = Developer.scoped
- assert_match "(salary = 80000) AND (name = 'David')", devs.to_sql
- assert_equal 10, devs.taken
- end
- end
- end
-
- def test_nested_scoped_find
- Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
- Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
- assert_nothing_raised { Developer.find(1) }
- assert_equal('David', Developer.find(:first).name)
- end
- assert_equal('Jamis', Developer.find(:first).name)
- end
- end
-
- def test_nested_scoped_find_include
- Developer.send(:with_scope, :find => { :include => :projects }) do
- Developer.send(:with_scope, :find => { :conditions => { 'projects.id' => 2 } }) do
- assert_nothing_raised { Developer.find(1) }
- assert_equal('David', Developer.find(:first).name)
- end
- end
- end
-
- def test_nested_scoped_find_merged_include
- # :include's remain unique and don't "double up" when merging
- Developer.send(:with_scope, :find => { :include => :projects, :conditions => { 'projects.id' => 2 } }) do
- Developer.send(:with_scope, :find => { :include => :projects }) do
- assert_equal 1, Developer.scoped.includes_values.uniq.length
- assert_equal 'David', Developer.find(:first).name
- end
- end
-
- # the nested scope doesn't remove the first :include
- Developer.send(:with_scope, :find => { :include => :projects, :conditions => { 'projects.id' => 2 } }) do
- Developer.send(:with_scope, :find => { :include => [] }) do
- assert_equal 1, Developer.scoped.includes_values.uniq.length
- assert_equal('David', Developer.find(:first).name)
- end
- end
-
- # mixing array and symbol include's will merge correctly
- Developer.send(:with_scope, :find => { :include => [:projects], :conditions => { 'projects.id' => 2 } }) do
- Developer.send(:with_scope, :find => { :include => :projects }) do
- assert_equal 1, Developer.scoped.includes_values.uniq.length
- assert_equal('David', Developer.find(:first).name)
- end
- end
- end
-
- def test_nested_scoped_find_replace_include
- Developer.send(:with_scope, :find => { :include => :projects }) do
- Developer.send(:with_exclusive_scope, :find => { :include => [] }) do
- assert_equal 0, Developer.scoped.includes_values.length
- end
- end
- end
-
- def test_three_level_nested_exclusive_scoped_find
- Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
- assert_equal('Jamis', Developer.find(:first).name)
-
- Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
- assert_equal('David', Developer.find(:first).name)
-
- Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'Maiha'" }) do
- assert_equal(nil, Developer.find(:first))
- end
-
- # ensure that scoping is restored
- assert_equal('David', Developer.find(:first).name)
- end
-
- # ensure that scoping is restored
- assert_equal('Jamis', Developer.find(:first).name)
- end
- end
-
- def test_merged_scoped_find
- poor_jamis = developers(:poor_jamis)
- Developer.send(:with_scope, :find => { :conditions => "salary < 100000" }) do
- Developer.send(:with_scope, :find => { :offset => 1, :order => 'id asc' }) do
- # Oracle adapter does not generated space after asc therefore trailing space removed from regex
- assert_sql(/ORDER BY\s+id asc/) do
- assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
- end
- end
- end
- end
-
- def test_merged_scoped_find_sanitizes_conditions
- Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
- Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
- assert_raise(ActiveRecord::RecordNotFound) { developers(:poor_jamis) }
- end
- end
- end
-
- def test_nested_scoped_find_combines_and_sanitizes_conditions
- Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
- Developer.send(:with_exclusive_scope, :find => { :conditions => ['salary = ?', 9000] }) do
- assert_equal developers(:poor_jamis), Developer.find(:first)
- assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
- end
- end
- end
-
- def test_merged_scoped_find_combines_and_sanitizes_conditions
- Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
- Developer.send(:with_scope, :find => { :conditions => ['salary > ?', 9000] }) do
- assert_equal %w(David), Developer.all.map(&:name)
- end
- end
- end
-
- def test_nested_scoped_create
- comment = nil
- Comment.send(:with_scope, :create => { :post_id => 1}) do
- Comment.send(:with_scope, :create => { :post_id => 2}) do
- assert_equal({:post_id => 2}, Comment.scoped.send(:scope_for_create))
- comment = Comment.create :body => "Hey guys, nested scopes are broken. Please fix!"
- end
- end
- assert_equal 2, comment.post_id
- end
-
- def test_nested_exclusive_scope_for_create
- comment = nil
-
- Comment.send(:with_scope, :create => { :body => "Hey guys, nested scopes are broken. Please fix!" }) do
- Comment.send(:with_exclusive_scope, :create => { :post_id => 1 }) do
- assert_equal({:post_id => 1}, Comment.scoped.send(:scope_for_create))
- assert_blank Comment.new.body
- comment = Comment.create :body => "Hey guys"
- end
- end
- assert_equal 1, comment.post_id
- assert_equal 'Hey guys', comment.body
- end
-
- def test_merged_scoped_find_on_blank_conditions
- [nil, " ", [], {}].each do |blank|
- Developer.send(:with_scope, :find => {:conditions => blank}) do
- Developer.send(:with_scope, :find => {:conditions => blank}) do
- assert_nothing_raised { Developer.find(:first) }
- end
- end
- end
- end
-
- def test_merged_scoped_find_on_blank_bind_conditions
- [ [""], ["",{}] ].each do |blank|
- Developer.send(:with_scope, :find => {:conditions => blank}) do
- Developer.send(:with_scope, :find => {:conditions => blank}) do
- assert_nothing_raised { Developer.find(:first) }
- end
- end
- end
- end
-
- def test_immutable_nested_scope
- options1 = { :conditions => "name = 'Jamis'" }
- options2 = { :conditions => "name = 'David'" }
- Developer.send(:with_scope, :find => options1) do
- Developer.send(:with_exclusive_scope, :find => options2) do
- assert_equal %w(David), Developer.all.map(&:name)
- options1[:conditions] = options2[:conditions] = nil
- assert_equal %w(David), Developer.all.map(&:name)
- end
- end
- end
-
- def test_immutable_merged_scope
- options1 = { :conditions => "name = 'Jamis'" }
- options2 = { :conditions => "salary > 10000" }
- Developer.send(:with_scope, :find => options1) do
- Developer.send(:with_scope, :find => options2) do
- assert_equal %w(Jamis), Developer.all.map(&:name)
- options1[:conditions] = options2[:conditions] = nil
- assert_equal %w(Jamis), Developer.all.map(&:name)
- end
- end
- end
-
- def test_ensure_that_method_scoping_is_correctly_restored
- Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
- begin
- Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
- raise "an exception"
- end
- rescue
- end
-
- assert Developer.scoped.where_values.include?("name = 'David'")
- assert !Developer.scoped.where_values.include?("name = 'Maiha'")
- end
- end
-
- def test_nested_scoped_find_merges_old_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id' }) do
- Author.send(:with_scope, :find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
- end
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_nested_scoped_find_merges_new_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
- Author.send(:with_scope, :find => { :joins => :comments }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
- end
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-
- def test_nested_scoped_find_merges_new_and_old_style_joins
- scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
- Author.send(:with_scope, :find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
- Author.find(:all, :select => 'DISTINCT authors.*', :joins => '', :conditions => 'comments.id = 1')
- end
- end
- assert scoped_authors.include?(authors(:david))
- assert !scoped_authors.include?(authors(:mary))
- assert_equal 1, scoped_authors.size
- assert_equal authors(:david).attributes, scoped_authors.first.attributes
- end
-end
diff --git a/activerecord/test/cases/readonly_test.rb b/activerecord/test/cases/readonly_test.rb
index e21109baae..b72b72ecf2 100644
--- a/activerecord/test/cases/readonly_test.rb
+++ b/activerecord/test/cases/readonly_test.rb
@@ -70,13 +70,13 @@ class ReadOnlyTest < ActiveRecord::TestCase
end
def test_readonly_scoping
- Post.send(:with_scope, :find => { :conditions => '1=1' }) do
+ Post.where('1=1').scoped do
assert !Post.find(1).readonly?
assert Post.readonly(true).find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
end
- Post.send(:with_scope, :find => { :joins => ' ' }) do
+ Post.joins(' ').scoped do
assert !Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
@@ -85,14 +85,14 @@ class ReadOnlyTest < ActiveRecord::TestCase
# Oracle barfs on this because the join includes unqualified and
# conflicting column names
unless current_adapter?(:OracleAdapter)
- Post.send(:with_scope, :find => { :joins => ', developers' }) do
+ Post.joins(', developers').scoped do
assert Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
end
end
- Post.send(:with_scope, :find => { :readonly => true }) do
+ Post.readonly(true).scoped do
assert Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb
index edf38cb7a3..7216282892 100644
--- a/activerecord/test/cases/relation_scoping_test.rb
+++ b/activerecord/test/cases/relation_scoping_test.rb
@@ -395,20 +395,6 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal 50000, wheres[:salary]
end
- def test_method_scope
- expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
- received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
- assert_equal expected, received
- end
-
- def test_nested_scope
- expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
- received = DeveloperOrderedBySalary.send(:with_scope, :find => { :order => 'name DESC'}) do
- DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
- end
- assert_equal expected, received
- end
-
def test_scope_overwrites_default
expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.name }
received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.name }
@@ -427,14 +413,6 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal expected, received
end
- def test_nested_exclusive_scope
- expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
- received = DeveloperOrderedBySalary.send(:with_exclusive_scope, :find => { :limit => 100 }) do
- DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
- end
- assert_equal expected, received
- end
-
def test_order_in_default_scope_should_prevail
expected = Developer.find(:all, :order => 'salary desc').collect { |dev| dev.salary }
received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 5e938968a3..3fe3122fa1 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -330,7 +330,6 @@ class RelationTest < ActiveRecord::TestCase
end
def test_respond_to_class_methods_and_scopes
- assert DeveloperOrderedBySalary.scoped.respond_to?(:all_ordered_by_name)
assert Topic.scoped.respond_to?(:by_lifo)
end
diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb
index ec09479c95..2b1f34a2d0 100644
--- a/activerecord/test/cases/validations/uniqueness_validation_test.rb
+++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -261,10 +261,10 @@ class UniquenessValidationTest < ActiveRecord::TestCase
assert i1.errors[:value].any?, "Should not be empty"
end
- def test_validates_uniqueness_inside_with_scope
+ def test_validates_uniqueness_inside_scoping
Topic.validates_uniqueness_of(:title)
- Topic.send(:with_scope, :find => { :conditions => { :author_name => "David" } }) do
+ Topic.where(:author_name => "David").scoping do
t1 = Topic.new("title" => "I'm unique!", "author_name" => "Mary")
assert t1.save
t2 = Topic.new("title" => "I'm unique!", "author_name" => "David")
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index e575a98170..b11b330374 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -93,30 +93,6 @@ class ValidationsTest < ActiveRecord::TestCase
end
end
- def test_scoped_create_without_attributes
- WrongReply.send(:with_scope, :create => {}) do
- assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
- end
- end
-
- def test_create_with_exceptions_using_scope_for_protected_attributes
- assert_nothing_raised do
- ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
- person = ProtectedPerson.create! :addon => "Addon"
- assert_equal person.first_name, "Mary", "scope should ignore attr_protected"
- end
- end
- end
-
- def test_create_with_exceptions_using_scope_and_empty_attributes
- assert_nothing_raised do
- ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
- person = ProtectedPerson.create!
- assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
- end
- end
- end
-
def test_create_without_validation
reply = WrongReply.new
assert !reply.save
diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb
index 4cc4947e3b..e6a468feca 100644
--- a/activerecord/test/models/developer.rb
+++ b/activerecord/test/models/developer.rb
@@ -102,12 +102,6 @@ class DeveloperOrderedBySalary < ActiveRecord::Base
default_scope { order('salary DESC') }
scope :by_name, -> { order('name DESC') }
-
- def self.all_ordered_by_name
- with_scope(:find => { :order => 'name DESC' }) do
- find(:all)
- end
- end
end
class DeveloperCalledDavid < ActiveRecord::Base