2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:30:42 -05:00
|
|
|
require 'models/post'
|
|
|
|
require 'models/comment'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/developer'
|
2008-09-12 06:02:40 -04:00
|
|
|
require 'models/company_in_module'
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class AssociationsExtensionsTest < ActiveRecord::TestCase
|
2005-11-06 14:25:58 -05:00
|
|
|
fixtures :projects, :developers, :developers_projects, :comments, :posts
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2005-11-06 14:05:42 -05:00
|
|
|
def test_extension_on_has_many
|
|
|
|
assert_equal comments(:more_greetings), posts(:welcome).comments.find_most_recent
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-11-03 04:06:42 -05:00
|
|
|
def test_extension_on_habtm
|
|
|
|
assert_equal projects(:action_controller), developers(:david).projects.find_most_recent
|
|
|
|
end
|
2005-11-06 14:05:42 -05:00
|
|
|
|
|
|
|
def test_named_extension_on_habtm
|
|
|
|
assert_equal projects(:action_controller), developers(:david).projects_extended_by_name.find_most_recent
|
|
|
|
end
|
2006-04-10 21:10:42 -04:00
|
|
|
|
|
|
|
def test_named_two_extensions_on_habtm
|
|
|
|
assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_twice.find_most_recent
|
|
|
|
assert_equal projects(:active_record), developers(:david).projects_extended_by_name_twice.find_least_recent
|
|
|
|
end
|
2007-09-17 17:19:44 -04:00
|
|
|
|
|
|
|
def test_named_extension_and_block_on_habtm
|
|
|
|
assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_and_block.find_most_recent
|
|
|
|
assert_equal projects(:active_record), developers(:david).projects_extended_by_name_and_block.find_least_recent
|
|
|
|
end
|
2011-02-20 20:09:41 -05:00
|
|
|
|
2011-01-28 19:16:44 -05:00
|
|
|
def test_extension_with_scopes
|
|
|
|
assert_equal comments(:greetings), posts(:welcome).comments.offset(1).find_most_recent
|
|
|
|
assert_equal comments(:greetings), posts(:welcome).comments.not_again.find_most_recent
|
|
|
|
end
|
2007-09-17 17:19:44 -04:00
|
|
|
|
2005-11-06 14:05:42 -05:00
|
|
|
def test_marshalling_extensions
|
|
|
|
david = developers(:david)
|
|
|
|
assert_equal projects(:action_controller), david.projects.find_most_recent
|
|
|
|
|
|
|
|
david = Marshal.load(Marshal.dump(david))
|
|
|
|
assert_equal projects(:action_controller), david.projects.find_most_recent
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_marshalling_named_extensions
|
|
|
|
david = developers(:david)
|
|
|
|
assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent
|
|
|
|
|
|
|
|
david = Marshal.load(Marshal.dump(david))
|
|
|
|
assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent
|
2005-11-03 04:06:42 -05:00
|
|
|
end
|
2008-09-12 06:02:40 -04:00
|
|
|
|
2010-08-18 21:44:31 -04:00
|
|
|
def test_extension_name
|
2011-02-20 20:09:41 -05:00
|
|
|
assert_equal 'DeveloperAssociationNameAssociationExtension', extension_name(Developer)
|
|
|
|
assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer)
|
|
|
|
assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer)
|
2008-09-12 06:02:40 -04:00
|
|
|
end
|
2011-02-20 20:09:41 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extension_name(model)
|
|
|
|
builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, {}) { }
|
|
|
|
builder.send(:wrap_block_extension)
|
|
|
|
builder.options[:extend].first.name
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|