将ApplicationController从一个Rails引擎加载到另一个Rails引擎

时间:2019-04-07 05:14:17

标签: ruby-on-rails ruby controller rubygems

有没有一种方法可以将我的用户MyAccounts :: ApplicationController包含在另一个引擎中,就像在MyOtherEngine :: ActionController中一样。我能够从其他引擎加载Helpers,但希望将其保留在此项目的ApplicationController中。

我也对其他方式持开放态度,但Accounts引擎使用def,并且希望其他引擎在不粘贴和重复定义的情况下使用它,还要考虑加载助手在两个引擎中都在重复。

module Phcaccounts
      class ApplicationController < ActionController::Base

        # Devise Filter
        before_action :phc_devise_permitted_parameters, if: :devise_controller?

        # Filter and Security
        protect_from_forgery with: :exception

        protected

        # Whitelist Additional Fields
        def phc_devise_permitted_parameters
          added_attrs = [:username, :firstname, :lastname, :email, :password, :password_confirmation, :remember_me]
          devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
          devise_parameter_sanitizer.permit :account_update, keys: added_attrs
        end

        # Load Helpers & Load Helpers to Mainapp
        helper Phctitleseo::Engine.helpers
        helper Phcnotifi::Engine.helpers

        # Redirect User to Welcome Screen After Signin
        def after_sign_in_path_for(resource)
          welcome_status_page_path
        end

        # Auth Filter for Admin
        def phcaccounts_authentication_filter!
          if admin_signed_in?
            true
          else
            authenticate_user!
          end
        end

      end
    end
    require "???/???"

    module Phcpress
      class ApplicationController < ActionController::Base

        # Security
        ?????include Phcaccounts::ApplicationController?????
        protect_from_forgery with: :exception

        # Load Helpers
        helper Phctitleseo::Engine.helpers
        helper Phcnotifi::Engine.helpers

      end
    end

1 个答案:

答案 0 :(得分:0)

也许还有其他方法,但这对任何构建Rails Engines并想要某种解决方案的人都适用。根据Fabio的评论,我创建了一个辅助引擎(例如MyCoreHelperEngine),将其添加为对其他引擎的依赖关系,并将逻辑放入辅助文件中。视图还可以,但是在控制器中,您必须包括引擎帮助程序文件。

 module Phcpress
   class ApplicationController < ActionController::Base

     include MyCoreHelperEngine::MyHelperFile

   end
 end