Automatically include a Gem in a controller
I am currently working a gem that has some metlhods that I w开发者_JAVA技巧ant to include into my controller classes, but I would like it to automatically include itself upon installation. So, for example, include MyGem
, will not be required.
I tried using a railtie, but I am unsure if that is the right way since it doesn't work as far as I can tell. It looks similar to:
require 'my_gem'
module MyGem
class Railtie < Rails::Railtie
initializer "my_gem.method_items" do
ActionController::Base.send :include, MethodItems
end
end
end
Any help would be great
module MyGem
module InstanceMethods
def foo
"bar"
end
end
end
module MyGem
class Engine < Rails::Engine
initializer "testing" do |app|
ActionController::Base.send :include, MyGem::InstanceMethods
end
end
end
Then in console:
ActionController::Base.instance_methods.grep(/foo/)
=> ["foo"]
精彩评论