2011-06-06 14:17:44 -04: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
|
|
|
|
|
2011-08-13 08:54:54 -04:00
|
|
|
marshalled = Marshal.dump(david)
|
|
|
|
|
2012-09-25 16:20:26 -04:00
|
|
|
# Marshaling an association shouldn't make it unusable by wiping its reflection.
|
|
|
|
assert_not_nil david.association(:projects).reflection
|
|
|
|
|
|
|
|
david_too = Marshal.load(marshalled)
|
|
|
|
assert_equal projects(:action_controller), david_too.projects.find_most_recent
|
2005-11-06 14:05:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_marshalling_named_extensions
|
|
|
|
david = developers(:david)
|
|
|
|
assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent
|
|
|
|
|
2011-08-13 08:54:54 -04:00
|
|
|
marshalled = Marshal.dump(david)
|
|
|
|
david = Marshal.load(marshalled)
|
|
|
|
|
2005-11-06 14:05:42 -05:00
|
|
|
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
|
2013-08-01 13:53:56 -04:00
|
|
|
extend!(Developer)
|
|
|
|
extend!(MyApplication::Business::Developer)
|
|
|
|
|
|
|
|
assert Object.const_get 'DeveloperAssociationNameAssociationExtension'
|
|
|
|
assert MyApplication::Business.const_get 'DeveloperAssociationNameAssociationExtension'
|
2008-09-12 06:02:40 -04:00
|
|
|
end
|
2011-02-20 20:09:41 -05:00
|
|
|
|
2011-12-08 14:58:59 -05:00
|
|
|
def test_proxy_association_after_scoped
|
|
|
|
post = posts(:welcome)
|
|
|
|
assert_equal post.association(:comments), post.comments.the_association
|
2012-07-27 12:27:47 -04:00
|
|
|
assert_equal post.association(:comments), post.comments.where('1=1').the_association
|
2011-12-08 14:58:59 -05:00
|
|
|
end
|
|
|
|
|
2011-02-20 20:09:41 -05:00
|
|
|
private
|
|
|
|
|
2013-08-01 13:53:56 -04:00
|
|
|
def extend!(model)
|
2013-12-11 16:28:32 -05:00
|
|
|
builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, nil, {}) { }
|
|
|
|
builder.define_extensions(model)
|
2011-02-20 20:09:41 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|