Module: Sniff::Fixture

Extended by:
Fixture
Included in:
Fixture
Defined in:
lib/sniff/fixture.rb

Instance Method Summary collapse

Instance Method Details

#load_fixtures(fixtures_path) ⇒ Object

FIXME TODO this doesn’t work for models where the model name is different from the table name (e.g. PetroleumAdministrationForDefenseDistrict, ResidentialEnergyConsumptionSurveyResponse)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sniff/fixture.rb', line 9

def load_fixtures(fixtures_path)
  Encoding.default_external = 'UTF-8' if Object.const_defined?('Encoding')
  Dir.glob(File.join(fixtures_path, '**/*.csv')) do |fixture_file|
    table_name = File.basename(fixture_file, '.csv')
    model_name = table_name.singularize.camelize
    next unless Object.const_defined?(model_name)

    model = model_name.constantize
    model.auto_upgrade! unless model.table_exists?
    ActiveRecord::Base.logger.info "Loading fixture #{fixture_file}"
    CSV.foreach(fixture_file, :headers => true) do |row|
      ActiveRecord::Base.connection.insert_fixture(row, table_name) rescue ActiveRecord::RecordNotUnique
    end
  end
end