aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-11-17 22:47:23 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-11-17 22:47:23 +0000
commit5446d5cb05b50a9a3f317ded774be438e0eff909 (patch)
tree6b0b87efe3e95783763208215a3159fb63217a6d /activemodel/lib
parent9754debb9a72f9385950e5282f3642b995ab76d8 (diff)
parentf8877d4b2a2a6f68770b376f0b1391a6295f62f2 (diff)
downloadrails-5446d5cb05b50a9a3f317ded774be438e0eff909.tar.gz
rails-5446d5cb05b50a9a3f317ded774be438e0eff909.tar.bz2
rails-5446d5cb05b50a9a3f317ded774be438e0eff909.zip
Merge remote branch 'mainstream/master'
Conflicts: activesupport/lib/active_support/core_ext/hash/conversions.rb
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model.rb2
-rw-r--r--activemodel/lib/active_model/errors.rb19
-rw-r--r--activemodel/lib/active_model/naming.rb9
-rw-r--r--activemodel/lib/active_model/translation.rb64
-rw-r--r--activemodel/lib/active_model/validations.rb11
-rw-r--r--activemodel/lib/active_model/validator.rb68
6 files changed, 153 insertions, 20 deletions
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb
index 67f529262d..505e16c195 100644
--- a/activemodel/lib/active_model.rb
+++ b/activemodel/lib/active_model.rb
@@ -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
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 7a48960f89..e8bb62953d 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -93,7 +93,7 @@ module ActiveModel
# 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 @@ module ActiveModel
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 @@ module ActiveModel
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 @@ module ActiveModel
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)
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index b8c2a367b4..675d62b9a6 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -5,12 +5,13 @@ module ActiveModel
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 ActiveModel
# 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
diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb
new file mode 100644
index 0000000000..42ca463f82
--- /dev/null
+++ b/activemodel/lib/active_model/translation.rb
@@ -0,0 +1,64 @@
+require 'active_support/core_ext/hash/reverse_merge'
+
+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={})
+ return @human unless @klass.respond_to?(:lookup_ancestors) &&
+ @klass.respond_to?(:i18n_scope)
+
+ defaults = @klass.lookup_ancestors.map do |klass|
+ klass.model_name.underscore.to_sym
+ end
+
+ defaults << options.delete(:default) if options[:default]
+ defaults << @human
+
+ options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
+ I18n.translate(defaults.shift, options)
+ end
+ end
+end
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index edeb508a08..064ec98f3a 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -1,14 +1,13 @@
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/keys'
-require 'active_support/concern'
-require 'active_support/callbacks'
module ActiveModel
module Validations
extend ActiveSupport::Concern
- include ActiveSupport::NewCallbacks
+ include ActiveSupport::Callbacks
included do
+ extend ActiveModel::Translation
define_callbacks :validate, :scope => :name
end
@@ -99,7 +98,7 @@ module ActiveModel
def invalid?
!valid?
end
-
+
protected
# Hook method defining how an attribute value should be retieved. By default this is assumed
# to be an instance named after the attribute. Override this method in subclasses should you
@@ -110,9 +109,9 @@ module ActiveModel
# def initialize(data = {})
# @data = data
# end
- #
+ #
# private
- #
+ #
# def read_attribute_for_validation(key)
# @data[key]
# end
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
new file mode 100644
index 0000000000..09de72b757
--- /dev/null
+++ b/activemodel/lib/active_model/validator.rb
@@ -0,0 +1,68 @@
+module ActiveModel #:nodoc:
+
+ # A simple base class that can be used along with ActiveModel::Base.validates_with
+ #
+ # class Person < ActiveModel::Base
+ # validates_with MyValidator
+ # end
+ #
+ # class MyValidator < ActiveModel::Validator
+ # def validate
+ # if some_complex_logic
+ # record.errors[:base] = "This record is invalid"
+ # end
+ # end
+ #
+ # private
+ # def some_complex_logic
+ # # ...
+ # end
+ # end
+ #
+ # 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 < ActiveModel::Base
+ # validates_with MyValidator
+ # end
+ #
+ # class MyValidator < ActiveModel::Validator
+ # def validate
+ # record # => The person instance being validated
+ # options # => Any non-standard options passed to validates_with
+ # end
+ # end
+ #
+ # To cause a validation error, you must add to the <tt>record<tt>'s errors directly
+ # from within the validators message
+ #
+ # class MyValidator < ActiveModel::Validator
+ # def validate
+ # record.errors[:base] << "This is some custom error message"
+ # record.errors[:first_name] << "This is some complex validation"
+ # # etc...
+ # end
+ # end
+ #
+ # To add behavior to the initialize method, use the following signature:
+ #
+ # class MyValidator < ActiveModel::Validator
+ # def initialize(record, options)
+ # super
+ # @my_custom_field = options[:field_name] || :first_name
+ # end
+ # end
+ #
+ class Validator
+ attr_reader :record, :options
+
+ def initialize(record, options)
+ @record = record
+ @options = options
+ end
+
+ def validate
+ raise "You must override this method"
+ end
+ end
+end