Permalink
Browse files

New options for .phony_formatted String helper.

  • Loading branch information...
1 parent f6018e5 commit 542d683bb51fbca8394d36a6e32298287eee2bb8 @joost committed Feb 13, 2013
Showing with 84 additions and 21 deletions.
  1. +10 −9 Gemfile.lock
  2. +13 −1 README.md
  3. +0 −1 lib/phony_rails/locales/fr.yml
  4. +11 −4 lib/phony_rails/string_extensions.rb
  5. +1 −1 lib/phony_rails/version.rb
  6. +49 −5 spec/lib/phony_rails_spec.rb
View
@@ -1,23 +1,23 @@
PATH
remote: .
specs:
- phony_rails (0.1.12)
+ phony_rails (0.1.13)
activerecord (>= 3.0)
countries (>= 0.8.2)
phony (>= 1.7.7)
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.2.10)
- activesupport (= 3.2.10)
+ activemodel (3.2.12)
+ activesupport (= 3.2.12)
builder (~> 3.0.0)
- activerecord (3.2.10)
- activemodel (= 3.2.10)
- activesupport (= 3.2.10)
+ activerecord (3.2.12)
+ activemodel (= 3.2.12)
+ activesupport (= 3.2.12)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activesupport (3.2.10)
+ activesupport (3.2.12)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
@@ -41,8 +41,8 @@ GEM
rb-fchange (~> 0.0.5)
rb-fsevent (~> 0.9.1)
rb-inotify (~> 0.8.8)
- multi_json (1.5.0)
- phony (1.8.4)
+ multi_json (1.6.0)
+ phony (1.8.6)
rake (10.0.3)
rb-fchange (0.0.5)
ffi
@@ -65,6 +65,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ countries
growl
guard (~> 1.2.0)
guard-bundler (~> 1.0.0)
View
@@ -76,7 +76,19 @@ the i18n key is `:improbable_phone`
In your views use:
- <%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>
+ <%= "311012341234".phony_formatted(:format => :international, :spaces => '-') %>
+ <%= "+31-10-12341234".phony_formatted(:format => :international, :spaces => '-') %>
+ <%= "+31(0)1012341234".phony_formatted(:format => :international, :spaces => '-') %>
+
+To first normalize the String to a certain country use:
+
+ <%= "010-12341234".phony_formatted(normalize => :NL, :format => :international, :spaces => '-') %>
+
+You can also use the bang method (phony_formatted!):
+
+ number = "010-12341234"
+ number.phony_formatted!(normalize => :NL, :format => :international)
+ number # => "+31 10 12341234"
### Find by normalized number
@@ -1,4 +1,3 @@
-
fr:
activerecord:
errors:
@@ -4,11 +4,18 @@ class String
# This enables:
# "31612341234".phony_formatted # => '06 12341234'
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
+ # To first normalize a String use:
+ # "010-12341234".phony_formatted(:normalize => :NL)
def phony_formatted(options = {})
- normalized = PhonyRails.normalize_number(self)
- if normalized
- Phony.formatted(normalized, options.reverse_merge(:format => :national))
- end
+ normalize_country_code = options.delete(:normalize)
+ s = (normalize_country_code ? PhonyRails.normalize_number(self, :country_code => normalize_country_code.to_s) : self.gsub(/\D/, ''))
+ return if s.blank?
+ Phony.formatted(s, options.reverse_merge(:format => :national))
+ end
+
+ # The bang method
+ def phony_formatted!(options = {})
+ replace(self.phony_formatted(options))
end
end
@@ -1,3 +1,3 @@
module PhonyRails
- VERSION = "0.1.12"
+ VERSION = "0.2.0"
end
@@ -5,13 +5,57 @@
should_not be_const_defined "Country"
end
- describe 'String extensions' do
- it "should phony_format a String" do
- "31101234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
+ describe 'phony_format String extension' do
+
+ describe 'the bang method phony_formatted!' do
+
+ it "should change the String using the bang method" do
+ s = "0101234123"
+ s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
+ s.should eql("+31 10 1234123")
+ end
+
+ end
+
+ describe 'with normalize option' do
+
+ it "should phony_format" do
+ "0101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
+ "0101234123".phony_formatted(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
+ end
+
+ it "should not change original String" do
+ s = "0101234123"
+ s.phony_formatted(:normalize => :NL).should eql('010 1234123')
+ s.should eql("0101234123")
+ end
+
+ it "should phony_format String with country code" do
+ "31101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
+ end
+
+ it "should phony_format String with country code" do
+ "31101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
+ end
+
+ it "should accept strings with non-digits in it" do
+ "+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-').should eql('+31-10-1234123')
+ end
+
+ end
+
+ it "should not change original String" do
+ s = "0101234123"
+ s.phony_formatted(:normalize => :NL).should eql('010 1234123')
+ s.should eql("0101234123")
+ end
+
+ it "should phony_format a digits string with spaces String" do
+ "31 10 1234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
end
- it "should accept strings with non-digits in it" do
- "+31-10-1234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
+ it "should phony_format a digits String" do
+ "31101234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
end
it "returns nil if implausible phone" do

0 comments on commit 542d683

Please sign in to comment.