1
votes

Is there a way I can take my User MyAccounts::ApplicationController and include it into another Engine as if it's in the MyOtherEngine::ActionController. I was able to load Helpers from other engines but like to keep things in ApplicationController for this project.

I'm also open to other ways but the Accounts engine uses the def and would like other engines to use it without pasting and repeating definition, also consider the loading helpers are repeating in both engines.

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
Introduce core engine with common logic included. Core engine will be loaded when one of the others engine get loaded. - Fabio
Thanks @Fabio your comment pushed me in the right direction. - bradpotts

1 Answers

0
votes

There might be other ways, but this works for anybody building Rails Engines and wants some sort of solution. Based on Fabio comment I created a helper engine (example MyCoreHelperEngine) add it as a dependency to my other engines, put the logic in helper files. Views were alright but in the controllers, you have to include the engine helper files.

 module Phcpress
   class ApplicationController < ActionController::Base

     include MyCoreHelperEngine::MyHelperFile

   end
 end