From ccf9577aee86ce1f766c5e8854e0c285dc38f8ac Mon Sep 17 00:00:00 2001 From: Evgeniy Dolzhenko Date: Fri, 11 Jun 2010 14:15:34 +0400 Subject: Fix a bunch of minor spelling mistakes --- activemodel/lib/active_model/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib/active_model/errors.rb') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 15d468f5d8..5c076d9d2f 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -240,7 +240,7 @@ module ActiveModel # default message (e.g. activemodel.errors.messages.MESSAGE). The translated model name, # translated attribute name and the value are available for interpolation. # - # When using inheritence in your models, it will check all the inherited models too, but only if the model itself + # When using inheritance in your models, it will check all the inherited models too, but only if the model itself # hasn't been found. Say you have class Admin < User; end and you wanted the translation for the :blank # error +message+ for the title +attribute+, it looks for these translations: # -- cgit v1.2.3 From 26392c4ac57e27c63984d47c6326c13f502d5786 Mon Sep 17 00:00:00 2001 From: Jeroen van Dijk Date: Wed, 19 May 2010 15:25:46 +0100 Subject: Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an options hash and make various Validators pass their (filtered) options. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it possible to pass additional options through Validators to message generation. E.g. plugin authors want to add validates_presence_of :foo, :format => "some format". Also, cleanup the :default vs :message options confusion in ActiveModel validation message generation. Also, deprecate ActiveModel::Errors#add_on_blank(attributes, custom_message) in favor of ActiveModel::Errors#add_on_blank(attributes, options). Also, refactoring of ActiveModel and ActiveRecord Validation tests. Test are a lot more DRY now. Better test coverage as well now. The first four points were reapplied from an older patch of Sven Fuchs which didn't apply cleanly anymore and was not complete yet. Signed-off-by: José Valim --- activemodel/lib/active_model/errors.rb | 52 +++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'activemodel/lib/active_model/errors.rb') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 9efb683547..b4660f3587 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -182,25 +182,43 @@ module ActiveModel # If +message+ is a proc, it will be called, allowing for things like Time.now to be used within an error. def add(attribute, message = nil, options = {}) message ||= :invalid - message = generate_message(attribute, message, options) if message.is_a?(Symbol) + + validation_conditionals = [:if, :unless, :on] + + message = generate_message(attribute, message, options.except(*validation_conditionals)) if message.is_a?(Symbol) + message = message.call if message.is_a?(Proc) self[attribute] << message end # Will add an error message to each of the attributes in +attributes+ that is empty. - def add_on_empty(attributes, custom_message = nil) + def add_on_empty(attributes, options = {}) + if options && !options.is_a?(Hash) + options = { :message => options } + ActiveSupport::Deprecation.warn \ + "ActiveModel::Errors#add_on_empty(attributes, custom_message) has been deprecated.\n" + + "Instead of passing a custom_message pass an options Hash { :message => custom_message }." + end + [attributes].flatten.each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) is_empty = value.respond_to?(:empty?) ? value.empty? : false - add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty + add(attribute, :empty, options) if value.nil? || is_empty end end # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?). - def add_on_blank(attributes, custom_message = nil) + def add_on_blank(attributes, options = {}) + if options && !options.is_a?(Hash) + options = { :message => options } + ActiveSupport::Deprecation.warn \ + "ActiveModel::Errors#add_on_blank(attributes, custom_message) has been deprecated.\n" + + "Instead of passing a custom_message pass an options Hash { :message => custom_message }." + end + [attributes].flatten.each do |attribute| value = @base.send(:read_attribute_for_validation, attribute) - add(attribute, :blank, :default => custom_message) if value.blank? + add(attribute, :blank, options) if value.blank? end end @@ -262,18 +280,26 @@ module ActiveModel #
  • errors.attributes.title.blank
  • #
  • errors.messages.blank
  • # - def generate_message(attribute, message = :invalid, options = {}) - message, options[:default] = options[:default], message if options[:default].is_a?(Symbol) + + def generate_message(attribute, type = :invalid, options = {}) + type = options.delete(:message) if options[:message].is_a?(Symbol) + + if options[:default] + ActiveSupport::Deprecation.warn \ + "ActiveModel::Errors#generate_message(attributes, custom_message) has been deprecated.\n" + + "Use ActiveModel::Errors#generate_message(attributes, :message => 'your message') instead." + options[:message] = options.delete(:default) + end defaults = @base.class.lookup_ancestors.map do |klass| - [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}", - :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ] + [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}", + :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ] end - defaults << options.delete(:default) - defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}" - defaults << :"errors.attributes.#{attribute}.#{message}" - defaults << :"errors.messages.#{message}" + defaults << options.delete(:message) + defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}" + defaults << :"errors.attributes.#{attribute}.#{type}" + defaults << :"errors.messages.#{type}" defaults.compact! defaults.flatten! -- cgit v1.2.3 From 0421fb7a913c1c8a7e07a395106bbc65e75e9d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 21 Jun 2010 12:17:24 +0200 Subject: Refactor previous commit a bit [#4057 state:resolved] --- activemodel/lib/active_model/errors.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'activemodel/lib/active_model/errors.rb') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index b4660f3587..d943e044a7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -61,6 +61,8 @@ module ActiveModel class Errors < ActiveSupport::OrderedHash include DeprecatedErrorMethods + CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank] + # Pass in the instance of the object that is using the errors object. # # class Person @@ -183,11 +185,12 @@ module ActiveModel def add(attribute, message = nil, options = {}) message ||= :invalid - validation_conditionals = [:if, :unless, :on] - - message = generate_message(attribute, message, options.except(*validation_conditionals)) if message.is_a?(Symbol) + if message.is_a?(Symbol) + message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS)) + elsif message.is_a?(Proc) + message = message.call + end - message = message.call if message.is_a?(Proc) self[attribute] << message end -- cgit v1.2.3 From e8c064bbe0fb5e07c7ceaa45d0cafa3c4ef01ab0 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Wed, 23 Jun 2010 14:41:28 +0200 Subject: Regression with how base errors messages are added to a model. Works correctly for both string error messages and symbol translated messages. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activemodel/lib/active_model/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib/active_model/errors.rb') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index d943e044a7..ae7822d8d5 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -308,7 +308,7 @@ module ActiveModel defaults.flatten! key = defaults.shift - value = @base.send(:read_attribute_for_validation, attribute) + value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) options = { :default => defaults, -- cgit v1.2.3 From 7bd85a8fc2d216a5e2b1d0380df572f782a54d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 26 Jun 2010 11:57:43 +0200 Subject: Work around the fact the JSON gem was overwriting to_json implementation for all Ruby core classes. This is required because the JSON gem is incompatible with Rails behavior and was not allowing ActiveModel::Errors to be serialized. So we need to ensure Rails implementation is the one triggered. [#4890 state:resolved] --- activemodel/lib/active_model/errors.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'activemodel/lib/active_model/errors.rb') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index ae7822d8d5..d42fc5291d 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/hash/reverse_merge' require 'active_support/ordered_hash' module ActiveModel @@ -164,15 +165,12 @@ module ActiveModel # # name must be specified # # def to_xml(options={}) - require 'builder' unless defined? ::Builder - options[:root] ||= "errors" - options[:indent] ||= 2 - options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent]) - - options[:builder].instruct! unless options.delete(:skip_instruct) - options[:builder].errors do |e| - to_a.each { |error| e.error(error) } - end + to_a.to_xml options.reverse_merge(:root => "errors", :skip_types => true) + end + + # Returns an array as JSON representation for this object. + def as_json(options=nil) + to_a end # Adds +message+ to the error messages on +attribute+, which will be returned on a call to @@ -283,7 +281,6 @@ module ActiveModel #
  • errors.attributes.title.blank
  • #
  • errors.messages.blank
  • # - def generate_message(attribute, type = :invalid, options = {}) type = options.delete(:message) if options[:message].is_a?(Symbol) -- cgit v1.2.3