Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Created March 29, 2014 13:22
Show Gist options
  • Save christiantakle/9854444 to your computer and use it in GitHub Desktop.
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 :)
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