Permalink
Browse files

Move validator, human_name and human_attribute_name to ActiveModel, r…

…emove deprecated error messages and add i18n_scope and lookup_ancestors.

Signed-off-by: Carl Lerche <carllerche@mac.com>
  • Loading branch information...
1 parent 4f6d6f7 commit e714b499cc1f7ebc84f8d0e96607b79e60f2828d @josevalim josevalim committed with Carl Lerche Oct 21, 2009
@@ -39,8 +39,10 @@ module ActiveModel
autoload :Serialization, 'active_model/serialization'
autoload :StateMachine, 'active_model/state_machine'
autoload :TestCase, 'active_model/test_case'
+ autoload :Translation, 'active_model/translation'
autoload :Validations, 'active_model/validations'
autoload :ValidationsRepairHelper, 'active_model/validations_repair_helper'
+ autoload :Validator, 'active_model/validator'
autoload :VERSION, 'active_model/version'
module Serializers
@@ -93,7 +93,7 @@ def add_on_blank(attributes, custom_message = nil)
# company = Company.create(:address => '123 First St.')
# company.errors.full_messages # =>
# ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
- def full_messages(options = {})
+ def full_messages
full_messages = []
each do |attribute, messages|
@@ -103,8 +103,10 @@ def full_messages(options = {})
if attribute == :base
messages.each {|m| full_messages << m }
else
- attr_name = attribute.to_s.humanize
- prefix = attr_name + I18n.t('activemodel.errors.format.separator', :default => ' ')
+ attr_name = @base.class.human_attribute_name(attribute)
+ options = { :default => ' ', :scope => @base.class.i18n_scope }
+ prefix = attr_name + I18n.t(:"errors.format.separator", options)
+
messages.each do |m|
full_messages << "#{prefix}#{m}"
end
@@ -135,10 +137,7 @@ def full_messages(options = {})
def generate_message(attribute, message = :invalid, options = {})
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
- klass_ancestors = [@base.class]
- klass_ancestors += @base.class.ancestors.reject {|x| x.is_a?(Module)}
-
- defaults = klass_ancestors.map do |klass|
+ defaults = @base.class.lookup_ancestors.map do |klass|
[ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}",
:"models.#{klass.name.underscore}.#{message}" ]
end
@@ -150,10 +149,10 @@ def generate_message(attribute, message = :invalid, options = {})
value = @base.send(:read_attribute_for_validation, attribute)
options = { :default => defaults,
- :model => @base.class.name.humanize,
- :attribute => attribute.to_s.humanize,
+ :model => @base.class.model_name.human,
+ :attribute => @base.class.human_attribute_name(attribute),
:value => value,
- :scope => [:activemodel, :errors]
+ :scope => [@base.class.i18n_scope, :errors]
}.merge(options)
I18n.translate(key, options)
@@ -5,12 +5,13 @@ class Name < String
attr_reader :singular, :plural, :element, :collection, :partial_path, :human
alias_method :cache_key, :collection
- def initialize(name)
- super
+ def initialize(klass, name)
+ super(name)
+ @klass = klass
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
- @human = @element.gsub(/_/, " ")
+ @human = ActiveSupport::Inflector.humanize(@element).freeze
@collection = ActiveSupport::Inflector.tableize(self).freeze
@partial_path = "#{@collection}/#{@element}".freeze
end
@@ -20,7 +21,7 @@ module Naming
# Returns an ActiveModel::Name object for module. It can be
# used to retrieve all kinds of naming-related information.
def model_name
- @_model_name ||= ActiveModel::Name.new(name)
+ @_model_name ||= ActiveModel::Name.new(self, name)
end
end
end
@@ -0,0 +1,59 @@
+module ActiveModel
+ module Translation
+ include ActiveModel::Naming
+
+ # Returns the i18n_scope for the class. Overwrite if you want custom lookup.
+ def i18n_scope
+ :activemodel
+ end
+
+ # When localizing a string, goes through the lookup returned by this method.
+ # Used in ActiveModel::Name#human, ActiveModel::Errors#full_messages and
+ # ActiveModel::Translation#human_attribute_name.
+ def lookup_ancestors
+ self.ancestors.select { |x| x.respond_to?(:model_name) }
+ end
+
+ # Transforms attributes names into a more human format, such as "First name" instead of "first_name".
+ #
+ # Example:
+ #
+ # Person.human_attribute_name("first_name") # => "First name"
+ #
+ # Specify +options+ with additional translating options.
+ def human_attribute_name(attribute, options = {})
+ defaults = lookup_ancestors.map do |klass|
+ :"#{klass.model_name.underscore}.#{attribute}"
+ end
+
+ defaults << options.delete(:default) if options[:default]
+ defaults << attribute.to_s.humanize
+
+ options.reverse_merge! :scope => [self.i18n_scope, :attributes], :count => 1, :default => defaults
+ I18n.translate(defaults.shift, options)
+ end
+
+ # Model.human_name is deprecated. Use Model.model_name.human instead.
+ def human_name(*args)
+ ActiveSupport::Deprecation.warn("human_name has been deprecated, please use model_name.human instead", caller[0,1])
+ model_name.human(*args)
+ end
+ end
+
+ class Name < String
+ # Transform the model name into a more humane format, using I18n. By default,
+ # it will underscore then humanize the class name (BlogPost.human_name #=> "Blog post").
+ # Specify +options+ with additional translating options.
+ def human(options={})
+ defaults = @klass.lookup_ancestors.map do |klass|
+ klass.model_name.underscore.to_sym
+ end
+
+ defaults << options.delete(:default) if options[:default]
@jfirebaugh

jfirebaugh Aug 12, 2010

Contributor

Shouldn't be modifying the options hash destructively here and with reverse_merge! below.

+ defaults << @human
+
+ options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
+ I18n.translate(defaults.shift, options)
+ end
+ end
+end
@@ -7,6 +7,7 @@ module Validations
include ActiveSupport::Callbacks
included do
+ extend ActiveModel::Translation
define_callbacks :validate, :scope => :name
end
@@ -1,12 +1,12 @@
-module ActiveRecord #:nodoc:
+module ActiveModel #:nodoc:
- # A simple base class that can be used along with ActiveRecord::Base.validates_with
+ # A simple base class that can be used along with ActiveModel::Base.validates_with
#
- # class Person < ActiveRecord::Base
+ # class Person < ActiveModel::Base
# validates_with MyValidator
# end
#
- # class MyValidator < ActiveRecord::Validator
+ # class MyValidator < ActiveModel::Validator
# def validate
# if some_complex_logic
# record.errors[:base] = "This record is invalid"
@@ -19,14 +19,14 @@ module ActiveRecord #:nodoc:
# end
# end
#
- # Any class that inherits from ActiveRecord::Validator will have access to <tt>record</tt>,
+ # Any class that inherits from ActiveModel::Validator will have access to <tt>record</tt>,
# which is an instance of the record being validated, and must implement a method called <tt>validate</tt>.
#
- # class Person < ActiveRecord::Base
+ # class Person < ActiveModel::Base
# validates_with MyValidator
# end
#
- # class MyValidator < ActiveRecord::Validator
+ # class MyValidator < ActiveModel::Validator
# def validate
# record # => The person instance being validated
# options # => Any non-standard options passed to validates_with
@@ -36,7 +36,7 @@ module ActiveRecord #:nodoc:
# To cause a validation error, you must add to the <tt>record<tt>'s errors directly
# from within the validators message
#
- # class MyValidator < ActiveRecord::Validator
+ # class MyValidator < ActiveModel::Validator
# def validate
# record.errors[:base] << "This is some custom error message"
# record.errors[:first_name] << "This is some complex validation"
@@ -46,7 +46,7 @@ module ActiveRecord #:nodoc:
#
# To add behavior to the initialize method, use the following signature:
#
- # class MyValidator < ActiveRecord::Validator
+ # class MyValidator < ActiveModel::Validator
# def initialize(record, options)
# super
# @my_custom_field = options[:field_name] || :first_name
@@ -2,7 +2,7 @@
class NamingTest < ActiveModel::TestCase
def setup
- @model_name = ActiveModel::Name.new('Post::TrackBack')
+ @model_name = ActiveModel::Name.new(self, 'Post::TrackBack')
end
def test_singular
@@ -0,0 +1,51 @@
+require 'cases/helper'
+
+class SuperUser
+ extend ActiveModel::Translation
+end
+
+class User < SuperUser
+end
+
+class ActiveModelI18nTests < ActiveModel::TestCase
+
+ def setup
+ I18n.backend = I18n::Backend::Simple.new
+ end
+
+ def test_translated_model_attributes
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } }
+ assert_equal 'super_user name attribute', SuperUser.human_attribute_name('name')
+ end
+
+ def test_translated_model_attributes_with_symbols
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } }
+ assert_equal 'super_user name attribute', SuperUser.human_attribute_name(:name)
+ end
+
+ def test_translated_model_attributes_with_ancestor
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:user => {:name => 'user name attribute'} } }
+ assert_equal 'user name attribute', User.human_attribute_name('name')
+ end
+
+ def test_translated_model_attributes_with_ancestors_fallback
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:super_user => {:name => 'super_user name attribute'} } }
+ assert_equal 'super_user name attribute', User.human_attribute_name('name')
+ end
+
+ def test_translated_model_names
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:super_user => 'super_user model'} }
+ assert_equal 'super_user model', SuperUser.model_name.human
+ end
+
+ def test_translated_model_names_with_sti
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:user => 'user model'} }
+ assert_equal 'user model', User.model_name.human
+ end
+
+ def test_translated_model_names_with_ancestors_fallback
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:super_user => 'super_user model'} }
+ assert_equal 'super_user model', User.model_name.human
+ end
+end
+
@@ -63,7 +63,6 @@ def test_generate_message_exclusion_with_custom_message
assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, :default => 'custom message {{value}}', :value => 'title')
end
- # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
# validates_format_of: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
def test_generate_message_invalid_with_default_message
assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, :default => nil, :value => 'title')
@@ -56,6 +56,12 @@ def test_errors_add_on_blank_generates_message_with_custom_default_message
@person.errors.add_on_blank :title, 'custom'
end
+ def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
+ @person.errors.add('name', 'empty')
+ I18n.expects(:translate).with(:"person.name", :default => ['Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name')
+ @person.errors.full_messages
+ end
+
# ActiveRecord::Validations
# validates_confirmation_of w/ mocha
def test_validates_confirmation_of_generates_message
@@ -494,6 +500,8 @@ def test_validates_numericality_of_less_than_finds_global_default_translation
assert_equal ['global message'], @person.errors[:title]
end
+ # test with validates_with
+
def test_validations_with_message_symbol_must_translate
I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
Person.validates_presence_of :title, :message => :custom_error
@@ -13,24 +13,24 @@ class ValidatesWithTest < ActiveRecord::TestCase
ERROR_MESSAGE = "Validation error from validator"
OTHER_ERROR_MESSAGE = "Validation error from other validator"
- class ValidatorThatAddsErrors < ActiveRecord::Validator
+ class ValidatorThatAddsErrors < ActiveModel::Validator
def validate()
record.errors[:base] << ERROR_MESSAGE
end
end
- class OtherValidatorThatAddsErrors < ActiveRecord::Validator
+ class OtherValidatorThatAddsErrors < ActiveModel::Validator
def validate()
record.errors[:base] << OTHER_ERROR_MESSAGE
end
end
- class ValidatorThatDoesNotAddErrors < ActiveRecord::Validator
+ class ValidatorThatDoesNotAddErrors < ActiveModel::Validator
def validate()
end
end
- class ValidatorThatValidatesOptions < ActiveRecord::Validator
+ class ValidatorThatValidatesOptions < ActiveModel::Validator
def validate()
if options[:field] == :first_name
record.errors[:base] << ERROR_MESSAGE
@@ -71,7 +71,6 @@ def self.load_all!
autoload :Timestamp, 'active_record/timestamp'
autoload :Transactions, 'active_record/transactions'
autoload :Types, 'active_record/types'
- autoload :Validator, 'active_record/validator'
autoload :Validations, 'active_record/validations'
module AttributeMethods
@@ -1386,7 +1386,8 @@ def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodo
subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
end
- def self_and_descendants_from_active_record#nodoc:
+ # Set the lookup ancestors for ActiveModel.
+ def lookup_ancestors #:nodoc:
klass = self
classes = [klass]
while klass != klass.base_class
@@ -1400,32 +1401,9 @@ def self_and_descendants_from_active_record#nodoc:
[self]
end
- # Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example:
- # Person.human_attribute_name("first_name") # => "First name"
- # This used to be deprecated in favor of humanize, but is now preferred, because it automatically uses the I18n
- # module now.
- # Specify +options+ with additional translating options.
- def human_attribute_name(attribute_key_name, options = {})
- defaults = self_and_descendants_from_active_record.map do |klass|
- :"#{klass.name.underscore}.#{attribute_key_name}"
- end
- defaults << options[:default] if options[:default]
- defaults.flatten!
- defaults << attribute_key_name.to_s.humanize
- options[:count] ||= 1
- I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes]))
- end
-
- # Transform the modelname into a more humane format, using I18n.
- # By default, it will underscore then humanize the class name (BlogPost.human_name #=> "Blog post").
- # Default scope of the translation is activerecord.models
- # Specify +options+ with additional translating options.
- def human_name(options = {})
- defaults = self_and_descendants_from_active_record.map do |klass|
- :"#{klass.name.underscore}"
- end
- defaults << self.name.underscore.humanize
- I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
+ # Set the i18n scope to overwrite ActiveModel.
+ def i18n_scope #:nodoc:
+ :activerecord
end
# True if this isn't a concrete subclass needing a STI type condition.
Oops, something went wrong.

0 comments on commit e714b49

Please sign in to comment.