Created
April 11, 2013 09:27
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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