Permalink
Browse files

Length validation now works with localized fields

  • Loading branch information...
1 parent 77c245a commit 6a8b5d6f3634ac1bb60361ef5ec0a56e848fae3d @durran durran committed Apr 22, 2012
View
@@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#1938 Length validation now works with localized fields.
+
* \#1933 `Proxy#extend` should delegate through to the target, where
extending the proxy itself is now handled through `Proxy#proxy_extend`.
@@ -61,4 +61,4 @@ en:
scope_overwrite:
"Cannot create scope :%{scope_name}, because of existing method
%{model_name}.%{scope_name}."
- blank_on_locale: "can't be blank in %{in_locale}"
+ blank_on_locale: "can't be blank in %{location}"
@@ -1,8 +1,10 @@
# encoding: utf-8
+require "mongoid/validations/localizable"
require "mongoid/validations/associated"
require "mongoid/validations/format"
-require "mongoid/validations/uniqueness"
+require "mongoid/validations/length"
require "mongoid/validations/presence"
+require "mongoid/validations/uniqueness"
module Mongoid #:nodoc:
@@ -2,7 +2,7 @@
module Mongoid #:nodoc:
module Validations #:nodoc:
- # Validates that the specified attributes do or do not match a certain
+ # Validates that the specified attributes do or do not match a certain
# regular expression.
#
# @example Set up the format validator.
@@ -14,27 +14,7 @@ module Validations #:nodoc:
# validates_format_of :website, :with => URI.regexp
# end
class FormatValidator < ActiveModel::Validations::FormatValidator
-
- # Validates each for format.
- #
- # @example Validate format.
- # validator.validate_each(model, :name, "value")
- #
- # @param [ Document ] document The document.
- # @param [ Symbol, String ] attribute The attribute to validate.
- # @param [ Object ] value The attribute value.
- #
- # @since 2.4.2
- def validate_each(document, attribute, value)
- field = document.fields[attribute.to_s]
- if field && field.localized? && !value.blank?
- value.each_pair do |_locale, _value|
- super(document, attribute, _value)
- end
- else
- super
- end
- end
+ include Localizable
end
end
end
@@ -0,0 +1,20 @@
+# encoding: utf-8
+module Mongoid
+ module Validations
+
+ # Validates that the specified attributes do or do not match a certain
+ # length.
+ #
+ # @example Set up the length validator.
+ #
+ # class Person
+ # include Mongoid::Document
+ # field :website
+ #
+ # validates_length_of :website, in: 1..10
+ # end
+ class LengthValidator < ActiveModel::Validations::LengthValidator
+ include Localizable
+ end
+ end
+end
@@ -0,0 +1,30 @@
+# encoding: utf-8
+module Mongoid
+ module Validations
+
+ # Adds localization support to validations.
+ module Localizable
+
+ # Validates each for localized fields.
+ #
+ # @example Validate localized fields.
+ # validator.validate_each(model, :name, "value")
+ #
+ # @param [ Document ] document The document.
+ # @param [ Symbol, String ] attribute The attribute to validate.
+ # @param [ Object ] value The attribute value.
+ #
+ # @since 2.4.2
+ def validate_each(document, attribute, value)
+ field = document.fields[attribute.to_s]
+ if field.try(:localized?) && !value.blank?
+ value.values.each do |_value|
+ super(document, attribute, _value)
+ end
+ else
+ super
+ end
+ end
+ end
+ end
+end
@@ -27,9 +27,9 @@ class PresenceValidator < ActiveModel::EachValidator
# @since 2.4.0
def validate_each(document, attribute, value)
field = document.fields[attribute.to_s]
- if field && field.localized? && !value.blank?
+ if field.try(:localized?) && !value.blank?
value.each_pair do |locale, value|
- document.errors.add(attribute, :blank_on_locale, options.merge(:in_locale => locale)) if value.blank?
+ document.errors.add(attribute, :blank_on_locale, options.merge(:location => locale)) if value.blank?
end
else
document.errors.add(attribute, :blank, options) if value.blank?
@@ -0,0 +1,89 @@
+require "spec_helper"
+
+describe Mongoid::Validations::LengthValidator do
+
+ describe "#validate_each" do
+
+ let(:product) do
+ Product.new
+ end
+
+ context "when the field is not localized" do
+
+ let(:validator) do
+ described_class.new(attributes: [:brand_name], in: 1..5)
+ end
+
+ context "when the value is valid" do
+
+ before do
+ validator.validate_each(product, :brand_name, "Apple")
+ end
+
+ it "adds no errors" do
+ product.errors[:brand_name].should be_empty
+ end
+ end
+
+ context "when the value is invalid" do
+
+ before do
+ validator.validate_each(product, :brand_name, "123456")
+ end
+
+ it "adds errors" do
+ product.errors[:brand_name].should eq(
+ ["is too long (maximum is 5 characters)"]
+ )
+ end
+ end
+ end
+
+ context "when the field is localized" do
+
+ let(:validator) do
+ described_class.new(attributes: [:website], in: 1..5)
+ end
+
+ context "when the localized value is valid" do
+
+ before do
+ validator.validate_each(product, :website, { "en" => "12345" })
+ end
+
+ it "adds no errors" do
+ product.errors[:website].should be_empty
+ end
+ end
+
+ context "when one of the localized values is invalid" do
+
+ before do
+ validator.validate_each(
+ product,
+ :website, { "en" => "12345", "fr" => "123456" }
+ )
+ end
+
+ it "adds errors" do
+ product.errors[:website].should eq(
+ ["is too long (maximum is 5 characters)"]
+ )
+ end
+ end
+
+ context "when the localized value is invalid" do
+
+ before do
+ validator.validate_each(product, :website, { "en" => "123456" })
+ end
+
+ it "adds errors" do
+ product.errors[:website].should eq(
+ ["is too long (maximum is 5 characters)"]
+ )
+ end
+ end
+ end
+ end
+end

0 comments on commit 6a8b5d6

Please sign in to comment.