phony_rails 0.14.7 → 0.14.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +3 -0
- data/.travis.yml +3 -2
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +4 -4
- data/README.md +2 -2
- data/lib/phony_rails.rb +13 -8
- data/lib/phony_rails/locales/es.yml +4 -0
- data/lib/phony_rails/version.rb +1 -1
- data/phony_rails.gemspec +1 -1
- data/spec/lib/phony_rails_spec.rb +31 -1
- data/spec/lib/validators/phony_validator_spec.rb +8 -0
- data/spec/spec_helper.rb +4 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7e279baa79785d7c0db788f230299a530f95900
|
4
|
+
data.tar.gz: 2b99f584e90f49369cd5114aac04e6b5c09f198b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ae91b311ac825d4a8fe61b44d7b74ccfa515ab2702c017c5bcf32774b00fcd622ac29c1fea2da0b962643e848b69dcbef5a8871f5b3c92a1db87ff2cb8dc93b
|
7
|
+
data.tar.gz: d3cdc2fdc49cabb7562b4916e6b56bd8eaf1b85c2d71b93813cda6dfb025a4538bc564115f30a2bde638717f682198915e38aae38258cfab5a297363e5d5551f
|
data/.rubocop_todo.yml
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## [v0.14.7](https://github.com/joost/phony_rails/tree/v0.14.7) (2018-05-25)
|
4
|
+
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.14.6...v0.14.7)
|
5
|
+
|
6
|
+
**Closed issues:**
|
7
|
+
|
8
|
+
- Country code wrapped in brackets is not recognized [\#170](https://github.com/joost/phony_rails/issues/170)
|
9
|
+
- Invalid number in countryCode VN [\#159](https://github.com/joost/phony_rails/issues/159)
|
10
|
+
|
11
|
+
**Merged pull requests:**
|
12
|
+
|
13
|
+
- Extension option added to normalize\_number [\#176](https://github.com/joost/phony_rails/pull/176) ([ramaboo](https://github.com/ramaboo))
|
14
|
+
- return country from phone number [\#169](https://github.com/joost/phony_rails/pull/169) ([aovertus](https://github.com/aovertus))
|
15
|
+
- Fix code example in README to match description [\#167](https://github.com/joost/phony_rails/pull/167) ([mattruzicka](https://github.com/mattruzicka))
|
16
|
+
|
3
17
|
## [v0.14.6](https://github.com/joost/phony_rails/tree/v0.14.6) (2017-06-20)
|
4
18
|
[Full Changelog](https://github.com/joost/phony_rails/compare/v0.14.5...v0.14.6)
|
5
19
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
phony_rails (0.14.
|
4
|
+
phony_rails (0.14.9)
|
5
5
|
activesupport (>= 3.0)
|
6
6
|
phony (> 2.15)
|
7
7
|
|
@@ -32,7 +32,7 @@ GEM
|
|
32
32
|
tins (~> 1.6)
|
33
33
|
diff-lcs (1.3)
|
34
34
|
docile (1.1.5)
|
35
|
-
ffi (1.9.
|
35
|
+
ffi (1.9.25)
|
36
36
|
formatador (0.2.5)
|
37
37
|
guard (2.14.1)
|
38
38
|
formatador (>= 0.2.4)
|
@@ -74,7 +74,7 @@ GEM
|
|
74
74
|
parallel (1.12.0)
|
75
75
|
parser (2.4.0.2)
|
76
76
|
ast (~> 2.3)
|
77
|
-
phony (2.16.
|
77
|
+
phony (2.16.5)
|
78
78
|
powerpack (0.1.1)
|
79
79
|
pry (0.11.3)
|
80
80
|
coderay (~> 1.1.0)
|
@@ -140,4 +140,4 @@ DEPENDENCIES
|
|
140
140
|
sqlite3
|
141
141
|
|
142
142
|
BUNDLED WITH
|
143
|
-
1.16.
|
143
|
+
1.16.4
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# PhonyRails [](https://travis-ci.org/joost/phony_rails) [](https://coveralls.io/r/joost/phony_rails) ](https://travis-ci.org/joost/phony_rails) [](https://coveralls.io/r/joost/phony_rails) 
|
2
2
|
|
3
3
|
This small Gem adds useful methods to your Rails app to validate, display and save phone numbers.
|
4
4
|
It uses the super awesome Phony gem (https://github.com/floere/phony).
|
@@ -133,7 +133,7 @@ validates_plausible_phone :phone_number, without: /\A\+\d+/
|
|
133
133
|
validates_plausible_phone :phone_number, presence: true, with: /\A\+\d+/
|
134
134
|
```
|
135
135
|
|
136
|
-
the i18n key is `:improbable_phone`. Languages supported by default: de, en, fr, it, ja, kh, nl, tr, ua and ru.
|
136
|
+
the i18n key is `:improbable_phone`. Languages supported by default: de, en, es, fr, it, ja, kh, nl, tr, ua and ru.
|
137
137
|
|
138
138
|
You can also validate if a number has the correct country number:
|
139
139
|
|
data/lib/phony_rails.rb
CHANGED
@@ -44,7 +44,7 @@ module PhonyRails
|
|
44
44
|
# :extension => Include the extension. (default: true)
|
45
45
|
# This idea came from:
|
46
46
|
# http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
|
47
|
-
def self.normalize_number(number, options = {})
|
47
|
+
def self.normalize_number(number, options = {}, current_instance = nil)
|
48
48
|
return if number.nil?
|
49
49
|
original_number = number
|
50
50
|
number = number.dup # Just to be sure, we don't want to change the original.
|
@@ -58,7 +58,7 @@ module PhonyRails
|
|
58
58
|
if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
|
59
59
|
number = "#{_country_number}#{number}"
|
60
60
|
end
|
61
|
-
elsif _default_country_number = extract_default_country_number(options)
|
61
|
+
elsif _default_country_number = extract_default_country_number(options, current_instance)
|
62
62
|
options[:add_plus] = true if options[:add_plus].nil?
|
63
63
|
number = normalize_number_default_country(number, _default_country_number)
|
64
64
|
end
|
@@ -76,7 +76,7 @@ module PhonyRails
|
|
76
76
|
def self.normalize_number_default_country(number, default_country_number)
|
77
77
|
# We try to add the default country number and see if it is a
|
78
78
|
# correct phone number. See https://github.com/joost/phony_rails/issues/87#issuecomment-89324426
|
79
|
-
unless number =~ /\A
|
79
|
+
unless number =~ /\A\(?\+/ # if we don't have a +
|
80
80
|
return "#{default_country_number}#{number}" if Phony.plausible?("#{default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil?
|
81
81
|
# If the number starts with ONE zero (two might indicate a country code)
|
82
82
|
# and this is a plausible number for the default_country
|
@@ -88,8 +88,13 @@ module PhonyRails
|
|
88
88
|
number
|
89
89
|
end
|
90
90
|
|
91
|
-
def self.extract_default_country_number(options = {})
|
92
|
-
|
91
|
+
def self.extract_default_country_number(options = {}, current_instance = nil)
|
92
|
+
country_code = if current_instance.present? && options[:default_country_code].respond_to?(:call)
|
93
|
+
options[:default_country_code].call(current_instance) rescue nil
|
94
|
+
else
|
95
|
+
options[:default_country_code]
|
96
|
+
end
|
97
|
+
options[:default_country_number] || country_number_for(country_code) || default_country_number
|
93
98
|
end
|
94
99
|
|
95
100
|
def self.country_code_from_number(number)
|
@@ -136,7 +141,7 @@ module PhonyRails
|
|
136
141
|
|
137
142
|
# This methods sets the attribute to the normalized version.
|
138
143
|
# It also adds the country_code (number), eg. 31 for NL numbers.
|
139
|
-
def set_phony_normalized_numbers(attributes, options = {})
|
144
|
+
def set_phony_normalized_numbers(current_instance, attributes, options = {})
|
140
145
|
options = options.dup
|
141
146
|
assign_values_for_phony_symbol_options(options)
|
142
147
|
if respond_to?(:country_code)
|
@@ -146,7 +151,7 @@ module PhonyRails
|
|
146
151
|
attributes.each do |attribute|
|
147
152
|
attribute_name = options[:as] || attribute
|
148
153
|
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
|
149
|
-
new_value = PhonyRails.normalize_number(send(attribute), options)
|
154
|
+
new_value = PhonyRails.normalize_number(send(attribute), options, current_instance)
|
150
155
|
send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
|
151
156
|
end
|
152
157
|
end
|
@@ -178,7 +183,7 @@ module PhonyRails
|
|
178
183
|
|
179
184
|
# Add before validation that saves a normalized version of the phone number
|
180
185
|
before_validation conditional do
|
181
|
-
set_phony_normalized_numbers(attributes, options)
|
186
|
+
set_phony_normalized_numbers(self, attributes, options)
|
182
187
|
end
|
183
188
|
end
|
184
189
|
|
data/lib/phony_rails/version.rb
CHANGED
data/phony_rails.gemspec
CHANGED
@@ -20,7 +20,7 @@ describe PhonyRails do
|
|
20
20
|
describe 'with the bang!' do
|
21
21
|
it 'changes the String using the bang method' do
|
22
22
|
# Mutable String
|
23
|
-
s = +'0101234123' rescue '0101234123'
|
23
|
+
s = +'0101234123' rescue '0101234123'
|
24
24
|
expect(s.phony_formatted!(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
|
25
25
|
expect(s).to eql('+31 10 123 4123')
|
26
26
|
end
|
@@ -218,6 +218,12 @@ describe PhonyRails do
|
|
218
218
|
phone = PhonyRails.normalize_number(phone)
|
219
219
|
expect(phone).to eq('+491751234567')
|
220
220
|
end
|
221
|
+
|
222
|
+
it 'should pass Github issue #170 (part 2)' do
|
223
|
+
phone = '(+49) 175 123 4567'
|
224
|
+
phone = PhonyRails.normalize_number(phone, default_country_code: 'DE')
|
225
|
+
expect(phone).to eq('+491751234567')
|
226
|
+
end
|
221
227
|
end
|
222
228
|
|
223
229
|
it 'should not change original String' do
|
@@ -722,6 +728,30 @@ describe PhonyRails do
|
|
722
728
|
expect(model.symboled_phone).to eql('+33606060606')
|
723
729
|
end
|
724
730
|
|
731
|
+
context 'relational normalization' do
|
732
|
+
it 'should normalize based on custom attribute of the current model' do
|
733
|
+
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.custom_country_code }
|
734
|
+
model = model_klass.new phone_number: '012 416 0001', custom_country_code: 'MY'
|
735
|
+
expect(model).to be_valid
|
736
|
+
expect(model.phone_number).to eq('+60124160001')
|
737
|
+
end
|
738
|
+
|
739
|
+
it 'should normalize based on specific attribute of the associative model' do
|
740
|
+
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country.country_code }
|
741
|
+
home_country = double('home_country', country_code: 'MY')
|
742
|
+
model = model_klass.new phone_number: '012 416 0001', home_country: home_country
|
743
|
+
expect(model).to be_valid
|
744
|
+
expect(model.phone_number).to eq('+60124160001')
|
745
|
+
end
|
746
|
+
|
747
|
+
it 'should normalize based on default value if missing associative model' do
|
748
|
+
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country&.country_code || 'MY' }
|
749
|
+
model = model_klass.new phone_number: '012 416 0001', home_country: nil
|
750
|
+
expect(model).to be_valid
|
751
|
+
expect(model.phone_number).to eq('+60124160001')
|
752
|
+
end
|
753
|
+
end
|
754
|
+
|
725
755
|
context 'conditional normalization' do
|
726
756
|
context 'standalone methods' do
|
727
757
|
it 'should only normalize if the :if conditional is true' do
|
@@ -240,6 +240,14 @@ describe PhonyPlausibleValidator do
|
|
240
240
|
end
|
241
241
|
end
|
242
242
|
|
243
|
+
it 'should translate the error message in Spanish' do
|
244
|
+
I18n.with_locale(:es) do
|
245
|
+
@home.phone_number = INVALID_NUMBER
|
246
|
+
@home.valid?
|
247
|
+
expect(@home.errors.messages).to include(phone_number: ['es un número inválido'])
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
243
251
|
it 'should translate the error message in French' do
|
244
252
|
I18n.with_locale(:fr) do
|
245
253
|
@home.phone_number = INVALID_NUMBER
|
data/spec/spec_helper.rb
CHANGED
@@ -31,7 +31,10 @@ end
|
|
31
31
|
module SharedModelMethods
|
32
32
|
extend ActiveSupport::Concern
|
33
33
|
included do
|
34
|
-
attr_accessor
|
34
|
+
attr_accessor(
|
35
|
+
:country_code, :country_code_attribute, :custom_country_code, :delivery_method,
|
36
|
+
:home_country, :phone_method, :phone1_method, :recipient, :symboled_phone_method
|
37
|
+
)
|
35
38
|
phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
|
36
39
|
phony_normalized_method :phone_method # adds normalized_phone_method method
|
37
40
|
phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.14.
|
4
|
+
version: 0.14.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joost Hietbrink
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05
|
11
|
+
date: 2018-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/phony_rails.rb
|
91
91
|
- lib/phony_rails/locales/de.yml
|
92
92
|
- lib/phony_rails/locales/en.yml
|
93
|
+
- lib/phony_rails/locales/es.yml
|
93
94
|
- lib/phony_rails/locales/fr.yml
|
94
95
|
- lib/phony_rails/locales/he.yml
|
95
96
|
- lib/phony_rails/locales/it.yml
|