Skip to content

Instantly share code, notes, and snippets.

@reyesyang
Created April 11, 2013 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyesyang/5361981 to your computer and use it in GitHub Desktop.
Save reyesyang/5361981 to your computer and use it in GitHub Desktop.
Rewrite human_attribute_name method in ActiveModel::Translation file for Padrino project which use ActiveRecord as ORM.
module ActiveModel
module Translation
include ActiveModel::Naming
def human_attribute_name(attribute, options = {})
options = { :count => 1 }.merge!(options)
parts = attribute.to_s.split(".")
attribute = parts.pop
namespace = parts.join("/") unless parts.empty?
attributes_scope = "models"
if namespace
defaults = lookup_ancestors.map do |klass|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes/#{namespace}.#{attribute}"
end
defaults << :"#{attributes_scope}.attributes.#{namespace}.#{attribute}"
else
defaults = lookup_ancestors.map do |klass|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute}"
end
end
defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
defaults << attribute.humanize
options[:default] = defaults
I18n.translate(defaults.shift, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment