Sha256: 7973fca05827f94f33da5030609f1015c358c8912d7a2d41f10a515af382d8fc

Contents?: true

Size: 1.48 KB

Versions: 53

Compression:

Stored size: 1.48 KB

Contents

ActiveModel::Translation.class_eval do

    # adds a default pluralization and singularization for english
    # useful to avoid to set a locale 'en' file and avoid
    # to pass around pluralize calls for 'en' defaults in hobo
    def human_attribute_name_with_en_pluralization_default(attribute, options={})
      if I18n.locale.to_s.match(/^en/)
        unless options[:count].blank? # skip default if we don't pass any count
          default = options[:count] == 1 ?
                    attribute.to_s.singularize.humanize : # singularize possible plural attributes
                    attribute.to_s.pluralize.humanize
          options.merge! :default => default
        end
      end
      human_attribute_name_without_en_pluralization_default(attribute, options)
    end
    alias_method_chain :human_attribute_name, :en_pluralization_default

    # Similar to human_name_attributes, this method retrieves the localized help string
    # of an attribute if it is defined as the key "activemodel.attribute_help.<attribute_name>",
    # otherwise it returns "".
    def attribute_help(attribute, options = {})
      defaults = lookup_ancestors.map do |klass|
        :"#{self.i18n_scope}.attribute_help.#{klass.to_s.underscore}.#{attribute}"
      end

      defaults << :"attribute_help.#{attribute}"
      defaults << options.delete(:default) if options[:default]
      defaults << ''

      options.reverse_merge! :count => 1, :default => defaults
      I18n.translate(defaults.shift, options)
    end

end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
hobo-2.0.0.pre7 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre6 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre5 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre4 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.2 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.1 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre3 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre2 lib/hobo/extensions/active_model/translation.rb
hobo-2.0.0.pre1 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre8 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre7 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre6 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre5 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre4 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre3 lib/hobo/extensions/active_model/translation.rb
hobo-1.4.0.pre2 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.0 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.0.RC4 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.0.RC3 lib/hobo/extensions/active_model/translation.rb
hobo-1.3.0.RC2 lib/hobo/extensions/active_model/translation.rb