diff options
author | Jamis Buck <jamis@37signals.com> | 2005-06-03 11:49:34 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-06-03 11:49:34 +0000 |
commit | 6d9ee4a306672110c97b9dd9af77015208106654 (patch) | |
tree | f16243589b66bbf1699905eee489bb43bd47c0cf /activerecord/lib | |
parent | 100fd7269990d5ecb6dc0f9731cce6714d5ebf76 (diff) | |
download | rails-6d9ee4a306672110c97b9dd9af77015208106654.tar.gz rails-6d9ee4a306672110c97b9dd9af77015208106654.tar.bz2 rails-6d9ee4a306672110c97b9dd9af77015208106654.zip |
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
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 30 |
1 files changed, 28 insertions, 2 deletions
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 |