Skip to content

Commit

Permalink
removing CSV fixture support
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 23, 2011
1 parent 081b36c commit 1716da0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 58 deletions.
62 changes: 10 additions & 52 deletions activerecord/lib/active_record/fixtures.rb
Expand Up @@ -6,7 +6,6 @@
end

require 'yaml'
require 'csv'
require 'zlib'
require 'active_support/dependencies'
require 'active_support/core_ext/array/wrap'
Expand Down Expand Up @@ -658,73 +657,32 @@ def column_names
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:
Expand Down Expand Up @@ -799,7 +757,7 @@ def set_fixture_class(class_names = {})

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 }
Expand Down
6 changes: 0 additions & 6 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -174,12 +174,6 @@ def test_dirty_dirty_yaml_file
end
end

def test_empty_csv_fixtures
assert_deprecated do
assert_not_nil ActiveRecord::Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/csv/accounts")
end
end

def test_omap_fixtures
assert_nothing_raised do
fixtures = ActiveRecord::Fixtures.new(Account.connection, 'categories', 'Category', FIXTURES_ROOT + "/categories_ordered")
Expand Down

0 comments on commit 1716da0

Please sign in to comment.