Created
July 2, 2009 18:57
-
-
Save phinze/139642 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# ---------------------------------------------------------------- | |
# Injecting reloadable code into a rails core class from a plugin | |
# ---------------------------------------------------------------- | |
# lib/action_view_extensions/foo_form_helper.rb | |
module ActionViewExtensions::FooFormHelper | |
def foo | |
"foo!" | |
end | |
def bar | |
"bar!" | |
end | |
end | |
# init.rb | |
# This injects code into ActionView::Base which allows my extensions to be | |
# reloaded even though ActionView::Base isn't. | |
ext = ActionViewExtensions::FooFormHelper | |
ActionView::Base.class_eval %{ | |
def method_missing_with_foo_extensions(method, *args, &block) | |
unless extended_by.include?(#{ext}) # skip if already extended | |
extend(#{ext}) # extend with module | |
if respond_to?(method) # check again | |
return send(method, *args, &block) # A.O.K, call it and go | |
end | |
end | |
# fall back otherwise | |
method_missing_without_foo_extensions(method, *args, &block) | |
end | |
alias_method_chain :method_missing, :foo_extensions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment