Extending controllers of a Rails 3 Engine in the main app

后端 未结 7 910
执念已碎
执念已碎 2020-11-30 02:52

I am using a Rails engine as a gem in my app. The engine has PostsController with a number of methods and I would like to extend the controller logic in my main

相关标签:
7条回答
  • 2020-11-30 03:29

    By design, classes in a Rails::Engine are supposed to be scoped to the engine. That way they don't introduce strange bugs by accidentally stomping all over code loaded in the main app or by other engines. Monkeypatching ActiveSupport::Dependencies to mix engines across-the-board is a really bad workaround.

    Just use a Rails::Railtie, instead. They have all the same functionality, but aren't scoped the same way as an engine. You have access to the entire rails app stack (including engines). It's a more surgical approach.

    module MyModule
    
      module SomeModelExtensions
        # Called when this module is included on the given class.
        def self.included(base)
          base.send(:include, InstanceMethods)
          base.extend(ClassMethods)
        end
    
        module ClassMethods
          def some_new_class_method
            # do stuff...
          end
        end
    
        module InstanceMethods
          def some_new_instance_method
            # do stuff...
          end
        end
    
      end
    
      module SomeControllerExtensions
        def self.included(base)
          base.send(:include, InstanceMethods)
          base.alias_method_chain :new, :my_module
        end
    
        module InstanceMethods
          # override the 'new' method
          def new_with_my_module
            # do stuff
          end
        end
      end
    
      class Railtie < ::Rails::Railtie
    
        # The block you pass to this method will run for every request in
        # development mode, but only once in production.
        config.to_prepare do
          SomeModel.send(:include, MyModule::SomeModelExtensions)
          SomeController.send(:include, MyModule::SomeControllerExtensions)
        end
    
      end
    
    end
    

    As far as file layout, railties look exactly like engines.

    Further reading: Extending Rails 3 with Railties

    And if you're still confused, take a look at this git project which has a full implementation: https://github.com/jamezilla/bcms_pubcookie

    0 讨论(0)
提交回复
热议问题