Sha256: d50e490f91e4d088e49c2d0295afaf3796cd2bf193e2a6637a7554428a5a3db6

Contents?: true

Size: 1 KB

Versions: 8

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.ends_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

8 entries across 8 versions & 1 rubygems

Version Path
gettext_i18n_rails-1.8.1 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.8.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.7.2 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.7.1 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.7.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.6.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.5.0 lib/gettext_i18n_rails/active_model/translation.rb
gettext_i18n_rails-1.4.0 lib/gettext_i18n_rails/active_model/translation.rb