Skip to content

Instantly share code, notes, and snippets.

@mati0090
Created February 27, 2012 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mati0090/1923694 to your computer and use it in GitHub Desktop.
Save mati0090/1923694 to your computer and use it in GitHub Desktop.
{ send_for_approval: "sent for approval",
deactivate: "deactivated",
reactivate: "reactivated",
}.each do |status, desc|
define_method status do
if @offer.send(status)
redirect_to @offer, :notice => "Offer was #{desc}."
else
render :action => "edit"
end
end
end
@staszek
Copy link

staszek commented Feb 27, 2012

def send_for_approval
  if @offer.send_for_approval
    redirect_to @offer, :notice => "Offer was sent for approval."
  else
    render :action => "edit"
  end
end

def deactivate
  if @offer.deactivate
    redirect_to @offer, :notice => "Offer was deactivated."
  else
    render :action => "edit"
  end
end

def reactivate
  if @offer.reactivate
    redirect_to @offer, :notice => "Offer was reactivated."
  else
    render :action => "edit"
  end
end

@staszek
Copy link

staszek commented Feb 27, 2012

def send_for_approval
  change_state(@offer.send_for_approval, 'sent for approval')
end

def deactivate
  change_state(@offer.deactivate, 'deactivated')
end

def reactivate
  change_state(@offer.reactivate, 'reactivated')
end

private

def change_state(result, snippet)
  if result
    redirect_to @offer, :notice => "Offer was #{snippet}"
  else
    render :action => "edit"
  end
end 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment