Sha256: ca8a29cbea82fa05e525c199b4a77268520853a3d3490c749846fc1e129e0df1

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'csv'
require 'erb'

module RailsCsvFixtures
  module CsvFixtures
    extend ActiveSupport::Concern

    included do
      alias_method_chain :read_fixture_files, :csv_support
    end

    def read_fixture_files_with_csv_support
      if ::File.file?(csv_file_path)
        read_csv_fixture_files
      else
        read_fixture_files_without_csv_support
      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

    def csv_file_path
      @fixture_path + ".csv"
    end  
    
    def erb_render(fixture_content)
      ERB.new(fixture_content).result
    end
  end
end

require 'active_record/fixtures'
::ActiveRecord::Fixtures.send :include, RailsCsvFixtures::CsvFixtures

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-csv-fixtures-0.0.1 lib/rails-csv-fixtures/active_record_csv_fixtures.rb