class DirtySeed::Seeder
Represents an Active Record model
Attributes
Public Class Methods
Initializes an record @param model [DirtySeed::Model] @return [DirtySeed::Seeder]
# File lib/dirty_seed/seeder.rb, line 11 def initialize(model) @model = model @records = [] @errors = [] end
Public Instance Methods
Creates records @param count [Integer] @return [Array<Object>]
# File lib/dirty_seed/seeder.rb, line 20 def seed(qty = 1) self.quantity = qty logger.seed_model_message(model) create_records records end
Private Instance Methods
Adds record errors @param record [Object] instance of model @return [void]
# File lib/dirty_seed/seeder.rb, line 92 def add_record_errors(record) self.errors |= record.errors.full_messages self.successive_errors = successive_errors + 1 logger.failure end
Adds standard error @param error [Error] error inheriting from StandardError @return [void]
# File lib/dirty_seed/seeder.rb, line 101 def add_standard_error(error) self.errors |= [logger.clean(error.message)] self.successive_errors = successive_errors + 1 logger.failure end
Generate associations params
Each sub-array contains the association name and a collection of values
@return [Array<Array>] @example
[ ["alfa", [#<Alfa>, #<Alfa>]], ["bravo", [#<Bravo>, #<Bravo>]] ]
# File lib/dirty_seed/seeder.rb, line 139 def associations_collection model.associations.map do |association| [association.name, Array.new(quantity) { association.value }] end end
Generate attributes params
Each sub-array contains the attribute name and a collection of values
@return [Array<Array>] @example
[ ["a_string", ["foo", "bar"]], [an_integer, [1, 2]] ]
# File lib/dirty_seed/seeder.rb, line 124 def attributes_collection model.attributes.map do |attribute| Faker::UniqueGenerator.clear [attribute.name, Array.new(quantity) { attribute.value }] end end
Creates records @return [void] @note To take advantage of the faker uniqueness system, all params needs to be defined
Then all records can be created with these params In other words: do not justcreate record one after the other
# File lib/dirty_seed/seeder.rb, line 43 def create_records self.successive_errors = 0 params_collection.each do |params| break if exceeded_successive_errors? record = initialize_record(params) record && save(record) end end
Is the successive errors maximum reached? @return [Boolean] @note The purpose is to stop trying to seed if to many errors happen
# File lib/dirty_seed/seeder.rb, line 56 def exceeded_successive_errors? return false if successive_errors < 3 logger.abort true end
Initialize a record @param params [Hash] params to pass to new @return [Object] instance of model
# File lib/dirty_seed/seeder.rb, line 66 def initialize_record(params) model.new(params) # rescue from errors on initialize rescue StandardError => e add_standard_error(e) end
Returns the logger @return [DirtySeed::Logger]
# File lib/dirty_seed/seeder.rb, line 34 def logger DirtySeed::Logger.instance end
Generate records params @return [Array<Hash>] where Hash is params for one record @note If model has no attributes and no associations, return empty hashes
# File lib/dirty_seed/seeder.rb, line 110 def params_collection data = Hash[attributes_collection + associations_collection] data = data.values.transpose.map { |vs| data.keys.zip(vs).to_h } data.any? ? data : Array.new(quantity, {}) end
Tries to save record in database
Populates records and errors and log message
@return [void]
# File lib/dirty_seed/seeder.rb, line 76 def save(record) if record.save records << record self.successive_errors = 0 logger.success else add_record_errors(record) end # rescue from errors on save rescue StandardError => e add_standard_error(e) end