Sha256: 9a6d243c1cf84b4a79918753ef3ac2fbf7c53331670a5c49d0f3cd7b1e85cf64

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

module ActiveModel
  module Translation
    # CarDealer.sales_count -> s_('CarDealer|Sales count') -> 'Sales count' if no translation was found
    def human_attribute_name(attribute, *args)
      s_(gettext_translation_for_attribute_name(attribute))
    end

    def gettext_translation_for_attribute_name(attribute)
      attribute = attribute.to_s
      if attribute.end_with?('_id')
        humanize_class_name(attribute)
      else
        "#{inheritance_tree_root(self)}|#{attribute.split('.').map! {|a| a.humanize }.join('|')}"
      end
    end

    def inheritance_tree_root(aclass)
      return aclass unless aclass.respond_to?(:base_class)
      base = aclass.base_class
      if base.superclass.abstract_class?
        if defined?(::ApplicationRecord) && base.superclass == ApplicationRecord
          base
        else
          base.superclass
        end
      else
        base
      end
    end

    def humanize_class_name(name=nil)
      name ||= self.to_s
      name.underscore.humanize
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gettext_i18n_rails-1.13.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.12.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.11.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.10.1 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.10.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.9.0 lib/gettext_i18n_rails/active_model/translation.rb