Skip to content

Instantly share code, notes, and snippets.

@cmc333333
Created February 19, 2015 16:13
Show Gist options
  • Save cmc333333/2385fbe26c7f58cdd616 to your computer and use it in GitHub Desktop.
Save cmc333333/2385fbe26c7f58cdd616 to your computer and use it in GitHub Desktop.
module Ncr
class ProposalsController < ApplicationController
before_filter :authenticate_user!
def new
@proposal_form = Ncr::ProposalForm.new
@form_url, @form_method = {action: "create"}, "post"
approver = self.suggested_approver
if approver
@proposal_form.approver_email = approver.email_address
end
end
def create
@proposal_form = Ncr::ProposalForm.new(params[:ncr_proposal])
@form_url, @form_method = {action: "create"}, "post"
@proposal_form.requester = current_user
if @proposal_form.valid?
cart = @proposal_form.create_cart
if cart.persisted?
flash[:success] = "Proposal submitted!"
redirect_to cart_path(cart)
else
flash[:error] = cart.errors.full_messages
render 'new'
end
else
flash[:error] = @proposal_form.errors.full_messages
render 'new'
end
end
def edit
cart = Cart.find(params[:id])
@proposal_form = Ncr::ProposalForm.from_cart(cart)
@form_url, @form_method = {action: "update"}, "put"
render 'new'
end
def update
@proposal_form = Ncr::ProposalForm.new(params[:ncr_proposal])
@proposal_form.requester = current_user
if @proposal_form.valid?
cart = Cart.find(params[:id])
@proposal_form.update_cart(cart)
if cart.persisted?
flash[:success] = "Proposal resubmitted!"
redirect_to cart_path(cart)
else
flash[:error] = cart.errors.full_messages
render 'new'
end
else
flash[:error] = @proposal_form.errors.full_messages
render 'new'
end
end
protected
def last_cart
current_user.last_requested_cart
end
def last_approvers
last_cart.try(:approvers)
end
def suggested_approver
last_approvers.try(:first)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment