lib/seed_helper.rb in seed_helper-1.0.1 vs lib/seed_helper.rb in seed_helper-1.0.2
- old
+ new
@@ -6,11 +6,11 @@
module SeedHelper
include SeedHelper::OutputFormatter
include SeedHelper::RakeHelper
def create_resource(resource_class, attributes)
- if resource = resource_class.find_by(attributes)
+ if resource = find_resource(resource_class, attributes)
resource_already_exists(resource)
else
resource = resource_class.new(attributes)
if resource.save
message = "#{resource} successfully created"
@@ -19,8 +19,20 @@
message = "#{resource} failed to create. Errors: #{resource.errors.full_messages}"
error(message)
end
end
return resource
+ end
+
+private
+
+ def find_resource(resource_class, attributes)
+ # Rails 4
+ if resource_class.respond_to?(:find_by)
+ return resource_class.find_by(attributes)
+ # Rails 3
+ else
+ return resource_class.where(attributes).first
+ end
end
end