Permalink
Browse files

Use foo/bar instead of foo_bar keys for fixtures

This solves an issue with set_fixture_class class method caused by create_fixtures method's overwriting passed to it fixture model classes, when the fixtures are "namespased": foo/bar or admin/users.

The idea is to use "foo/bar" string as the name and identifier of a
fixture file bar in directory foo.  The model class of the fixture is either set with set_fixture_class method, or otherwise inferred from its name using camelize method.

Also a bug is fixed in lines 487-489 when the table names were guessed by substitution from the fixture file names, ambiguously called  table_names, instead of being taken from fixture attributes.  Now they are taken from attributes.

I plan to submit another fix so that the fixture's table name (for
example foo_bar) be defined by the fixture's model whenever possible,
instead of inferring it from the fixture's name ("foo/bar").
  • Loading branch information...
1 parent 6468ff4 commit e0ef0936193491689724880599ae26a8f5c2b5a6 Alexey Muranov committed with alexeymuranov Dec 20, 2011
Showing with 25 additions and 12 deletions.
  1. +25 −12 activerecord/lib/active_record/fixtures.rb
@@ -388,10 +388,10 @@ class Fixtures
@@all_cached_fixtures = Hash.new { |h,k| h[k] = {} }
- def self.find_table_name(table_name) # :nodoc:
+ def self.default_fixture_model_name(fixture_name) # :nodoc:
ActiveRecord::Base.pluralize_table_names ?
- table_name.to_s.singularize.camelize :
- table_name.to_s.camelize
+ fixture_name.singularize.camelize :
+ fixture_name.camelize
end
def self.reset_cache
@@ -441,9 +441,6 @@ def self.instantiate_all_loaded_fixtures(object, load_instances = true)
def self.create_fixtures(fixtures_directory, table_names, class_names = {})
table_names = [table_names].flatten.map { |n| n.to_s }
- table_names.each { |n|
- class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/')
- }
# FIXME: Apparently JK uses this.
connection = block_given? ? yield : ActiveRecord::Base.connection
@@ -458,11 +455,12 @@ def self.create_fixtures(fixtures_directory, table_names, class_names = {})
fixture_files = files_to_read.map do |path|
table_name = path.tr '/', '_'
+ fixture_name = path
- fixtures_map[path] = ActiveRecord::Fixtures.new(
+ fixtures_map[fixture_name] = new( # ActiveRecord::Fixtures.new
connection,
table_name,
- class_names[table_name.to_sym] || table_name.classify,
+ class_names[fixture_name] || default_fixture_model_name(fixture_name),
::File.join(fixtures_directory, path))
end
@@ -723,14 +721,29 @@ module TestFixtures
self.use_instantiated_fixtures = false
self.pre_loaded_fixtures = false
- self.fixture_class_names = Hash.new do |h, table_name|
- h[table_name] = ActiveRecord::Fixtures.find_table_name(table_name)
+ self.fixture_class_names = Hash.new do |h, fixture_name|
+ h[fixture_name] = ActiveRecord::Fixtures.default_fixture_model_name(fixture_name)
end
end
module ClassMethods
+ # Sets the model class for a fixture when the class name cannot be inferred from the fixture name.
+ #
+ # Examples:
+ #
+ # set_fixture_class :some_fixture => SomeModel,
+ # 'namespaced/fixture' => Another::Model
+ #
+ # The keys must be the fixture names, that coincide with the short paths to the fixture files.
+ #--
+ # It is also possible to pass the class name instead of the class:
+ # set_fixture_class 'some_fixture' => 'SomeModel'
+ # I think this option is redundant, i propose to deprecate it.
+ # Isn't it easier to always pass the class itself?
+ # (2011-12-20 alexeymuranov)
+ #++
def set_fixture_class(class_names = {})
- self.fixture_class_names = self.fixture_class_names.merge(class_names)
+ self.fixture_class_names = self.fixture_class_names.merge(class_names.stringify_keys)
end
def fixtures(*fixture_names)
@@ -770,7 +783,7 @@ def setup_fixture_accessors(fixture_names = nil)
fixture_names = Array.wrap(fixture_names || fixture_table_names)
methods = Module.new do
fixture_names.each do |fixture_name|
- fixture_name = fixture_name.to_s.tr('./', '_')
+ fixture_name = fixture_name.to_s.tr('./', '_') # TODO: use fixture_name variable for only one form of fixture names ("admin/users" for example)
define_method(fixture_name) do |*fixtures|
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload

0 comments on commit e0ef093

Please sign in to comment.