diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 62 |
1 files changed, 10 insertions, 52 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index d8f88cf2be..49df63d585 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -6,7 +6,6 @@ rescue LoadError end require 'yaml' -require 'csv' require 'zlib' require 'active_support/dependencies' require 'active_support/core_ext/array/wrap' @@ -658,73 +657,32 @@ module ActiveRecord def read_fixture_files if ::File.file?(yaml_file_path) read_yaml_fixture_files - elsif ::File.file?(csv_file_path) - read_csv_fixture_files else - raise FixturesFileNotFound, "Could not find #{yaml_file_path} or #{csv_file_path}" + raise FixturesFileNotFound, "Could not find #{yaml_file_path}" end end def read_yaml_fixture_files - yaml_string = (Dir["#{@fixture_path}/**/*.yml"].select { |f| - File.file?(f) - } + [yaml_file_path]).map { |file_path| IO.read(file_path) }.join - - if yaml = parse_yaml_string(yaml_string) - # If the file is an ordered map, extract its children. - yaml_value = - if yaml.respond_to?(:type_id) && yaml.respond_to?(:value) - yaml.value - else - [yaml] - end - - yaml_value.each do |fixture| - raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{fixture}" unless fixture.respond_to?(:each) - fixture.each do |name, data| - unless data - raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{name} (nil)" - end - - fixtures[name] = ActiveRecord::Fixture.new(data, model_class) + yaml_files = Dir["#{@fixture_path}/**/*.yml"].select { |f| + ::File.file?(f) + } + [yaml_file_path] + + yaml_files.each do |file| + Fixtures::File.open(file) do |fh| + fh.each do |name, row| + fixtures[name] = ActiveRecord::Fixture.new(row, model_class) end end end end - def read_csv_fixture_files - reader = CSV.parse(erb_render(IO.read(csv_file_path))) - header = reader.shift - i = 0 - reader.each do |row| - data = {} - row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip } - fixtures["#{@class_name.to_s.underscore}_#{i+=1}"] = ActiveRecord::Fixture.new(data, model_class) - end - end - deprecate :read_csv_fixture_files - def yaml_file_path "#{@fixture_path}.yml" end - def csv_file_path - @fixture_path + ".csv" - end - def yaml_fixtures_key(path) ::File.basename(@fixture_path).split(".").first end - - def parse_yaml_string(fixture_content) - YAML::load(erb_render(fixture_content)) - rescue => error - raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. 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}" - end - - def erb_render(fixture_content) - ERB.new(fixture_content).result - end end class Fixture #:nodoc: @@ -799,7 +757,7 @@ module ActiveRecord def fixtures(*fixture_names) if fixture_names.first == :all - fixture_names = Dir["#{fixture_path}/**/*.{yml,csv}"] + fixture_names = Dir["#{fixture_path}/**/*.{yml}"] fixture_names.map! { |f| f[(fixture_path.size + 1)..-5] } else fixture_names = fixture_names.flatten.map { |n| n.to_s } |