Skip to content

Commit

Permalink
Moved #create! method from Validations to Persistence module
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Aug 5, 2014
1 parent 3300fde commit 977a489
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
12 changes: 12 additions & 0 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -36,6 +36,18 @@ def create(attributes = nil, &block)
end
end

# Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
# so an exception is raised if the record is invalid.
def create!(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create!(attr, &block) }
else
object = new(attributes, &block)
object.save!
object
end
end

# Given an attributes hash, +instantiate+ returns a new instance of
# the appropriate class. Accepts only keys as strings.
#
Expand Down
15 changes: 0 additions & 15 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -29,21 +29,6 @@ module Validations
extend ActiveSupport::Concern
include ActiveModel::Validations

module ClassMethods
# Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
# so an exception is raised if the record is invalid.
def create!(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create!(attr, &block) }
else
object = new(attributes)
yield(object) if block_given?
object.save!
object
end
end
end

# The validation process on save can be skipped by passing <tt>validate: false</tt>.
# The regular Base#save method is replaced with this when the validations
# module is mixed in, which it is by default.
Expand Down

0 comments on commit 977a489

Please sign in to comment.