Permalink
Browse files

New version with improvements by ddidier.

  • Loading branch information...
2 parents 657a1f3 + b7c03c1 commit a6a5e3bc099f7a0b57e9f7f09a2257a11cfeee01 @joost committed Jul 30, 2012
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- phony_rails (0.1.6)
+ phony_rails (0.1.8)
activerecord (~> 3.0)
countries (~> 0.8.2)
phony (~> 1.7.7)
View
@@ -62,6 +62,8 @@ so we can use:
validates_plausible_phone :phone_number, :without => /^\+\d+/
validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/
+the i18n key is `:improbable_phone`
+
### Display / Views
In your views use:
@@ -76,6 +78,9 @@ Say you want to find a record by a phone number. Best is to normalize user input
## Changelog
+0.1.8
+* Improved validation methods by ddidier.
+
0.1.6
* Added :as option to phony_normalize.
View
@@ -40,8 +40,8 @@ def self.extended(base)
end
module InstanceMethods
-
- private
+
+ private
# This methods sets the attribute to the normalized version.
# It also adds the country_code (number), eg. 31 for NL numbers.
@@ -70,7 +70,7 @@ def phony_normalize(*attributes)
attributes.each do |attribute|
raise ArgumentError, "No attribute #{attribute} found on #{self.class.name} (PhonyRails)" if not self.attribute_method?(attribute)
# Add before validation that saves a normalized version of the phone number
- self.before_validation do
+ self.before_validation do
set_phony_normalized_numbers(attributes, options)
end
end
@@ -80,7 +80,7 @@ def phony_normalize(*attributes)
# phony_normalized_method :fax_number, :default_country_code => 'US'
# Creates a normalized_fax_number method.
def phony_normalized_method(*attributes)
- main_options = attributes.last.is_a?(Hash) ? attributes.pop : {}
+ main_options = attributes.last.is_a?(Hash) ? attributes.pop : {}
main_options.assert_valid_keys :country_code, :default_country_code
attributes.each do |attribute|
raise StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)" if self.instance_methods.include?(:"normalized_#{attribute}")
@@ -97,4 +97,9 @@ def phony_normalized_method(*attributes)
end
end
-ActiveRecord::Base.extend PhonyRails::ActiveRecordExtension
+
+ActiveRecord::Base.extend PhonyRails::ActiveRecordExtension
+
+Dir["#{File.dirname(__FILE__)}/phony_rails/locales/*.yml"].each do |file|
+ I18n.load_path << file
+end
@@ -0,0 +1,6 @@
+
+en:
+ activerecord:
+ errors:
+ messages:
+ improbable_phone: "is an invalid number"
@@ -0,0 +1,6 @@
+
+fr:
+ activerecord:
+ errors:
+ messages:
+ improbable_phone: "est un numéro invalide"
@@ -1,3 +1,3 @@
module PhonyRails
- VERSION = "0.1.7"
+ VERSION = "0.1.8"
end
@@ -6,7 +6,7 @@ class PhonyPlausibleValidator < ActiveModel::EachValidator
# Validates a String using Phony.plausible? method.
def validate_each(record, attribute, value)
return if value.blank?
- record.errors[attribute] << (options[:message] || "is an invalid number") if not Phony.plausible?(value)
+ record.errors.add(attribute, options[:message] || :improbable_phone) if not Phony.plausible?(value)
end
end
@@ -1,3 +1,4 @@
+# encoding: utf-8
require 'spec_helper'
#-----------------------------------------------------------------------------------------------------------------------
@@ -81,6 +82,7 @@ class BigHelpfulHome < ActiveRecord::Base
# Tests
#-----------------------------------------------------------------------------------------------------------------------
+I18n.locale = :en
VALID_NUMBER = '123456789'
INVALID_NUMBER = '123456789 123456789 123456789 123456789'
@@ -109,6 +111,22 @@ class BigHelpfulHome < ActiveRecord::Base
@home.errors.messages.should include(:phone_number => ["is an invalid number"])
end
+ it "should translate the error message in english" do
+ I18n.with_locale(:en) do
+ @home.phone_number = INVALID_NUMBER
+ @home.valid?
+ @home.errors.messages.should include(:phone_number => ["is an invalid number"])
+ end
+ end
+
+ it "should translate the error message in french" do
+ I18n.with_locale(:fr) do
+ @home.phone_number = INVALID_NUMBER
+ @home.valid?
+ @home.errors.messages.should include(:phone_number => ["est un numéro invalide"])
+ end
+ end
+
end
end

0 comments on commit a6a5e3b

Please sign in to comment.