aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/fixtures.rb
diff options
context:
space:
mode:
authorRoque Pinel <repinel@gmail.com>2015-06-15 23:16:39 -0400
committerRoque Pinel <repinel@gmail.com>2015-09-11 14:29:06 -0400
commit2acec4657752d441ab87b9f5862d7918843d6409 (patch)
tree8c838ca318721dd615165b498e1bd74bd17cc1cf /activerecord/lib/active_record/fixtures.rb
parentbbf0d35bf6148752911c1da4b7449450faea8755 (diff)
downloadrails-2acec4657752d441ab87b9f5862d7918843d6409.tar.gz
rails-2acec4657752d441ab87b9f5862d7918843d6409.tar.bz2
rails-2acec4657752d441ab87b9f5862d7918843d6409.zip
Allow fixtures YAML files to set the model class in the file itself
Currently, `set_fixture_class` is only available using the `TestFixtures` concern and it is ignored for `rake db:fixtures:load`. Using the correct model class, it is possible for the fixture load to also load the associations from the YAML files (e.g., `:belongs_to` and `:has_many`).
Diffstat (limited to 'activerecord/lib/active_record/fixtures.rb')
-rw-r--r--activerecord/lib/active_record/fixtures.rb42
1 files changed, 33 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index f1dc56df63..e5d51fc5fb 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -395,6 +395,23 @@ module ActiveRecord
# <<: *DEFAULTS
#
# Any fixture labeled "DEFAULTS" is safely ignored.
+ #
+ # == Support to custom model class
+ #
+ # You can also set the model class in your fixtures YAML file.
+ # This is helpful when fixtures are being loaded outside tests and
+ # you cannot use +set_fixture_class+, e.g., when running
+ # <tt>rake db:fixtures:load</tt>.
+ #
+ # To load the fixtures file +test/fixtures/accounts.yml+ as the +User+
+ # model, use:
+ #
+ # _fixture:
+ # model_class: User
+ # david:
+ # name: David
+ #
+ # Any fixture labeled "_fixture" is safely ignored.
class FixtureSet
#--
# An instance of FixtureSet is normally stored in a single YAML file and
@@ -578,21 +595,16 @@ module ActiveRecord
@name = name
@path = path
@config = config
- @model_class = nil
- if class_name.is_a?(Class) # TODO: Should be an AR::Base type class, or any?
- @model_class = class_name
- else
- @model_class = class_name.safe_constantize if class_name
- end
+ self.model_class = class_name
+
+ @fixtures = read_fixture_files(path)
@connection = connection
@table_name = ( model_class.respond_to?(:table_name) ?
model_class.table_name :
self.class.default_fixture_table_name(name, config) )
-
- @fixtures = read_fixture_files path, @model_class
end
def [](x)
@@ -761,13 +773,25 @@ module ActiveRecord
@column_names ||= @connection.columns(@table_name).collect(&:name)
end
- def read_fixture_files(path, model_class)
+ def model_class=(class_name)
+ if class_name.is_a?(Class) # TODO: Should be an AR::Base type class, or any?
+ @model_class = class_name
+ else
+ @model_class = class_name.safe_constantize if class_name
+ end
+ end
+
+ # Loads the fixtures from the YAML file at +path+.
+ # If the file sets the +model_class+ and current instance value is not set,
+ # it uses the file value.
+ def read_fixture_files(path)
yaml_files = Dir["#{path}/{**,*}/*.yml"].select { |f|
::File.file?(f)
} + [yaml_file_path(path)]
yaml_files.each_with_object({}) do |file, fixtures|
FixtureSet::File.open(file) do |fh|
+ self.model_class ||= fh.model_class if fh.model_class
fh.each do |fixture_name, row|
fixtures[fixture_name] = ActiveRecord::Fixture.new(row, model_class)
end