Skip to content

Instantly share code, notes, and snippets.

@LTe
Forked from mati0090/gist:1923694
Created February 27, 2012 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LTe/1923818 to your computer and use it in GitHub Desktop.
Save LTe/1923818 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
class MyResponder < ActionController::Responder
# Example usage
# def controller_method
# respond_with @offer
# end
def to_html
if available_methods.keys.include? controller.action
if resources.first.send(controller.action)
redirect_to resources.first, :notice => "Offer was #{available_methods[controller.action]}."
else
render :action => "edit"
end
return # just return, don't execute super
end
super
end
# Example usage
# def controller_method
# respond_with @offer, :change_state => true
# end
def to_html # another impelemnetation
if options[:change_state]
if resources.first.send(action.controller)
redirect_to resources.first, :notice => "Offer was #{available_methods[controller.action]}."
else
render :action => "edit"
end
else
super
end
end
protected
def available_methods
{
send_for_approval: "sent for approval",
deactivate: "deactivated" ,
reactivate: "reactivated" ,
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment