Skip to content

Instantly share code, notes, and snippets.

@vinioliveira
Created July 8, 2013 13:42
Show Gist options
  • Save vinioliveira/5948861 to your computer and use it in GitHub Desktop.
Save vinioliveira/5948861 to your computer and use it in GitHub Desktop.
Forms Service
class SignupSuccessForm
include ActiveModel::Model
attr_accessor :validation_token
attr_accessor :transaction_id
attr_reader :order
validates_presence_of :validation_token, :transaction_id
validate :order_must_exist
def process
if valid?
Order.transaction do
# Create project here instead
klass = order.product_type == "ecommerce" ? EcommerceProject : SalesforceProject
project = klass.create!(manager: order.user,
profile: order.profile,
name: order.profile.url,
expires_at: 1.year.from_now + 1.days)
order.update_attributes! transaction_id: transaction_id,
status: "submitted",
project: project
Signup.where(user_id: order.user).delete_all
Postman.welcome(order).deliver
end
end
end
private
def order_must_exist
if validation_token && transaction_id
@order = Order.find_by_validation_token(validation_token)
if @order.nil? || @order.transaction_id.present?
errors.add :validation_token, "é invalido"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment