2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/developer'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/comment'
|
|
|
|
require 'models/post'
|
|
|
|
require 'models/category'
|
2005-07-22 16:05:42 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class MethodScopingTest < ActiveRecord::TestCase
|
2008-07-31 16:59:53 -04:00
|
|
|
fixtures :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
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => 'just a test...' }) do
|
2006-03-13 12:28:55 -05:00
|
|
|
assert_equal 'just a test...', Developer.send(:current_scoped_methods)[:find][:conditions]
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_find
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.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
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.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
|
|
|
|
2006-01-03 22:43:28 -05:00
|
|
|
def test_scoped_find_combines_conditions
|
|
|
|
Developer.with_scope(:find => { :conditions => "salary = 9000" }) do
|
|
|
|
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
|
|
|
|
Developer.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
|
|
|
|
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
|
|
|
|
Developer.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
|
|
|
|
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
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
2006-01-03 22:43:28 -05:00
|
|
|
assert_equal [developers(:david)], Developer.find(: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
|
|
|
|
Developer.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_options_select_replaces_scope_select
|
|
|
|
Developer.with_scope(:find => { :select => "id, name" }) do
|
|
|
|
developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
|
|
|
|
assert_equal 80000, developer.salary
|
|
|
|
assert !developer.has_attribute?(:name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_scoped_count
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.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
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.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
|
|
|
|
scoped_developers = Developer.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
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-06-21 07:41:30 -04:00
|
|
|
def test_scoped_find_joins
|
|
|
|
scoped_developers = Developer.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
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_scoped_count_include
|
|
|
|
# with the include, will retrieve only developers for the given project
|
|
|
|
Developer.with_scope(:find => { :include => :projects }) do
|
2006-09-26 13:02:45 -04:00
|
|
|
assert_equal 1, Developer.count(:conditions => 'projects.id = 2')
|
2006-03-27 01:19:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
def test_scoped_create
|
|
|
|
new_comment = nil
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
VerySpecialComment.with_scope(:create => { :post_id => 1 }) do
|
2006-03-13 12:28:55 -05:00
|
|
|
assert_equal({ :post_id => 1 }, VerySpecialComment.send(:current_scoped_methods)[: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
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_immutable_scope
|
2005-10-28 00:53:22 -04:00
|
|
|
options = { :conditions => "name = 'David'" }
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => options) do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
options[:conditions] = "name != 'David'"
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
|
|
|
scope = { :find => { :conditions => "name = 'David'" }}
|
|
|
|
Developer.with_scope(scope) do
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
scope[:find][:conditions] = "name != 'David'"
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_scoped_with_duck_typing
|
|
|
|
scoping = Struct.new(:method_scoping).new(:find => { :conditions => ["name = ?", 'David'] })
|
|
|
|
Developer.with_scope(scoping) do
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ensure_that_method_scoping_is_correctly_restored
|
|
|
|
scoped_methods = Developer.instance_eval('current_scoped_methods')
|
|
|
|
|
|
|
|
begin
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
|
|
|
|
raise "an exception"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class NestedScopingTest < ActiveRecord::TestCase
|
2006-03-27 01:19:31 -05:00
|
|
|
fixtures :developers, :projects, :comments, :posts
|
2006-02-26 15:12:09 -05:00
|
|
|
|
|
|
|
def test_merge_options
|
|
|
|
Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do
|
|
|
|
Developer.with_scope(:find => { :limit => 10 }) do
|
|
|
|
merged_option = Developer.instance_eval('current_scoped_methods')[:find]
|
|
|
|
assert_equal({ :conditions => 'salary = 80000', :limit => 10 }, merged_option)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace_options
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
|
|
|
Developer.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
|
|
|
|
assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer.instance_eval('current_scoped_methods'))
|
2006-03-13 12:28:55 -05:00
|
|
|
assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer.send(:scoped_methods)[-1])
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_append_conditions
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
|
|
|
Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do
|
|
|
|
appended_condition = Developer.instance_eval('current_scoped_methods')[:find][:conditions]
|
2008-03-23 01:00:25 -04:00
|
|
|
assert_equal("(name = 'David') AND (salary = 80000)", appended_condition)
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal(1, Developer.count)
|
|
|
|
end
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
|
|
|
|
assert_equal(0, Developer.count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge_and_append_options
|
|
|
|
Developer.with_scope(:find => { :conditions => 'salary = 80000', :limit => 10 }) do
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
|
|
|
merged_option = Developer.instance_eval('current_scoped_methods')[:find]
|
2008-03-23 01:00:25 -04:00
|
|
|
assert_equal({ :conditions => "(salary = 80000) AND (name = 'David')", :limit => 10 }, merged_option)
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_scoped_find
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
|
|
|
|
Developer.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
|
|
|
|
|
2006-03-27 01:19:31 -05:00
|
|
|
def test_nested_scoped_find_include
|
|
|
|
Developer.with_scope(:find => { :include => :projects }) do
|
|
|
|
Developer.with_scope(:find => { :conditions => "projects.id = 2" }) do
|
|
|
|
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
|
|
|
|
Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
|
|
|
|
Developer.with_scope(:find => { :include => :projects }) do
|
|
|
|
assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
|
|
|
|
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
|
|
|
# the nested scope doesn't remove the first :include
|
|
|
|
Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
|
|
|
|
Developer.with_scope(:find => { :include => [] }) do
|
|
|
|
assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
|
|
|
|
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
|
|
|
# mixing array and symbol include's will merge correctly
|
|
|
|
Developer.with_scope(:find => { :include => [:projects], :conditions => "projects.id = 2" }) do
|
|
|
|
Developer.with_scope(:find => { :include => :projects }) do
|
|
|
|
assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
|
|
|
|
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
|
|
|
|
Developer.with_scope(:find => { :include => :projects }) do
|
|
|
|
Developer.with_exclusive_scope(:find => { :include => [] }) do
|
|
|
|
assert_equal 0, Developer.instance_eval('current_scoped_methods')[:find][:include].length
|
|
|
|
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
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
|
|
|
|
assert_equal('Jamis', Developer.find(:first).name)
|
|
|
|
|
|
|
|
Developer.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
|
|
|
|
assert_equal('David', Developer.find(:first).name)
|
|
|
|
|
|
|
|
Developer.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.with_scope(:find => { :conditions => "salary < 100000" }) do
|
|
|
|
Developer.with_scope(:find => { :offset => 1 }) do
|
2006-02-26 18:03:59 -05:00
|
|
|
assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
|
2006-02-26 15:12:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merged_scoped_find_sanitizes_conditions
|
|
|
|
Developer.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.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.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.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.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
|
|
|
|
Developer.with_scope(:find => { :conditions => ['salary > ?', 9000] }) do
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-23 01:00:25 -04:00
|
|
|
def test_merged_scoped_find_on_blank_conditions
|
|
|
|
[nil, " ", [], {}].each do |blank|
|
|
|
|
Developer.with_scope(:find => {:conditions => blank}) do
|
|
|
|
Developer.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.with_scope(:find => {:conditions => blank}) do
|
|
|
|
Developer.with_scope(:find => {:conditions => blank}) do
|
|
|
|
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'" }
|
|
|
|
Developer.with_scope(:find => options1) do
|
|
|
|
Developer.with_exclusive_scope(:find => options2) do
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
options1[:conditions] = options2[:conditions] = nil
|
|
|
|
assert_equal %w(David), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_immutable_merged_scope
|
|
|
|
options1 = { :conditions => "name = 'Jamis'" }
|
|
|
|
options2 = { :conditions => "salary > 10000" }
|
|
|
|
Developer.with_scope(:find => options1) do
|
|
|
|
Developer.with_scope(:find => options2) do
|
|
|
|
assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
|
|
|
|
options1[:conditions] = options2[:conditions] = nil
|
|
|
|
assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ensure_that_method_scoping_is_correctly_restored
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
|
|
|
scoped_methods = Developer.instance_eval('current_scoped_methods')
|
|
|
|
begin
|
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
|
|
|
|
raise "an exception"
|
|
|
|
end
|
|
|
|
rescue
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class HasManyScopingTest< ActiveRecord::TestCase
|
2005-07-22 16:05:42 -04:00
|
|
|
fixtures :comments, :posts
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def setup
|
|
|
|
@welcome = Post.find(1)
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a comment...', Comment.what_are_you
|
|
|
|
assert_equal 'a comment...', @welcome.comments.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_scoped
|
|
|
|
assert_equal 4, Comment.search_by_type('Comment').size
|
|
|
|
assert_equal 2, @welcome.comments.search_by_type('Comment').size
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-07-22 16:05:42 -04:00
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 4, Comment.find_all_by_type('Comment').size
|
|
|
|
assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
|
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_nested_scope
|
2005-11-06 05:18:51 -05:00
|
|
|
Comment.with_scope(:find => { :conditions => '1=1' }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal 'a comment...', @welcome.comments.what_are_you
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
|
2005-10-23 15:02:38 -04:00
|
|
|
fixtures :posts, :categories, :categories_posts
|
2005-07-22 16:05:42 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@welcome = Post.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a category...', Category.what_are_you
|
|
|
|
assert_equal 'a category...', @welcome.categories.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
2006-02-28 12:46:16 -05:00
|
|
|
assert_equal 4, Category.find_all_by_type('SpecialCategory').size
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal 0, @welcome.categories.find_all_by_type('SpecialCategory').size
|
|
|
|
assert_equal 2, @welcome.categories.find_all_by_type('Category').size
|
|
|
|
end
|
2005-10-25 14:14:09 -04:00
|
|
|
|
2006-02-26 15:12:09 -05:00
|
|
|
def test_nested_scope
|
2005-11-06 05:18:51 -05:00
|
|
|
Category.with_scope(:find => { :conditions => '1=1' }) do
|
2006-02-26 15:12:09 -05:00
|
|
|
assert_equal 'a comment...', @welcome.comments.what_are_you
|
2005-11-06 05:18:51 -05:00
|
|
|
end
|
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
|
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class BelongsToScopingTest< ActiveRecord::TestCase
|
2005-07-22 16:05:42 -04:00
|
|
|
fixtures :comments, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@greetings = Comment.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_method
|
|
|
|
assert_equal 'a post...', Post.what_are_you
|
|
|
|
assert_equal 'a post...', @greetings.post.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 4, Post.find_all_by_type('Post').size
|
|
|
|
assert_equal 1, @greetings.post.find_all_by_type('Post').size
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class HasOneScopingTest< ActiveRecord::TestCase
|
2005-07-22 16:05:42 -04:00
|
|
|
fixtures :comments, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@sti_comments = Post.find(4)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a comment...', Comment.what_are_you
|
|
|
|
assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
|
|
|
|
assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
|
|
|
|
assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-10-06 18:21:10 -04:00
|
|
|
=end
|