Sha256: ebabec5054e8c63ae6a30acf825bba74256aef81578498100305bd2b3e02dfa4

Contents?: true

Size: 1.99 KB

Versions: 31

Compression:

Stored size: 1.99 KB

Contents

module Edgarj
  # Mixin for All controllers (not only Edgarj::ControllerMixin include class)
  module ControllerMixinForApp
    def self.included(klass)
      klass.helper_method :v, :url_prefix
    end

  private
    # Transform the controller name into a more humane format, using I18n.
    # I18n fallbacks work as follows (LeadsController as example):
    #
    # 1. t('activerecord.models.lead')
    # 1. t('controller.leads')
    # 1. t('lead')
    # 1. 'Lead'
    def human_name
      @human_name ||= begin
        str = controller_path
        single = str.singularize
        I18n.t(         "activerecord.models.#{model.to_s.underscore}",
          default:      I18n.t("controller.#{str}",
            default:    I18n.t(single,
              default:  single.camelize)))
      end
    end

    # application-wide rescue to log error info.
    def app_rescue
      logger.error(sprintf("error %s(%s) at %s#%s:\n%s",
         $!.class.to_s,
         $!.to_s,
         self.class.name,
         self.action_name,
         $@.join("\n")))
    end

    # set @sssn if not exist
    def intern_sssn
      @sssn ||=
          if (sid = request.session_options[:id])
            Edgarj::Sssn.find_by_session_id(sid) ||
                Edgarj::Sssn.new(session_id: sid)
          else
            Edgarj::Sssn.new
          end
    end

    # convenient t() for view.  v(KEY) fallback works as follows:
    #
    # 1. t('view.CONTROLLER.KEY') if exists.  Where, CONTROLLER
    #    is controller name.
    # 1. t('edgarj.view.CONTROLLER.KEY') if exists.
    # 1. t('edgarj.default.KEY') if exists.
    # 1. Key
    #
    def v(key)
      t(key,
          scope:    "view.#{controller_path}",
          default:  I18n.t(key,
              scope:    "edgarj.view.#{controller_path}",
              default:  I18n.t(key,
                  scope:    'edgarj.default',
                  default:  key.camelize)))
    end

    # TODO: dirty solution to support url namespace
    def url_prefix
      @url_prefix ||= '/'
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
edgarj-4.06.10 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.09 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.07 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.05 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.04 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.03 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.02 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.01 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.06.00 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.05.04 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.05.03 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.05.02 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.05.01 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.05.00 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.04.01 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.04.00 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.03.00 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.02.00 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.01.01 app/controllers/edgarj/controller_mixin_for_app.rb
edgarj-4.01.00 app/controllers/edgarj/controller_mixin_for_app.rb