Skip to content

Instantly share code, notes, and snippets.

@phinze
Created July 2, 2009 18:57
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 phinze/139642 to your computer and use it in GitHub Desktop.
Save phinze/139642 to your computer and use it in GitHub Desktop.
# ----------------------------------------------------------------
# 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