module SeedHelper::BulkCreate
Public Instance Methods
bulk_create(klass, identifiable_attributes={}, output_text=nil, &creation_block)
click to toggle source
Allow an easy means of using something like FactoryGirl.create_list to seed a number of records.
If any instances of resource_class with matching identifiable_attributes exists, present a resource_already_exists message.
Otherwise, run the provided creation_block.
@param [Class] resource_class: The class to create the resource in. EG: User @param [Hash] identifiable_attributes: A hash of attributes and values that can be used to
identify the given resource. EG: {email: "jordan@example.com"}
@params [String] output_text: The text to output in the results @params [Proc] creation_block: A block that will create some objects
# File lib/seed_helper/bulk_create.rb, line 16 def bulk_create(klass, identifiable_attributes={}, output_text=nil, &creation_block) message_identifier = output_text || bulk_create_message_identifier(klass, identifiable_attributes) if klass.where(identifiable_attributes).exists? resource_already_exists(message_identifier) else begin creation_block.call(identifiable_attributes) success("Created #{message_identifier}") rescue error("Failed to create #{message_identifier}: #{$!}") end end end
Private Instance Methods
bulk_create_message_identifier(klass, identifiable_attributes)
click to toggle source
# File lib/seed_helper/bulk_create.rb, line 33 def bulk_create_message_identifier(klass, identifiable_attributes) klass_plural = klass.name.pluralize if identifiable_attributes.any? "#{klass_plural} (#{identifiable_attributes})" else klass_plural end end