Sha256: 774f12595b015f8774d16980b076aad6efa0049e6cdf377c18ffc17f30f6c0fe

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# coding: UTF-8

module Edgarj
  module CommonHelper
    # Edgarj standard datetime format
    def datetime_fmt(dt)
      if dt.blank? then
        ''
      else
        I18n.l(dt, format: I18n.t('edgarj.time.format'))
      end
    end

    # Edgarj standard date format
    def date_fmt(dt)
      if dt == nil then
        ''
      else
        dt.strftime(I18n.t('date.formats.default'))
      end
    end

    # get enum Module.
    #
    # When Col(camelized argument col name) module exists, the Col is
    # assumed enum definition.
    def get_enum(model, col)
      col_name  = col.name
      if model.const_defined?(col_name.camelize, false)
        enum = model.const_get(col_name.camelize)
        enum.is_a?(Module) ? enum : nil
      else
        nil
      end
    end

    # column label with the following fallback order:
    # 1. t('view.CONTROLLER.label.MODEL.COLUMN') if exists.
    # 1. model.human_attribute_name(col.name)
    #
    # @param col_or_sym [Column, String, or Symbol]
    def column_label(col_or_sym)
      col_name =
          case col_or_sym
          when String, Symbol
            col_or_sym
          else
            col_or_sym.name
          end
      @controller_model ||= controller.send(:model)
      I18n.t(col_name,
          scope:    "view.#{controller_path}.#{@controller_model.name.underscore}",
          default:  @controller_model.human_attribute_name(col_name))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edgarj-0.01.28 app/helpers/edgarj/common_helper.rb