Skip to content

Commit

Permalink
New version with improvements by ddidier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost Hietbrink committed Jul 30, 2012
2 parents 657a1f3 + b7c03c1 commit a6a5e3b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down
15 changes: 10 additions & 5 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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
6 changes: 6 additions & 0 deletions lib/phony_rails/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

en:
activerecord:
errors:
messages:
improbable_phone: "is an invalid number"
6 changes: 6 additions & 0 deletions lib/phony_rails/locales/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

fr:
activerecord:
errors:
messages:
improbable_phone: "est un numéro invalide"
2 changes: 1 addition & 1 deletion lib/phony_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module PhonyRails
VERSION = "0.1.7"
VERSION = "0.1.8"
end
2 changes: 1 addition & 1 deletion lib/validators/phony_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/validators/phony_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
require 'spec_helper'

#-----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -81,6 +82,7 @@ class BigHelpfulHome < ActiveRecord::Base
# Tests
#-----------------------------------------------------------------------------------------------------------------------

I18n.locale = :en
VALID_NUMBER = '123456789'
INVALID_NUMBER = '123456789 123456789 123456789 123456789'

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a6a5e3b

Please sign in to comment.