From 6d9ee4a306672110c97b9dd9af77015208106654 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Fri, 3 Jun 2005 11:49:34 +0000 Subject: Added fixture accessors that can be used when fixture instantiation is disabled, "model_name(:fixture_name)" git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1383 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 8 ++++++++ activerecord/lib/active_record/fixtures.rb | 30 ++++++++++++++++++++++++++++-- activerecord/test/fixtures_test.rb | 7 +++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 82c8e5b6df..d44d966f5c 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,13 @@ *SVN* +* Added fixture accessor methods that can be used when instantiated fixtures are disabled. + + fixtures :web_sites + + def test_something + assert_equal "Ruby on Rails", web_sites(:rubyonrails).name + end + * Added DoubleRenderError exception that'll be raised if render* is called twice #518 [Nicholas Seckar] * Fixed exceptions occuring after render has been called #1096 [Nicholas Seckar] diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index ce79efc648..30b587b4ec 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -129,6 +129,16 @@ require 'csv' # - to keep the fixture instance (@web_sites) available, but do not automatically 'find' each instance: # self.use_instantiated_fixtures = :no_instances # +# Even if auto-instantiated fixtures are disabled, you can still access them +# by name via special dynamic methods. Each method has the same name as the +# model, and accepts the name of the fixture to instantiate: +# +# fixtures :web_sites +# +# def test_find +# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name +# end +# # = Dynamic fixtures with ERb # # Some times you don't care about the content of the fixtures as much as you care about the volume. In these cases, you can @@ -401,8 +411,10 @@ module Test #:nodoc: self.pre_loaded_fixtures = false def self.fixtures(*table_names) - self.fixture_table_names |= table_names.flatten - require_fixture_classes + table_names = table_names.flatten + self.fixture_table_names |= table_names + require_fixture_classes(table_names) + setup_fixture_accessors(table_names) end def self.require_fixture_classes(table_names=nil) @@ -415,11 +427,25 @@ module Test #:nodoc: end end + def self.setup_fixture_accessors(table_names=nil) + (table_names || fixture_table_names).each do |table_name| + table_name = table_name.to_s.tr('.','_') + 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 + @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find + end + end + end + def setup_with_fixtures if pre_loaded_fixtures && !use_transactional_fixtures raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' end + @fixture_cache = Hash.new + # Load fixtures once and begin transaction. if use_transactional_fixtures load_fixtures unless @already_loaded_fixtures diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index e1f2f6d24c..6de231a4fb 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -3,6 +3,7 @@ require 'fixtures/topic' require 'fixtures/developer' require 'fixtures/company' require 'fixtures/task' +require 'fixtures/reply' class FixturesTest < Test::Unit::TestCase fixtures :topics, :developers, :accounts, :tasks @@ -130,6 +131,12 @@ class FixturesWithoutInstantiationTest < Test::Unit::TestCase def test_fixtures_from_root_yml_without_instantiation assert_nil @unknown end + + def test_accessor_methods + assert_equal "The First Topic", topics(:first).title + assert_equal "Jamis", developers(:jamis).name + assert_equal 50, accounts(:signals37).credit_limit + end end -- cgit v1.2.3