Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

Add methods to a Rails engine model from a Rails plugin

I'm writing a Rails plugin to extend a Rails engine. Namely MyPlugin has MyEngine as a dependency.

On my Rails engine I have a MyEngine::Foo model.

I'd like to add new methods to this model so I created a file in my plugin app/models/my_engine/foo.rb which has the following code:

module MyEngine
  class Foo

        def sayhi
            puts "hi"
        end

  end
end

If I enter the Rails console on the plugin dummy application I can find MyEngine::Foo, but runnning MyEngine::Foo.new.sayhi returns

NoMethodError: undefined method `sayhi'

Why MyPlugin cannot see the updates to MyEngine::Foo model? Where am I wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, found out. To make MyPlugin aware and able to modify MyEngine models the engine must be required on the plugin engine.rb like so:

require "MyEngine"

module MyPlugin
  class Engine < ::Rails::Engine
    isolate_namespace MyPlugin

    # You can also inherit the ApplicationController from MyEngine
    config.parent_controller = 'MyEngine::ApplicationController'
  end
end

In order to extend MyEngine::Foo model I then had to create a file lib/my_engine/foo_extension.rb:

require 'active_support/concern'

module FooExtension
    extend ActiveSupport::Concern

    def sayhi
        puts "Hi!"
    end

    class_methods do 
        def sayhello
            puts "Hello!"
        end
    end
end

::MyEngine::Foo(:include, FooExtension)

And require it in config/initializers/my_engine_extensions.rb

require 'my_engine/foo_extension' 

Now from MyPlugin I can:

 MyEngine::Foo.new.sayhi
 => "Hi!"
 MyEngine::Foo.sayhello
 => "Hello!"

See ActiveSupport Concern documentation for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...