Sha256: cb07a33b10137bcbfbbc362ee83cce76f6b094c62f317f2fbfd169d4f34d3515

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module ActiveElement
  module Components
    module Util
      # Translates various parameters using `I18n` library, allows users to specify labels,
      # descriptions, placeholders, etc. for various components in locales files.
      class I18n
        def self.class_name(val, plural: false)
          base = val&.to_s&.underscore&.tr('/', '_')
          plural ? base&.pluralize : base
        end

        def initialize(component)
          @component = component
        end

        def label(field)
          return titleize(field) unless model?

          key = "admin.models.#{model_key}.fields.#{field}.label"
          ::I18n.t(key, default: titleize(field))
        end

        def description(field)
          return nil unless model?

          key = "admin.models.#{model_key}.fields.#{field}.description"
          ::I18n.t(key, default: nil)
        end

        def placeholder(field)
          return nil unless model?

          key = "admin.models.#{model_key}.fields.#{field}.placeholder"
          ::I18n.t(key, default: nil)
        end

        def format(field)
          return nil unless model?

          key = "admin.models.#{model_key}.fields.#{field}.format"
          ::I18n.t(key, default: nil)
        end

        private

        attr_reader :component

        def model_key
          @model_key ||= component.model.name.underscore.pluralize
        end

        def model?
          return false if component.model.nil?

          component.model.is_a?(ActiveModel::Naming)
        end

        def titleize(field)
          field.to_s.titleize(keep_id_suffix: true).gsub(/ Id$/, ' ID')
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
active_element-0.0.23 lib/active_element/components/util/i18n.rb
active_element-0.0.22 lib/active_element/components/util/i18n.rb
active_element-0.0.21 lib/active_element/components/util/i18n.rb
active_element-0.0.19 lib/active_element/components/util/i18n.rb
active_element-0.0.18 lib/active_element/components/util/i18n.rb
active_element-0.0.17 lib/active_element/components/util/i18n.rb
active_element-0.0.16 lib/active_element/components/util/i18n.rb
active_element-0.0.15 lib/active_element/components/util/i18n.rb
active_element-0.0.14 lib/active_element/components/util/i18n.rb
active_element-0.0.13 lib/active_element/components/util/i18n.rb
active_element-0.0.12 lib/active_element/components/util/i18n.rb
active_element-0.0.11 lib/active_element/components/util/i18n.rb