Sha256: dbf6b5dc7561327e26eea8f3b62dab49b32cdb8bb3be8dc4278b2efc4ca66bc2

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 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('_', '-')&.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

9 entries across 9 versions & 1 rubygems

Version Path
active_element-0.0.10 lib/active_element/components/util/i18n.rb
active_element-0.0.9 lib/active_element/components/util/i18n.rb
active_element-0.0.8 lib/active_element/components/util/i18n.rb
active_element-0.0.7 lib/active_element/components/util/i18n.rb
active_element-0.0.6 lib/active_element/components/util/i18n.rb
active_element-0.0.5 lib/active_element/components/util/i18n.rb
active_element-0.0.4 lib/active_element/components/util/i18n.rb
active_element-0.0.3 lib/active_element/components/util/i18n.rb
active_element-0.0.2 lib/active_element/components/util/i18n.rb