Created
August 29, 2009 23:25
-
-
Save darkhelmet/177740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WidgetController < ApplicationController | |
include RespondGlue | |
def index | |
@widgets = some_magic | |
respond_glue(:html) { render(:template => 'widgets/index') } | |
end | |
def show | |
@widget = get_widget | |
respond_glue(:html) { render(:template => 'widgets/show') } | |
end | |
end | |
class FancyWidgetController < WidgetController | |
def index | |
super | |
respond_glue(:html) { render } # will render the default index action template for fancy_widgets | |
end | |
glue_for(:index) | |
superglue_for(:show) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RespondGlue | |
def self.included(klass) | |
klass.send(:extend, ClassMethods) | |
end | |
module ClassMethods | |
def glue_for(*actions) | |
blk = block_given? ? Proc.new : nil | |
actions.each do |a| | |
if blk.nil? | |
alias_method("#{a}_original", a) | |
define_method(a) do | |
eval("#{a}_original") | |
glue | |
end | |
else | |
define_method(a) do | |
super | |
instance_eval(&blk) | |
glue | |
end | |
end | |
end | |
end | |
def superglue_for(*actions) | |
actions.each do |a| | |
glue_for(a) { } | |
end | |
end | |
end | |
def respond_glue(format = nil) | |
@glue ||= { } | |
return @glue if format.nil? | |
block_given? ? @glue[format] = Proc.new : @glue[format] | |
end | |
def glue | |
unless respond_glue.empty? | |
respond_to do |format| | |
respond_glue.each do |k,v| | |
format.send(k,&v) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment