Created
March 29, 2014 13:22
-
-
Save christiantakle/9854444 to your computer and use it in GitHub Desktop.
Feedback on http://t.co/sFZYBp08bV - Sorry for being such an advocate for tap :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call(form_object) | |
form_object.validate! | |
if User.where(email: form_object.email).exists? | |
form_object.errros.add(:email, 'email already taken') | |
end | |
# Forgot to access the company_name attribute on the form_object | |
company = Company.create!(name: form_object.company_name) | |
User.create!(name: form_object.name, email: form_object.email, company: company) | |
form_object | |
rescue form_object.class::ValidationError | |
form_object # we have errors here | |
rescue ActiveRecord::RecordInvalid => e | |
# add additional errors to form object | |
end | |
#-- VS ---------------------------------------------------------------- | |
def call(form_object) | |
form_object.tap do |this| | |
this.validate! | |
if User.where(email: this.email).exists? | |
this.errros.add(:email, 'email already taken') | |
end | |
User.create!({ | |
name: this.name, | |
email: this.email, | |
company: Company.create!({ | |
name: this.company_name | |
}) | |
}) | |
end | |
rescue form_object.class::ValidationError | |
form_object # we have errors here | |
rescue ActiveRecord::RecordInvalid => e | |
# add additional errors to form object | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment