aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/fixtures.rb24
-rwxr-xr-xactiverecord/test/associations_test.rb4
-rwxr-xr-xactiverecord/test/fixtures_test.rb13
4 files changed, 32 insertions, 11 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 3cde1dce39..527e7c4510 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixtures: people(:technomancy, :josh) returns both fixtures. #7880 [technomancy, Josh Peek]
+
* Calculations support non-numeric foreign keys. #8154 [kamal]
* with_scope is protected. #8524 [Josh Peek]
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 47a77e4249..0a8b75e014 100755
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -501,20 +501,26 @@ module Test #:nodoc:
end
end
- def self.setup_fixture_accessors(table_names=nil)
+ def self.setup_fixture_accessors(table_names = nil)
(table_names || fixture_table_names).each do |table_name|
- table_name = table_name.to_s.tr('.','_')
+ table_name = table_name.to_s.tr('.', '_')
+
+ define_method(table_name) do |*fixtures|
+ force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
- define_method(table_name) do |fixture, *optionals|
- force_reload = optionals.shift
@fixture_cache[table_name] ||= Hash.new
- @fixture_cache[table_name][fixture] = nil if force_reload
- if @loaded_fixtures[table_name][fixture.to_s]
- @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find
- else
- raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
+ instances = fixtures.map do |fixture|
+ @fixture_cache[table_name].delete(fixture) if force_reload
+
+ if @loaded_fixtures[table_name][fixture.to_s]
+ @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find
+ else
+ raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
+ end
end
+
+ instances.size == 1 ? instances.first : instances
end
end
end
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index fe2a720c32..9dec102e7c 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -743,9 +743,9 @@ class HasManyAssociationsTest < Test::Unit::TestCase
def test_find_or_create
number_of_clients = companies(:first_firm).clients.size
the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
- assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size
+ assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
- assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size
+ assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
end
def test_deleting
diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb
index d9973683cc..3e61c7b465 100755
--- a/activerecord/test/fixtures_test.rb
+++ b/activerecord/test/fixtures_test.rb
@@ -259,6 +259,19 @@ class FixturesWithoutInstantiationTest < Test::Unit::TestCase
assert_equal "Jamis", developers(:jamis).name
assert_equal 50, accounts(:signals37).credit_limit
end
+
+ def test_accessor_methods_with_multiple_args
+ assert_equal 2, topics(:first, :second).size
+ assert_raise(StandardError) { topics([:first, :second]) }
+ end
+
+ uses_mocha 'reloading_fixtures_through_accessor_methods' do
+ def test_reloading_fixtures_through_accessor_methods
+ assert_equal "The First Topic", topics(:first).title
+ @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
+ assert_equal "Fresh Topic!", topics(:first, true).title
+ end
+ end
end