Ruby on Rails application controller

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












My application controller keeps growing with small helper methods that I use in both my controller and views, is it okay? or what do you recommend doing?



class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_admin?, :is_representative?, :is_shipper?,
:is_agent?, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,
:is_pricing_representative?, :is_fcl_exw_quotation_confirmed?



# Roles detection helpers
def is_admin?
current_role == 'admin' ? true : false
end

def is_representative?
current_role == 'representative' ? true : false
end

def is_shipper?
current_role == 'shipper' ? true : false
end

def is_agent?
current_role == 'agent' ? true : false
end

def is_pricing_representative?
current_role == 'pricing_representative' ? true : false
end



# Devise helpers
def resource_name
:user
end

def resource
@resource ||= User.new
end

def resource_class
User
end

def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end


# Operation helpers
def is_operation_completed?(operation_id)
GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
end

# FCL-EXW helpers
def is_fcl_exw_info_stage_completed?(operation_id)
FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
end

def is_fcl_exw?(operation_id)
Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
end

def is_fcl_exw_info_requested?(operation_id)
Operation.find(operation_id).fcl_exw_info_requested
end

def is_fcl_exw_info_confirmed?(operation_id)
Operation.find(operation_id).fcl_exw_info_confirmed
end

def is_fcl_exw_quotation_confirmed?(operation_id)
Operation.find(operation_id).fcl_exw_quotation_confirmed
end

private
def current_role
current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
end

def require_new_operation_permission
check_permission('representative', 'agent')
end

def check_permission(*roles)
unless roles.include? current_role
flash[:alert] = "Access denied"
redirect_back(fallback_location: authenticated_root_path)
end
end


end






share|improve this question



























    up vote
    0
    down vote

    favorite












    My application controller keeps growing with small helper methods that I use in both my controller and views, is it okay? or what do you recommend doing?



    class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception

    helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_admin?, :is_representative?, :is_shipper?,
    :is_agent?, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,
    :is_pricing_representative?, :is_fcl_exw_quotation_confirmed?



    # Roles detection helpers
    def is_admin?
    current_role == 'admin' ? true : false
    end

    def is_representative?
    current_role == 'representative' ? true : false
    end

    def is_shipper?
    current_role == 'shipper' ? true : false
    end

    def is_agent?
    current_role == 'agent' ? true : false
    end

    def is_pricing_representative?
    current_role == 'pricing_representative' ? true : false
    end



    # Devise helpers
    def resource_name
    :user
    end

    def resource
    @resource ||= User.new
    end

    def resource_class
    User
    end

    def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
    end


    # Operation helpers
    def is_operation_completed?(operation_id)
    GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
    end

    # FCL-EXW helpers
    def is_fcl_exw_info_stage_completed?(operation_id)
    FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
    end

    def is_fcl_exw?(operation_id)
    Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
    end

    def is_fcl_exw_info_requested?(operation_id)
    Operation.find(operation_id).fcl_exw_info_requested
    end

    def is_fcl_exw_info_confirmed?(operation_id)
    Operation.find(operation_id).fcl_exw_info_confirmed
    end

    def is_fcl_exw_quotation_confirmed?(operation_id)
    Operation.find(operation_id).fcl_exw_quotation_confirmed
    end

    private
    def current_role
    current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
    end

    def require_new_operation_permission
    check_permission('representative', 'agent')
    end

    def check_permission(*roles)
    unless roles.include? current_role
    flash[:alert] = "Access denied"
    redirect_back(fallback_location: authenticated_root_path)
    end
    end


    end






    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My application controller keeps growing with small helper methods that I use in both my controller and views, is it okay? or what do you recommend doing?



      class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception

      helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_admin?, :is_representative?, :is_shipper?,
      :is_agent?, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,
      :is_pricing_representative?, :is_fcl_exw_quotation_confirmed?



      # Roles detection helpers
      def is_admin?
      current_role == 'admin' ? true : false
      end

      def is_representative?
      current_role == 'representative' ? true : false
      end

      def is_shipper?
      current_role == 'shipper' ? true : false
      end

      def is_agent?
      current_role == 'agent' ? true : false
      end

      def is_pricing_representative?
      current_role == 'pricing_representative' ? true : false
      end



      # Devise helpers
      def resource_name
      :user
      end

      def resource
      @resource ||= User.new
      end

      def resource_class
      User
      end

      def devise_mapping
      @devise_mapping ||= Devise.mappings[:user]
      end


      # Operation helpers
      def is_operation_completed?(operation_id)
      GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
      end

      # FCL-EXW helpers
      def is_fcl_exw_info_stage_completed?(operation_id)
      FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
      end

      def is_fcl_exw?(operation_id)
      Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
      end

      def is_fcl_exw_info_requested?(operation_id)
      Operation.find(operation_id).fcl_exw_info_requested
      end

      def is_fcl_exw_info_confirmed?(operation_id)
      Operation.find(operation_id).fcl_exw_info_confirmed
      end

      def is_fcl_exw_quotation_confirmed?(operation_id)
      Operation.find(operation_id).fcl_exw_quotation_confirmed
      end

      private
      def current_role
      current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
      end

      def require_new_operation_permission
      check_permission('representative', 'agent')
      end

      def check_permission(*roles)
      unless roles.include? current_role
      flash[:alert] = "Access denied"
      redirect_back(fallback_location: authenticated_root_path)
      end
      end


      end






      share|improve this question













      My application controller keeps growing with small helper methods that I use in both my controller and views, is it okay? or what do you recommend doing?



      class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception

      helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_admin?, :is_representative?, :is_shipper?,
      :is_agent?, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,
      :is_pricing_representative?, :is_fcl_exw_quotation_confirmed?



      # Roles detection helpers
      def is_admin?
      current_role == 'admin' ? true : false
      end

      def is_representative?
      current_role == 'representative' ? true : false
      end

      def is_shipper?
      current_role == 'shipper' ? true : false
      end

      def is_agent?
      current_role == 'agent' ? true : false
      end

      def is_pricing_representative?
      current_role == 'pricing_representative' ? true : false
      end



      # Devise helpers
      def resource_name
      :user
      end

      def resource
      @resource ||= User.new
      end

      def resource_class
      User
      end

      def devise_mapping
      @devise_mapping ||= Devise.mappings[:user]
      end


      # Operation helpers
      def is_operation_completed?(operation_id)
      GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
      end

      # FCL-EXW helpers
      def is_fcl_exw_info_stage_completed?(operation_id)
      FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
      end

      def is_fcl_exw?(operation_id)
      Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
      end

      def is_fcl_exw_info_requested?(operation_id)
      Operation.find(operation_id).fcl_exw_info_requested
      end

      def is_fcl_exw_info_confirmed?(operation_id)
      Operation.find(operation_id).fcl_exw_info_confirmed
      end

      def is_fcl_exw_quotation_confirmed?(operation_id)
      Operation.find(operation_id).fcl_exw_quotation_confirmed
      end

      private
      def current_role
      current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
      end

      def require_new_operation_permission
      check_permission('representative', 'agent')
      end

      def check_permission(*roles)
      unless roles.include? current_role
      flash[:alert] = "Access denied"
      redirect_back(fallback_location: authenticated_root_path)
      end
      end


      end








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 21 at 19:50









      Sam Onela

      5,75961543




      5,75961543









      asked May 21 at 19:28









      Sebastian Delgado

      1011




      1011




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          There is no need to use the prefix is with every method if your method return type is boolean then simply use ? at the end of the method name
          I replace all the role methods name from is_role? with role? ,i.e is_admin? to admin? etc.
          instead of nil? Should use present?



          class ApplicationController < ActionController::Base
          protect_from_forgery with: :exception

          helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,:is_fcl_exw_quotation_confirmed?

          ROLES = %w[admin representative shipper agent pricing_representative]
          ROLES.each do |role|
          define_method("#role?") current_role == role
          helper_method "#role?".to_sym
          end

          # Devise helpers
          def resource_name
          :user
          end

          def resource
          @resource ||= User.new
          end

          def resource_class
          User
          end

          def devise_mapping
          @devise_mapping ||= Devise.mappings[:user]
          end


          # Operation helpers
          def is_operation_completed?(operation_id)
          GeneralCargoInfo.find_by(operation_id: operation_id).present?
          end

          # FCL-EXW helpers
          def is_fcl_exw_info_stage_completed?(operation_id)
          FclExwCargoInfo.find_by(operation_id: operation_id).present?
          end

          def is_fcl_exw?(operation_id)
          Operation.find(operation_id).modality == 'FCL - EXW'
          end

          def is_fcl_exw_info_requested?(operation_id)
          Operation.find(operation_id).fcl_exw_info_requested
          end

          def is_fcl_exw_info_confirmed?(operation_id)
          Operation.find(operation_id).fcl_exw_info_confirmed
          end

          def is_fcl_exw_quotation_confirmed?(operation_id)
          Operation.find(operation_id).fcl_exw_quotation_confirmed
          end

          private
          def current_role
          current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
          end

          def require_new_operation_permission
          check_permission('representative', 'agent')
          end

          def check_permission(*roles)
          unless roles.include? current_role
          flash[:alert] = "Access denied"
          redirect_back(fallback_location: authenticated_root_path)
          end
          end
          end


          Note: remove is with other methods as well also if you not consume this helper methods with any of the controllers then move them to view helper






          share|improve this answer




























            up vote
            0
            down vote













            You can move helper methods to ApplicationHelper and simply include it in Application controller, so your code will look like:



            ApplicationController:



            class ApplicationController < ActionController::Base
            include ApplicationHelper
            protect_from_forgery with: :exception

            private
            def current_role
            current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
            end

            def require_new_operation_permission
            check_permission('representative', 'agent')
            end

            def check_permission(*roles)
            unless roles.include? current_role
            flash[:alert] = "Access denied"
            redirect_back(fallback_location: authenticated_root_path)
            end
            end
            end


            ApplicationHelper:



            module ApplicationHelper
            # Roles detection helpers
            def is_admin?
            current_role == 'admin' ? true : false
            end

            def is_representative?
            current_role == 'representative' ? true : false
            end

            def is_shipper?
            current_role == 'shipper' ? true : false
            end

            def is_agent?
            current_role == 'agent' ? true : false
            end

            def is_pricing_representative?
            current_role == 'pricing_representative' ? true : false
            end

            # Devise helpers
            def resource_name
            :user
            end

            def resource
            @resource ||= User.new
            end

            def resource_class
            User
            end

            def devise_mapping
            @devise_mapping ||= Devise.mappings[:user]
            end


            # Operation helpers
            def is_operation_completed?(operation_id)
            GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
            end

            # FCL-EXW helpers
            def is_fcl_exw_info_stage_completed?(operation_id)
            FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
            end

            def is_fcl_exw?(operation_id)
            Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
            end

            def is_fcl_exw_info_requested?(operation_id)
            Operation.find(operation_id).fcl_exw_info_requested
            end

            def is_fcl_exw_info_confirmed?(operation_id)
            Operation.find(operation_id).fcl_exw_info_confirmed
            end

            def is_fcl_exw_quotation_confirmed?(operation_id)
            Operation.find(operation_id).fcl_exw_quotation_confirmed
            end
            end





            share|improve this answer





















            • You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
              – Dannnno
              May 23 at 13:47










            • The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
              – Sebastian Delgado
              May 24 at 14:22










            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194893%2fruby-on-rails-application-controller%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            There is no need to use the prefix is with every method if your method return type is boolean then simply use ? at the end of the method name
            I replace all the role methods name from is_role? with role? ,i.e is_admin? to admin? etc.
            instead of nil? Should use present?



            class ApplicationController < ActionController::Base
            protect_from_forgery with: :exception

            helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,:is_fcl_exw_quotation_confirmed?

            ROLES = %w[admin representative shipper agent pricing_representative]
            ROLES.each do |role|
            define_method("#role?") current_role == role
            helper_method "#role?".to_sym
            end

            # Devise helpers
            def resource_name
            :user
            end

            def resource
            @resource ||= User.new
            end

            def resource_class
            User
            end

            def devise_mapping
            @devise_mapping ||= Devise.mappings[:user]
            end


            # Operation helpers
            def is_operation_completed?(operation_id)
            GeneralCargoInfo.find_by(operation_id: operation_id).present?
            end

            # FCL-EXW helpers
            def is_fcl_exw_info_stage_completed?(operation_id)
            FclExwCargoInfo.find_by(operation_id: operation_id).present?
            end

            def is_fcl_exw?(operation_id)
            Operation.find(operation_id).modality == 'FCL - EXW'
            end

            def is_fcl_exw_info_requested?(operation_id)
            Operation.find(operation_id).fcl_exw_info_requested
            end

            def is_fcl_exw_info_confirmed?(operation_id)
            Operation.find(operation_id).fcl_exw_info_confirmed
            end

            def is_fcl_exw_quotation_confirmed?(operation_id)
            Operation.find(operation_id).fcl_exw_quotation_confirmed
            end

            private
            def current_role
            current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
            end

            def require_new_operation_permission
            check_permission('representative', 'agent')
            end

            def check_permission(*roles)
            unless roles.include? current_role
            flash[:alert] = "Access denied"
            redirect_back(fallback_location: authenticated_root_path)
            end
            end
            end


            Note: remove is with other methods as well also if you not consume this helper methods with any of the controllers then move them to view helper






            share|improve this answer

























              up vote
              1
              down vote













              There is no need to use the prefix is with every method if your method return type is boolean then simply use ? at the end of the method name
              I replace all the role methods name from is_role? with role? ,i.e is_admin? to admin? etc.
              instead of nil? Should use present?



              class ApplicationController < ActionController::Base
              protect_from_forgery with: :exception

              helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,:is_fcl_exw_quotation_confirmed?

              ROLES = %w[admin representative shipper agent pricing_representative]
              ROLES.each do |role|
              define_method("#role?") current_role == role
              helper_method "#role?".to_sym
              end

              # Devise helpers
              def resource_name
              :user
              end

              def resource
              @resource ||= User.new
              end

              def resource_class
              User
              end

              def devise_mapping
              @devise_mapping ||= Devise.mappings[:user]
              end


              # Operation helpers
              def is_operation_completed?(operation_id)
              GeneralCargoInfo.find_by(operation_id: operation_id).present?
              end

              # FCL-EXW helpers
              def is_fcl_exw_info_stage_completed?(operation_id)
              FclExwCargoInfo.find_by(operation_id: operation_id).present?
              end

              def is_fcl_exw?(operation_id)
              Operation.find(operation_id).modality == 'FCL - EXW'
              end

              def is_fcl_exw_info_requested?(operation_id)
              Operation.find(operation_id).fcl_exw_info_requested
              end

              def is_fcl_exw_info_confirmed?(operation_id)
              Operation.find(operation_id).fcl_exw_info_confirmed
              end

              def is_fcl_exw_quotation_confirmed?(operation_id)
              Operation.find(operation_id).fcl_exw_quotation_confirmed
              end

              private
              def current_role
              current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
              end

              def require_new_operation_permission
              check_permission('representative', 'agent')
              end

              def check_permission(*roles)
              unless roles.include? current_role
              flash[:alert] = "Access denied"
              redirect_back(fallback_location: authenticated_root_path)
              end
              end
              end


              Note: remove is with other methods as well also if you not consume this helper methods with any of the controllers then move them to view helper






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                There is no need to use the prefix is with every method if your method return type is boolean then simply use ? at the end of the method name
                I replace all the role methods name from is_role? with role? ,i.e is_admin? to admin? etc.
                instead of nil? Should use present?



                class ApplicationController < ActionController::Base
                protect_from_forgery with: :exception

                helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,:is_fcl_exw_quotation_confirmed?

                ROLES = %w[admin representative shipper agent pricing_representative]
                ROLES.each do |role|
                define_method("#role?") current_role == role
                helper_method "#role?".to_sym
                end

                # Devise helpers
                def resource_name
                :user
                end

                def resource
                @resource ||= User.new
                end

                def resource_class
                User
                end

                def devise_mapping
                @devise_mapping ||= Devise.mappings[:user]
                end


                # Operation helpers
                def is_operation_completed?(operation_id)
                GeneralCargoInfo.find_by(operation_id: operation_id).present?
                end

                # FCL-EXW helpers
                def is_fcl_exw_info_stage_completed?(operation_id)
                FclExwCargoInfo.find_by(operation_id: operation_id).present?
                end

                def is_fcl_exw?(operation_id)
                Operation.find(operation_id).modality == 'FCL - EXW'
                end

                def is_fcl_exw_info_requested?(operation_id)
                Operation.find(operation_id).fcl_exw_info_requested
                end

                def is_fcl_exw_info_confirmed?(operation_id)
                Operation.find(operation_id).fcl_exw_info_confirmed
                end

                def is_fcl_exw_quotation_confirmed?(operation_id)
                Operation.find(operation_id).fcl_exw_quotation_confirmed
                end

                private
                def current_role
                current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                end

                def require_new_operation_permission
                check_permission('representative', 'agent')
                end

                def check_permission(*roles)
                unless roles.include? current_role
                flash[:alert] = "Access denied"
                redirect_back(fallback_location: authenticated_root_path)
                end
                end
                end


                Note: remove is with other methods as well also if you not consume this helper methods with any of the controllers then move them to view helper






                share|improve this answer













                There is no need to use the prefix is with every method if your method return type is boolean then simply use ? at the end of the method name
                I replace all the role methods name from is_role? with role? ,i.e is_admin? to admin? etc.
                instead of nil? Should use present?



                class ApplicationController < ActionController::Base
                protect_from_forgery with: :exception

                helper_method :resource_name, :resource, :devise_mapping, :resource_class, :is_operation_completed?, :is_fcl_exw?, :is_fcl_exw_info_stage_completed?, :is_fcl_exw_info_requested?, :is_fcl_exw_info_confirmed?,:is_fcl_exw_quotation_confirmed?

                ROLES = %w[admin representative shipper agent pricing_representative]
                ROLES.each do |role|
                define_method("#role?") current_role == role
                helper_method "#role?".to_sym
                end

                # Devise helpers
                def resource_name
                :user
                end

                def resource
                @resource ||= User.new
                end

                def resource_class
                User
                end

                def devise_mapping
                @devise_mapping ||= Devise.mappings[:user]
                end


                # Operation helpers
                def is_operation_completed?(operation_id)
                GeneralCargoInfo.find_by(operation_id: operation_id).present?
                end

                # FCL-EXW helpers
                def is_fcl_exw_info_stage_completed?(operation_id)
                FclExwCargoInfo.find_by(operation_id: operation_id).present?
                end

                def is_fcl_exw?(operation_id)
                Operation.find(operation_id).modality == 'FCL - EXW'
                end

                def is_fcl_exw_info_requested?(operation_id)
                Operation.find(operation_id).fcl_exw_info_requested
                end

                def is_fcl_exw_info_confirmed?(operation_id)
                Operation.find(operation_id).fcl_exw_info_confirmed
                end

                def is_fcl_exw_quotation_confirmed?(operation_id)
                Operation.find(operation_id).fcl_exw_quotation_confirmed
                end

                private
                def current_role
                current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                end

                def require_new_operation_permission
                check_permission('representative', 'agent')
                end

                def check_permission(*roles)
                unless roles.include? current_role
                flash[:alert] = "Access denied"
                redirect_back(fallback_location: authenticated_root_path)
                end
                end
                end


                Note: remove is with other methods as well also if you not consume this helper methods with any of the controllers then move them to view helper







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 27 at 12:59









                rahul mishra

                1111




                1111






















                    up vote
                    0
                    down vote













                    You can move helper methods to ApplicationHelper and simply include it in Application controller, so your code will look like:



                    ApplicationController:



                    class ApplicationController < ActionController::Base
                    include ApplicationHelper
                    protect_from_forgery with: :exception

                    private
                    def current_role
                    current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                    end

                    def require_new_operation_permission
                    check_permission('representative', 'agent')
                    end

                    def check_permission(*roles)
                    unless roles.include? current_role
                    flash[:alert] = "Access denied"
                    redirect_back(fallback_location: authenticated_root_path)
                    end
                    end
                    end


                    ApplicationHelper:



                    module ApplicationHelper
                    # Roles detection helpers
                    def is_admin?
                    current_role == 'admin' ? true : false
                    end

                    def is_representative?
                    current_role == 'representative' ? true : false
                    end

                    def is_shipper?
                    current_role == 'shipper' ? true : false
                    end

                    def is_agent?
                    current_role == 'agent' ? true : false
                    end

                    def is_pricing_representative?
                    current_role == 'pricing_representative' ? true : false
                    end

                    # Devise helpers
                    def resource_name
                    :user
                    end

                    def resource
                    @resource ||= User.new
                    end

                    def resource_class
                    User
                    end

                    def devise_mapping
                    @devise_mapping ||= Devise.mappings[:user]
                    end


                    # Operation helpers
                    def is_operation_completed?(operation_id)
                    GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    # FCL-EXW helpers
                    def is_fcl_exw_info_stage_completed?(operation_id)
                    FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    def is_fcl_exw?(operation_id)
                    Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
                    end

                    def is_fcl_exw_info_requested?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_requested
                    end

                    def is_fcl_exw_info_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_confirmed
                    end

                    def is_fcl_exw_quotation_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_quotation_confirmed
                    end
                    end





                    share|improve this answer





















                    • You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Dannnno
                      May 23 at 13:47










                    • The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                      – Sebastian Delgado
                      May 24 at 14:22














                    up vote
                    0
                    down vote













                    You can move helper methods to ApplicationHelper and simply include it in Application controller, so your code will look like:



                    ApplicationController:



                    class ApplicationController < ActionController::Base
                    include ApplicationHelper
                    protect_from_forgery with: :exception

                    private
                    def current_role
                    current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                    end

                    def require_new_operation_permission
                    check_permission('representative', 'agent')
                    end

                    def check_permission(*roles)
                    unless roles.include? current_role
                    flash[:alert] = "Access denied"
                    redirect_back(fallback_location: authenticated_root_path)
                    end
                    end
                    end


                    ApplicationHelper:



                    module ApplicationHelper
                    # Roles detection helpers
                    def is_admin?
                    current_role == 'admin' ? true : false
                    end

                    def is_representative?
                    current_role == 'representative' ? true : false
                    end

                    def is_shipper?
                    current_role == 'shipper' ? true : false
                    end

                    def is_agent?
                    current_role == 'agent' ? true : false
                    end

                    def is_pricing_representative?
                    current_role == 'pricing_representative' ? true : false
                    end

                    # Devise helpers
                    def resource_name
                    :user
                    end

                    def resource
                    @resource ||= User.new
                    end

                    def resource_class
                    User
                    end

                    def devise_mapping
                    @devise_mapping ||= Devise.mappings[:user]
                    end


                    # Operation helpers
                    def is_operation_completed?(operation_id)
                    GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    # FCL-EXW helpers
                    def is_fcl_exw_info_stage_completed?(operation_id)
                    FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    def is_fcl_exw?(operation_id)
                    Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
                    end

                    def is_fcl_exw_info_requested?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_requested
                    end

                    def is_fcl_exw_info_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_confirmed
                    end

                    def is_fcl_exw_quotation_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_quotation_confirmed
                    end
                    end





                    share|improve this answer





















                    • You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Dannnno
                      May 23 at 13:47










                    • The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                      – Sebastian Delgado
                      May 24 at 14:22












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You can move helper methods to ApplicationHelper and simply include it in Application controller, so your code will look like:



                    ApplicationController:



                    class ApplicationController < ActionController::Base
                    include ApplicationHelper
                    protect_from_forgery with: :exception

                    private
                    def current_role
                    current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                    end

                    def require_new_operation_permission
                    check_permission('representative', 'agent')
                    end

                    def check_permission(*roles)
                    unless roles.include? current_role
                    flash[:alert] = "Access denied"
                    redirect_back(fallback_location: authenticated_root_path)
                    end
                    end
                    end


                    ApplicationHelper:



                    module ApplicationHelper
                    # Roles detection helpers
                    def is_admin?
                    current_role == 'admin' ? true : false
                    end

                    def is_representative?
                    current_role == 'representative' ? true : false
                    end

                    def is_shipper?
                    current_role == 'shipper' ? true : false
                    end

                    def is_agent?
                    current_role == 'agent' ? true : false
                    end

                    def is_pricing_representative?
                    current_role == 'pricing_representative' ? true : false
                    end

                    # Devise helpers
                    def resource_name
                    :user
                    end

                    def resource
                    @resource ||= User.new
                    end

                    def resource_class
                    User
                    end

                    def devise_mapping
                    @devise_mapping ||= Devise.mappings[:user]
                    end


                    # Operation helpers
                    def is_operation_completed?(operation_id)
                    GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    # FCL-EXW helpers
                    def is_fcl_exw_info_stage_completed?(operation_id)
                    FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    def is_fcl_exw?(operation_id)
                    Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
                    end

                    def is_fcl_exw_info_requested?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_requested
                    end

                    def is_fcl_exw_info_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_confirmed
                    end

                    def is_fcl_exw_quotation_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_quotation_confirmed
                    end
                    end





                    share|improve this answer













                    You can move helper methods to ApplicationHelper and simply include it in Application controller, so your code will look like:



                    ApplicationController:



                    class ApplicationController < ActionController::Base
                    include ApplicationHelper
                    protect_from_forgery with: :exception

                    private
                    def current_role
                    current_role = current_user.nil? ? 'Not logged in' : Role.find(current_user.role_id).name
                    end

                    def require_new_operation_permission
                    check_permission('representative', 'agent')
                    end

                    def check_permission(*roles)
                    unless roles.include? current_role
                    flash[:alert] = "Access denied"
                    redirect_back(fallback_location: authenticated_root_path)
                    end
                    end
                    end


                    ApplicationHelper:



                    module ApplicationHelper
                    # Roles detection helpers
                    def is_admin?
                    current_role == 'admin' ? true : false
                    end

                    def is_representative?
                    current_role == 'representative' ? true : false
                    end

                    def is_shipper?
                    current_role == 'shipper' ? true : false
                    end

                    def is_agent?
                    current_role == 'agent' ? true : false
                    end

                    def is_pricing_representative?
                    current_role == 'pricing_representative' ? true : false
                    end

                    # Devise helpers
                    def resource_name
                    :user
                    end

                    def resource
                    @resource ||= User.new
                    end

                    def resource_class
                    User
                    end

                    def devise_mapping
                    @devise_mapping ||= Devise.mappings[:user]
                    end


                    # Operation helpers
                    def is_operation_completed?(operation_id)
                    GeneralCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    # FCL-EXW helpers
                    def is_fcl_exw_info_stage_completed?(operation_id)
                    FclExwCargoInfo.find_by(operation_id: operation_id).nil? ? false : true
                    end

                    def is_fcl_exw?(operation_id)
                    Operation.find(operation_id).modality == 'FCL - EXW' ? true : false
                    end

                    def is_fcl_exw_info_requested?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_requested
                    end

                    def is_fcl_exw_info_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_info_confirmed
                    end

                    def is_fcl_exw_quotation_confirmed?(operation_id)
                    Operation.find(operation_id).fcl_exw_quotation_confirmed
                    end
                    end






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered May 23 at 11:47









                    fidato

                    1011




                    1011











                    • You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Dannnno
                      May 23 at 13:47










                    • The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                      – Sebastian Delgado
                      May 24 at 14:22
















                    • You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Dannnno
                      May 23 at 13:47










                    • The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                      – Sebastian Delgado
                      May 24 at 14:22















                    You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                    – Dannnno
                    May 23 at 13:47




                    You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                    – Dannnno
                    May 23 at 13:47












                    The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                    – Sebastian Delgado
                    May 24 at 14:22




                    The problem with this is that I need to access those helpers from my controllers, adding them in a helper module would make them accessible only for my views.
                    – Sebastian Delgado
                    May 24 at 14:22












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194893%2fruby-on-rails-application-controller%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation