Method: ConfigScripts::Seeds::SeedType#read_from_folder

Defined in:
lib/config_scripts/seeds/seed_type.rb

#read_from_folder(folder) ⇒ Object

This method reads the seed data from a file, and creates new records from it.

This will extract all the rows from the CSV file, and use #read_value_for_attribute to get the attributes for the record from each cell in the CSV file.

If this seed type has no attributes, this method will not try to read the file.

Parameters:

  • The full path to the folder with the seed files.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/config_scripts/seeds/seed_type.rb', line 202

def read_from_folder(folder)
  return unless attributes.any?
  CSV.open(File.join(folder, "#{self.filename}.csv"), headers: true) do |csv|
    csv.each do |row|
      record = self.klass.new
      row.each do |attribute, value|
        attribute = attribute.to_sym
        value = self.read_value_for_attribute(value, attribute)
        record.send("#{attribute}=", value)
      end
      
      begin
        record.save!
      rescue
        puts "#{self.filename}.csv"
        puts "#{row}"
        raise
      end
    end
  end
end