Why do twitter-bootstrap-rails generated views use model_class.human_attribute_name (: column) instead of a string?

Using twitter-bootstrap-rails using a generator:

rails g bootstrap:themed Model 

Creates views containing strings, such as:

  <th><%= model_class.human_attribute_name(:comment_date) %></th> <th><%= model_class.human_attribute_name(:comment_time) %></th> 

where the default rail generator simply inserts the name of the human-readable column directly:

  <th>Comment date</th> <th>Comment time</th> 

which makes cleaner views and seems to be more efficient at runtime.

Are there any benefits to twitter-bootstrap-rails generated views?

Thanks!

(PS, the developers website is offline, and neither github nor google + allow private messages, and neither the "problem" on github nor the public comment on google + post seems to be suitable for this question so hopefully someone here may be familiar with the use case for model_class.human_attribute_name ())

+4
source share
1 answer

Take a look at its source, and everything will be clear to you:

 # File activemodel/lib/active_model/translation.rb, line 45 def human_attribute_name(attribute, options = {}) defaults = [] parts = attribute.to_s.split(".", 2) attribute = parts.pop namespace = parts.pop if namespace lookup_ancestors.each do |klass| defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}" end defaults << :"#{self.i18n_scope}.attributes.#{namespace}.#{attribute}" else lookup_ancestors.each do |klass| defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}" end end defaults << :"attributes.#{attribute}" defaults << options.delete(:default) if options[:default] defaults << attribute.humanize options.reverse_merge! :count => 1, :default => defaults I18n.translate(defaults.shift, options) end 

human_attribute_name great job of localizing the names of your attributes. Even if you are building an application that uses exclusively English, who knows, maybe your mailbox already has an email from your client about his plans to expand his business, and this extension will include countries where people write from right to left.

How it works

Suppose you have a Product model with a title attribute. Call human_attribute_name as follows:

 Product.human_attribute_name 'title' 

will call I18n.t (see the last line in the fragment above) with the following parameters:

 I18.t 'activerecord.attributes.product.title', { :count=>1, :default=>[:"attributes.title", "Title"] } 

In this way, you can provide translations that are specific to a model and specific to an attribute name. If you do not provide any of these translations, you will receive an English version of your attribute created using the humanize method.

+5
source

All Articles