lib/seed_helper.rb in seed_helper-1.9.0 vs lib/seed_helper.rb in seed_helper-1.11.0
- old
+ new
@@ -1,11 +1,13 @@
require 'colorize'
require 'seed_helper/version'
+require 'seed_helper/bulk_create'
require 'seed_helper/output_formatter'
require 'seed_helper/rake_helper'
class SeedHelper
+ extend SeedHelper::BulkCreate
extend SeedHelper::OutputFormatter
extend SeedHelper::RakeHelper
# If a resource_class with matching identifiable_attributes exists, return that user and present a
# resource_already_exists message.
@@ -21,11 +23,11 @@
def self.find_or_create_resource(resource_class, identifiable_attributes, additional_attributes={}, &constructor)
if resource = find_resource(resource_class, identifiable_attributes)
resource_already_exists(resource)
else
if constructor.present?
- resource = constructor.call
+ resource = constructor.call(identifiable_attributes.merge(additional_attributes))
else
resource = resource_class.new(identifiable_attributes.merge(additional_attributes))
end
create_resource(resource)
end
@@ -39,24 +41,9 @@
else
message = "#{resource} failed to create. Errors: #{resource.errors.full_messages}"
error(message)
end
did_save
- end
-
- def self.bulk_create(klass, &creation_block)
- klass_plural = klass.name.pluralize
-
- if klass.any?
- resource_already_exists(klass_plural)
- else
- begin
- creation_block.call
- success("Created #{klass_plural}")
- rescue
- error("Failed to create #{klass_plural}: #{$!}")
- end
- end
end
private
def self.find_resource(resource_class, attributes)