From 2acec4657752d441ab87b9f5862d7918843d6409 Mon Sep 17 00:00:00 2001 From: Roque Pinel Date: Mon, 15 Jun 2015 23:16:39 -0400 Subject: 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`). --- activerecord/lib/active_record/fixture_set/file.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/fixture_set') diff --git a/activerecord/lib/active_record/fixture_set/file.rb b/activerecord/lib/active_record/fixture_set/file.rb index 8132310c91..9b74ed0ef7 100644 --- a/activerecord/lib/active_record/fixture_set/file.rb +++ b/activerecord/lib/active_record/fixture_set/file.rb @@ -18,23 +18,34 @@ module ActiveRecord def initialize(file) @file = file @rows = nil + @raw_rows = nil + @model_class = nil end def each(&block) rows.each(&block) end + def model_class + return @model_class if @model_class + row = raw_rows.find { |fixture_name, _| fixture_name == '_fixture' } + @model_class = row.last['model_class'] if row + end private def rows - return @rows if @rows + @rows ||= raw_rows.reject { |fixture_name, _| fixture_name == '_fixture' } + end + + def raw_rows + return @raw_rows if @raw_rows begin data = YAML.load(render(IO.read(@file))) rescue ArgumentError, Psych::SyntaxError => error raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace end - @rows = data ? validate(data).to_a : [] + @raw_rows = data ? validate(data).to_a : [] end def render(content) -- cgit v1.2.3