4

With twitter-bootstrap-rails installed, using the generator:

rails g bootstrap:themed Model

creates views that contain lines such as:

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

where the default rails generator just inserts the human readable column name directly:

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

which makes for cleaner views, and seems like it would be more efficient at run-time.

Is there a benefit to the twitter-bootstrap-rails generated views?

Thanks!

(PS, The developers website is offline, and neither github nor google+ allow sending private messages, and neither an "issue" on github nor a public comment on a google+ post seemed appropriate for this question, so hoping someone here might be familiar with the use-case for model_class.human_attribute_name())

Matt
  • 3,682
  • 1
  • 21
  • 27
  • 1
    I am a complete and utter noob with RoR, so here's a comment instead of an answer: probably i18n / l10n? Does that make any sense? – dr Hannibal Lecter Jun 10 '12 at 20:51
  • @drHannibalLecter Good guess! `human_attribute_name` is indeed bothered by the internationalization stuff. So it's more appropriate in general case where you may add some locales in future. – jdoe Jun 10 '12 at 21:06

1 Answers1

5

Take a look at its source and things will get 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 does a great job for localizing your attribute names. Even if you're building app which uses English exclusively -- who knows, maybe your inbox already has an email from your client about his plans on expanding business and this expansion will include countries where people write right-to-left.

How it works

Assume you have Product model with title attribute. Calling human_attribute_name this way:

Product.human_attribute_name 'title'

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

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

So, you can provide translations specific to some model and specific to some attribute's name. Unless you provide any of these translations you'll get English version of your attribute created via humanize method.

jdoe
  • 15,665
  • 2
  • 46
  • 48