2010-06-29 11:18:55 -04:00
|
|
|
# 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.
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2009-04-23 09:26:52 -04:00
|
|
|
require 'models/post'
|
2008-08-28 12:00:18 -04:00
|
|
|
require 'models/author'
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/developer'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/comment'
|
2005-07-22 16:05:42 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class MethodScopingTest < ActiveRecord::TestCase
|
2008-08-28 12:00:18 -04:00
|
|
|
fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_set_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => 'just a test...' }) do
|
2011-08-08 18:27:54 -04:00
|
|
|
assert_match '(just a test...)', Developer.scoped.to_sql
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_find
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_nothing_raised { Developer.find(1) }
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_scoped_find_first
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "salary = 100000" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-12-21 17:38:12 -05:00
|
|
|
def test_scoped_find_last
|
|
|
|
highest_salary = Developer.find(:first, :order => "salary DESC")
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :order => "salary" }) do
|
2008-12-21 17:38:12 -05:00
|
|
|
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")
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :order => "salary" }) do
|
2008-12-21 17:38:12 -05:00
|
|
|
assert_equal highest_salary, Developer.last
|
|
|
|
assert_equal lowest_salary, Developer.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-01-03 22:43:28 -05:00
|
|
|
def test_scoped_find_combines_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "salary = 9000" }) do
|
2006-01-03 22:43:28 -05:00
|
|
|
assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-01-03 22:43:28 -05:00
|
|
|
def test_scoped_find_sanitizes_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
|
2006-01-03 22:43:28 -05:00
|
|
|
assert_equal developers(:poor_jamis), Developer.find(:first)
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2006-01-03 22:43:28 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-01-03 22:43:28 -05:00
|
|
|
def test_scoped_find_combines_and_sanitizes_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
|
2006-01-03 22:43:28 -05:00
|
|
|
assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2006-01-03 22:43:28 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_scoped_find_all
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal [developers(:david)], Developer.all
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-29 07:59:29 -04:00
|
|
|
def test_scoped_find_select
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :select => "id, name" }) do
|
2008-05-29 07:59:29 -04:00
|
|
|
developer = Developer.find(:first, :conditions => "name = 'David'")
|
|
|
|
assert_equal "David", developer.name
|
|
|
|
assert !developer.has_attribute?(:salary)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-24 21:52:15 -04:00
|
|
|
def test_scope_select_concatenates
|
|
|
|
Developer.send(:with_scope, :find => { :select => "name" }) do
|
2008-05-29 07:59:29 -04:00
|
|
|
developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
|
|
|
|
assert_equal 80000, developer.salary
|
2010-06-24 21:52:15 -04:00
|
|
|
assert developer.has_attribute?(:id)
|
|
|
|
assert developer.has_attribute?(:name)
|
|
|
|
assert developer.has_attribute?(:salary)
|
2008-05-29 07:59:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_scoped_count
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal 1, Developer.count
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => 'salary = 100000' }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal 8, Developer.count
|
2006-09-26 13:02:45 -04:00
|
|
|
assert_equal 1, Developer.count(:conditions => "name LIKE 'fixture_1%'")
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_scoped_find_include
|
|
|
|
# with the include, will retrieve only developers for the given project
|
2012-01-13 18:42:07 -05:00
|
|
|
scoped_developers = Developer.send(:with_scope, :find => { :include => :projects }) do
|
2012-01-16 16:14:34 -05:00
|
|
|
Developer.find(:all, :conditions => { 'projects.id' => 2 })
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
|
|
|
assert scoped_developers.include?(developers(:david))
|
|
|
|
assert !scoped_developers.include?(developers(:jamis))
|
|
|
|
assert_equal 1, scoped_developers.size
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-06-21 07:41:30 -04:00
|
|
|
def test_scoped_find_joins
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_developers = Developer.send(:with_scope, :find => { :joins => 'JOIN developers_projects ON id = developer_id' } ) do
|
2008-06-21 07:41:30 -04:00
|
|
|
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
|
|
|
|
|
2008-08-28 12:00:18 -04:00
|
|
|
def test_scoped_find_using_new_style_joins
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_developers = Developer.send(:with_scope, :find => { :joins => :projects }) do
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id ' }) do
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
|
|
|
|
2008-09-23 16:32:17 -04:00
|
|
|
def test_scoped_find_merges_string_array_style_and_string_style_joins
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => ["INNER JOIN posts ON posts.author_id = authors.id"]}) do
|
2008-09-23 16:32:17 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => :posts}) do
|
2008-09-23 16:32:17 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON posts.author_id = authors.id'}) do
|
2008-09-23 16:32:17 -04:00
|
|
|
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))
|
2008-10-30 06:40:09 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => ' INNER JOIN posts ON posts.author_id = authors.id '}) do
|
2008-10-30 06:40:09 -04:00
|
|
|
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))
|
2008-09-23 16:32:17 -04:00
|
|
|
assert !scoped_authors.include?(authors(:mary))
|
|
|
|
assert_equal 1, scoped_authors.size
|
|
|
|
assert_equal authors(:david).attributes, scoped_authors.first.attributes
|
|
|
|
end
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_scoped_count_include
|
|
|
|
# with the include, will retrieve only developers for the given project
|
2012-01-13 18:42:07 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects }) do
|
2012-01-16 16:14:34 -05:00
|
|
|
assert_equal 1, Developer.count(:conditions => { 'projects.id' => 2 })
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-06 14:31:05 -04:00
|
|
|
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
|
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
def test_scoped_create
|
|
|
|
new_comment = nil
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
VerySpecialComment.send(:with_scope, :create => { :post_id => 1 }) do
|
2010-09-15 11:26:54 -04:00
|
|
|
assert_equal({:post_id => 1, :type => 'VerySpecialComment' }, VerySpecialComment.scoped.send(:scope_for_create))
|
2005-11-04 14:39:50 -05:00
|
|
|
new_comment = VerySpecialComment.create :body => "Wonderful world"
|
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
assert Post.find(1).comments.include?(new_comment)
|
|
|
|
end
|
|
|
|
|
2010-10-30 15:26:38 -04:00
|
|
|
def test_scoped_create_with_join_and_merge
|
2011-02-10 14:03:25 -05:00
|
|
|
Comment.where(:body => "but Who's Buying?").joins(:post).merge(Post.where(:body => 'Peace Sells...')).with_scope do
|
2010-10-30 15:26:38 -04:00
|
|
|
assert_equal({:body => "but Who's Buying?"}, Comment.scoped.scope_for_create)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_immutable_scope
|
2005-10-28 00:53:22 -04:00
|
|
|
options = { :conditions => "name = 'David'" }
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => options) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2005-10-28 00:53:22 -04:00
|
|
|
options[:conditions] = "name != 'David'"
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
|
|
|
scope = { :find => { :conditions => "name = 'David'" }}
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, scope) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2005-11-06 05:18:51 -05:00
|
|
|
scope[:find][:conditions] = "name != 'David'"
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_scoped_with_duck_typing
|
2011-04-03 19:07:45 -04:00
|
|
|
scoping = Struct.new(:current_scope).new(:find => { :conditions => ["name = ?", 'David'] })
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, scoping) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ensure_that_method_scoping_is_correctly_restored
|
|
|
|
begin
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
raise "an exception"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
2011-04-03 19:07:45 -04:00
|
|
|
|
|
|
|
assert !Developer.scoped.where_values.include?("name = 'Jamis'")
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class NestedScopingTest < ActiveRecord::TestCase
|
2012-01-13 18:42:07 -05:00
|
|
|
fixtures :authors, :developers, :projects, :comments, :posts
|
2006-02-26 15:12:09 -05:00
|
|
|
|
|
|
|
def test_merge_options
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
|
|
|
|
Developer.send(:with_scope, :find => { :limit => 10 }) do
|
2010-01-14 03:06:33 -05:00
|
|
|
devs = Developer.scoped
|
2011-08-08 18:27:54 -04:00
|
|
|
assert_match '(salary = 80000)', devs.to_sql
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal 10, devs.taken
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-10 07:49:58 -04:00
|
|
|
def test_merge_inner_scope_has_priority
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :limit => 5 }) do
|
|
|
|
Developer.send(:with_scope, :find => { :limit => 10 }) do
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal 10, Developer.scoped.taken
|
2009-03-10 07:49:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_replace_options
|
2010-01-14 03:06:33 -05:00
|
|
|
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]
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
2010-01-14 03:06:33 -05:00
|
|
|
|
|
|
|
assert_equal 'David', Developer.scoped.send(:scope_for_create)[:name]
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-28 17:17:56 -04:00
|
|
|
def test_with_exclusive_scope_with_relation
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
Developer.all_johns
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_append_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
|
|
|
Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
|
2010-01-14 03:06:33 -05:00
|
|
|
devs = Developer.scoped
|
2011-08-08 18:27:54 -04:00
|
|
|
assert_match "(name = 'David') AND (salary = 80000)", devs.to_sql
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal(1, Developer.count)
|
|
|
|
end
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal(0, Developer.count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge_and_append_options
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => 'salary = 80000', :limit => 10 }) do
|
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
2010-01-14 03:06:33 -05:00
|
|
|
devs = Developer.scoped
|
2011-08-08 18:27:54 -04:00
|
|
|
assert_match "(salary = 80000) AND (name = 'David')", devs.to_sql
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal 10, devs.taken
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_scoped_find
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
|
|
|
|
Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_nothing_raised { Developer.find(1) }
|
|
|
|
assert_equal('David', Developer.find(:first).name)
|
|
|
|
end
|
|
|
|
assert_equal('Jamis', Developer.find(:first).name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_nested_scoped_find_include
|
2012-01-13 18:42:07 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects }) do
|
2012-01-16 16:14:34 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => { 'projects.id' => 2 } }) do
|
2006-03-27 01:19:31 -05:00
|
|
|
assert_nothing_raised { Developer.find(1) }
|
|
|
|
assert_equal('David', Developer.find(:first).name)
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_scoped_find_merged_include
|
|
|
|
# :include's remain unique and don't "double up" when merging
|
2012-01-16 16:14:34 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects, :conditions => { 'projects.id' => 2 } }) do
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects }) do
|
2012-01-13 18:42:07 -05:00
|
|
|
assert_equal 1, Developer.scoped.includes_values.uniq.length
|
|
|
|
assert_equal 'David', Developer.find(:first).name
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
# the nested scope doesn't remove the first :include
|
2012-01-16 16:14:34 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects, :conditions => { 'projects.id' => 2 } }) do
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => [] }) do
|
2012-01-13 18:42:07 -05:00
|
|
|
assert_equal 1, Developer.scoped.includes_values.uniq.length
|
|
|
|
assert_equal('David', Developer.find(:first).name)
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
# mixing array and symbol include's will merge correctly
|
2012-01-16 16:14:34 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => [:projects], :conditions => { 'projects.id' => 2 } }) do
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects }) do
|
2012-01-13 18:42:07 -05:00
|
|
|
assert_equal 1, Developer.scoped.includes_values.uniq.length
|
2006-03-27 01:19:31 -05:00
|
|
|
assert_equal('David', Developer.find(:first).name)
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_nested_scoped_find_replace_include
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :include => :projects }) do
|
|
|
|
Developer.send(:with_exclusive_scope, :find => { :include => [] }) do
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal 0, Developer.scoped.includes_values.length
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_three_level_nested_exclusive_scoped_find
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal('Jamis', Developer.find(:first).name)
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal('David', Developer.find(:first).name)
|
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'Maiha'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
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)
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "salary < 100000" }) do
|
|
|
|
Developer.send(:with_scope, :find => { :offset => 1, :order => 'id asc' }) do
|
2009-03-22 18:17:05 -04:00
|
|
|
# Oracle adapter does not generated space after asc therefore trailing space removed from regex
|
2010-09-10 14:51:09 -04:00
|
|
|
assert_sql(/ORDER BY\s+id asc/) do
|
2009-03-11 02:11:05 -04:00
|
|
|
assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
|
|
|
|
end
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merged_scoped_find_sanitizes_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { developers(:poor_jamis) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_scoped_find_combines_and_sanitizes_conditions
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.send(:with_exclusive_scope, :find => { :conditions => ['salary = ?', 9000] }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.send(:with_scope, :find => { :conditions => ['salary > ?', 9000] }) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-10 07:49:58 -04:00
|
|
|
def test_nested_scoped_create
|
|
|
|
comment = nil
|
2009-12-28 17:07:23 -05:00
|
|
|
Comment.send(:with_scope, :create => { :post_id => 1}) do
|
|
|
|
Comment.send(:with_scope, :create => { :post_id => 2}) do
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal({:post_id => 2}, Comment.scoped.send(:scope_for_create))
|
2009-03-10 07:49:58 -04:00
|
|
|
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
|
2010-01-14 03:06:33 -05:00
|
|
|
|
2009-12-28 17:07:23 -05:00
|
|
|
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
|
2010-01-14 03:06:33 -05:00
|
|
|
assert_equal({:post_id => 1}, Comment.scoped.send(:scope_for_create))
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_blank Comment.new.body
|
2009-03-10 07:49:58 -04:00
|
|
|
comment = Comment.create :body => "Hey guys"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 1, comment.post_id
|
|
|
|
assert_equal 'Hey guys', comment.body
|
|
|
|
end
|
|
|
|
|
2008-03-23 01:00:25 -04:00
|
|
|
def test_merged_scoped_find_on_blank_conditions
|
|
|
|
[nil, " ", [], {}].each do |blank|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => {:conditions => blank}) do
|
|
|
|
Developer.send(:with_scope, :find => {:conditions => blank}) do
|
2008-03-23 01:00:25 -04:00
|
|
|
assert_nothing_raised { Developer.find(:first) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merged_scoped_find_on_blank_bind_conditions
|
|
|
|
[ [""], ["",{}] ].each do |blank|
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => {:conditions => blank}) do
|
|
|
|
Developer.send(:with_scope, :find => {:conditions => blank}) do
|
2008-03-23 01:00:25 -04:00
|
|
|
assert_nothing_raised { Developer.find(:first) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_immutable_nested_scope
|
|
|
|
options1 = { :conditions => "name = 'Jamis'" }
|
|
|
|
options2 = { :conditions => "name = 'David'" }
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => options1) do
|
|
|
|
Developer.send(:with_exclusive_scope, :find => options2) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
options1[:conditions] = options2[:conditions] = nil
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(David), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_immutable_merged_scope
|
|
|
|
options1 = { :conditions => "name = 'Jamis'" }
|
|
|
|
options2 = { :conditions => "salary > 10000" }
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => options1) do
|
|
|
|
Developer.send(:with_scope, :find => options2) do
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(Jamis), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
options1[:conditions] = options2[:conditions] = nil
|
2011-05-10 14:45:16 -04:00
|
|
|
assert_equal %w(Jamis), Developer.all.map(&:name)
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ensure_that_method_scoping_is_correctly_restored
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
begin
|
2009-12-28 17:07:23 -05:00
|
|
|
Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
raise "an exception"
|
|
|
|
end
|
|
|
|
rescue
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
2011-04-03 19:07:45 -04:00
|
|
|
|
|
|
|
assert Developer.scoped.where_values.include?("name = 'David'")
|
|
|
|
assert !Developer.scoped.where_values.include?("name = 'Maiha'")
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2008-08-28 12:00:18 -04:00
|
|
|
|
|
|
|
def test_nested_scoped_find_merges_old_style_joins
|
2009-12-28 17:07:23 -05:00
|
|
|
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
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
|
|
|
|
Author.send(:with_scope, :find => { :joins => :comments }) do
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2009-12-28 17:07:23 -05:00
|
|
|
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
|
2008-08-28 12:00:18 -04:00
|
|
|
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
|
2010-07-19 22:53:54 -04:00
|
|
|
end
|