phony_rails 0.14.5 → 0.14.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e5820adc5e9ecff0a12904b4b8622784bcc13bf
4
- data.tar.gz: 276760afba65090f145ac77fdd6e8bf28a8abf20
3
+ metadata.gz: 5286e498c6adb68558671f8441c33298b4f21ceb
4
+ data.tar.gz: 43c4bac232facb55347ce9ea716fac1a9c0d98ec
5
5
  SHA512:
6
- metadata.gz: a80c625610f3956da0b2841d78f6c6faffe5b66f8b1e24e74024a3071a63f450d221b984bf2d5cd80d3d052b409b9cfe2187bb0cb3536d5d5bf2c080eb7dd133
7
- data.tar.gz: 048f69f27476c6590f8ebbef288ce38937821a1b06ef7de7b496e372fa086eb81710f3d864ea662e7cde442693af6efa50f17b0edd85a7a06a1a7531aa4b7f1b
6
+ metadata.gz: b467fdaa24310f98b25e1a8a6734f7e13c891c8eaa326d8911cee36f7428bd18ad8e6441aa5304c8ce2b6b4cc9ae6d8a45bfb426b83029237c2afeeba1a6c41a
7
+ data.tar.gz: e93e196d750c260dea3028206070e2fddfe5c7b57b5bf26bd8b487ec73dca41848743813d0d2c669cf039eae836bf061a6252c60a833d6b807faac670687189d
data/Gemfile.lock CHANGED
@@ -1,40 +1,40 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.14.5)
4
+ phony_rails (0.14.6)
5
5
  activesupport (>= 3.0)
6
- phony (~> 2.15)
6
+ phony (> 2.15)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (5.0.0.1)
12
- activesupport (= 5.0.0.1)
13
- activerecord (5.0.0.1)
14
- activemodel (= 5.0.0.1)
15
- activesupport (= 5.0.0.1)
16
- arel (~> 7.0)
17
- activesupport (5.0.0.1)
11
+ activemodel (5.1.1)
12
+ activesupport (= 5.1.1)
13
+ activerecord (5.1.1)
14
+ activemodel (= 5.1.1)
15
+ activesupport (= 5.1.1)
16
+ arel (~> 8.0)
17
+ activesupport (5.1.1)
18
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
19
  i18n (~> 0.7)
20
20
  minitest (~> 5.1)
21
21
  tzinfo (~> 1.1)
22
- arel (7.1.4)
22
+ arel (8.0.0)
23
23
  ast (2.3.0)
24
- bson (4.1.1)
24
+ bson (4.2.1)
25
25
  coderay (1.1.1)
26
- concurrent-ruby (1.0.2)
27
- coveralls (0.8.15)
26
+ concurrent-ruby (1.0.5)
27
+ coveralls (0.8.21)
28
28
  json (>= 1.8, < 3)
29
- simplecov (~> 0.12.0)
29
+ simplecov (~> 0.14.1)
30
30
  term-ansicolor (~> 1.3)
31
- thor (~> 0.19.1)
32
- tins (>= 1.6.0, < 2)
33
- diff-lcs (1.2.5)
31
+ thor (~> 0.19.4)
32
+ tins (~> 1.6)
33
+ diff-lcs (1.3)
34
34
  docile (1.1.5)
35
- ffi (1.9.14)
35
+ ffi (1.9.18)
36
36
  formatador (0.2.5)
37
- guard (2.14.0)
37
+ guard (2.14.1)
38
38
  formatador (>= 0.2.4)
39
39
  listen (>= 2.7, < 4.0)
40
40
  lumberjack (~> 1.0)
@@ -52,52 +52,55 @@ GEM
52
52
  guard (~> 2.1)
53
53
  guard-compat (~> 1.1)
54
54
  rspec (>= 2.99.0, < 4.0)
55
- i18n (0.7.0)
56
- json (2.0.2)
55
+ i18n (0.8.4)
56
+ json (2.1.0)
57
57
  listen (3.1.5)
58
58
  rb-fsevent (~> 0.9, >= 0.9.4)
59
59
  rb-inotify (~> 0.9, >= 0.9.7)
60
60
  ruby_dep (~> 1.2)
61
- lumberjack (1.0.10)
61
+ lumberjack (1.0.12)
62
62
  method_source (0.8.2)
63
- minitest (5.9.1)
64
- mongo (2.3.0)
65
- bson (~> 4.1)
66
- mongoid (6.0.0)
67
- activemodel (~> 5.0)
68
- mongo (~> 2.3)
63
+ minitest (5.10.2)
64
+ mongo (2.4.2)
65
+ bson (>= 4.2.1, < 5.0.0)
66
+ mongoid (6.2.0)
67
+ activemodel (~> 5.1)
68
+ mongo (>= 2.4.1, < 3.0.0)
69
69
  nenv (0.3.0)
70
70
  notiffany (0.1.1)
71
71
  nenv (~> 0.1)
72
72
  shellany (~> 0.0)
73
- parser (2.3.1.4)
73
+ parallel (1.11.2)
74
+ parser (2.4.0.0)
74
75
  ast (~> 2.2)
75
- phony (2.15.32)
76
+ phony (2.15.44)
76
77
  powerpack (0.1.1)
77
78
  pry (0.10.4)
78
79
  coderay (~> 1.1.0)
79
80
  method_source (~> 0.8.1)
80
81
  slop (~> 3.4)
81
- rainbow (2.1.0)
82
- rake (11.3.0)
83
- rb-fsevent (0.9.7)
84
- rb-inotify (0.9.7)
85
- ffi (>= 0.5.0)
86
- rspec (3.5.0)
87
- rspec-core (~> 3.5.0)
88
- rspec-expectations (~> 3.5.0)
89
- rspec-mocks (~> 3.5.0)
90
- rspec-core (3.5.4)
91
- rspec-support (~> 3.5.0)
92
- rspec-expectations (3.5.0)
82
+ rainbow (2.2.2)
83
+ rake
84
+ rake (12.0.0)
85
+ rb-fsevent (0.9.8)
86
+ rb-inotify (0.9.10)
87
+ ffi (>= 0.5.0, < 2)
88
+ rspec (3.6.0)
89
+ rspec-core (~> 3.6.0)
90
+ rspec-expectations (~> 3.6.0)
91
+ rspec-mocks (~> 3.6.0)
92
+ rspec-core (3.6.0)
93
+ rspec-support (~> 3.6.0)
94
+ rspec-expectations (3.6.0)
93
95
  diff-lcs (>= 1.2.0, < 2.0)
94
- rspec-support (~> 3.5.0)
95
- rspec-mocks (3.5.0)
96
+ rspec-support (~> 3.6.0)
97
+ rspec-mocks (3.6.0)
96
98
  diff-lcs (>= 1.2.0, < 2.0)
97
- rspec-support (~> 3.5.0)
98
- rspec-support (3.5.0)
99
- rubocop (0.43.0)
100
- parser (>= 2.3.1.1, < 3.0)
99
+ rspec-support (~> 3.6.0)
100
+ rspec-support (3.6.0)
101
+ rubocop (0.49.1)
102
+ parallel (~> 1.10)
103
+ parser (>= 2.3.3.1, < 3.0)
101
104
  powerpack (~> 0.1)
102
105
  rainbow (>= 1.99.1, < 3.0)
103
106
  ruby-progressbar (~> 1.7)
@@ -105,21 +108,21 @@ GEM
105
108
  ruby-progressbar (1.8.1)
106
109
  ruby_dep (1.5.0)
107
110
  shellany (0.0.1)
108
- simplecov (0.12.0)
111
+ simplecov (0.14.1)
109
112
  docile (~> 1.1.0)
110
113
  json (>= 1.8, < 3)
111
114
  simplecov-html (~> 0.10.0)
112
- simplecov-html (0.10.0)
115
+ simplecov-html (0.10.1)
113
116
  slop (3.6.0)
114
- sqlite3 (1.3.12)
115
- term-ansicolor (1.4.0)
117
+ sqlite3 (1.3.13)
118
+ term-ansicolor (1.6.0)
116
119
  tins (~> 1.0)
117
- thor (0.19.1)
118
- thread_safe (0.3.5)
119
- tins (1.12.0)
120
- tzinfo (1.2.2)
120
+ thor (0.19.4)
121
+ thread_safe (0.3.6)
122
+ tins (1.14.0)
123
+ tzinfo (1.2.3)
121
124
  thread_safe (~> 0.1)
122
- unicode-display_width (1.1.1)
125
+ unicode-display_width (1.2.1)
123
126
 
124
127
  PLATFORMS
125
128
  ruby
@@ -138,4 +141,4 @@ DEPENDENCIES
138
141
  sqlite3
139
142
 
140
143
  BUNDLED WITH
141
- 1.12.5
144
+ 1.13.5
data/README.md CHANGED
@@ -43,6 +43,9 @@ class SomeModel < ActiveRecord::Base
43
43
 
44
44
  # Creates method normalized_fax_number that returns the normalized version of fax_number
45
45
  phony_normalized_method :fax_number
46
+
47
+ # Conditionally normalizes the attribute
48
+ phony_normalize :recipient, default_country_code: 'US', if: -> { contact_method == 'phone_number' }
46
49
  end
47
50
  ```
48
51
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module PhonyRails
3
- VERSION = '0.14.5'.freeze
3
+ VERSION = '0.14.6'.freeze
4
4
  end
data/lib/phony_rails.rb CHANGED
@@ -110,13 +110,8 @@ module PhonyRails
110
110
 
111
111
  def self.extract_extension(number_and_ext)
112
112
  return [nil, nil] if number_and_ext.nil?
113
- # :nocov:
114
- if subbed = number_and_ext.sub(COMMON_EXTENSIONS, '')
115
- [subbed, Regexp.last_match(2)]
116
- else
117
- [number_and_ext, nil]
118
- end
119
- # :nocov:
113
+ subbed = number_and_ext.sub(COMMON_EXTENSIONS, '')
114
+ [subbed, Regexp.last_match(2)]
120
115
  end
121
116
 
122
117
  def self.format_extension(number, ext)
@@ -162,15 +157,17 @@ module PhonyRails
162
157
  # you've geocoded before calling this method!
163
158
  def phony_normalize(*attributes)
164
159
  options = attributes.last.is_a?(Hash) ? attributes.pop : {}
165
- options.assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country
160
+ options.assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country, :if, :unless
166
161
  if options[:as].present?
167
162
  raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1
168
163
  end
169
164
 
170
165
  options[:enforce_record_country] = true if options[:enforce_record_country].nil?
171
166
 
167
+ conditional = create_before_validation_conditional_hash(options)
168
+
172
169
  # Add before validation that saves a normalized version of the phone number
173
- before_validation do
170
+ before_validation conditional do
174
171
  set_phony_normalized_numbers(attributes, options)
175
172
  end
176
173
  end
@@ -192,12 +189,45 @@ module PhonyRails
192
189
  end
193
190
  end
194
191
  end
192
+
193
+ private
194
+
195
+ # Creates a hash representing a conditional for before_validation
196
+ # This allows conditional normalization
197
+ # Returns something like `{ unless: -> { attribute == 'something' } }`
198
+ # If no if/unless options passed in, returns `{ if: -> { true } }`
199
+ def create_before_validation_conditional_hash(options)
200
+ if options[:if].present?
201
+ type = :if
202
+ source = options[:if]
203
+ elsif options[:unless].present?
204
+ type = :unless
205
+ source = options[:unless]
206
+ else
207
+ type = :if
208
+ source = true
209
+ end
210
+
211
+ conditional = {}
212
+ conditional[type] = if source.respond_to?(:call)
213
+ source
214
+ elsif source.respond_to?(:to_sym)
215
+ -> { send(source.to_sym) }
216
+ else
217
+ -> { source }
218
+ end
219
+ conditional
220
+ end
195
221
  end
196
222
  end
197
223
  end
198
224
 
199
225
  # check whether it is ActiveRecord or Mongoid being used
200
- ActiveRecord::Base.send :include, PhonyRails::Extension if defined?(ActiveRecord)
226
+ if defined?(ActiveRecord)
227
+ ActiveSupport.on_load(:active_record) do
228
+ ActiveRecord::Base.send :include, PhonyRails::Extension
229
+ end
230
+ end
201
231
 
202
232
  ActiveModel::Model.send :include, PhonyRails::Extension if defined?(ActiveModel::Model)
203
233
 
data/phony_rails.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.post_install_message = 'PhonyRails v0.10.0 changes the way numbers are stored!'
21
21
  gem.post_install_message = "It now adds a '+' to the normalized number when it starts with a country number!"
22
22
 
23
- gem.add_runtime_dependency 'phony', '~> 2.15'
23
+ gem.add_runtime_dependency 'phony', '> 2.15'
24
24
  gem.add_runtime_dependency 'activesupport', '>= 3.0'
25
25
  gem.add_development_dependency 'activerecord', '>= 3.0'
26
26
  gem.add_development_dependency 'mongoid', '>= 3.0'
@@ -702,6 +702,56 @@ describe PhonyRails do
702
702
  expect(model).to be_valid
703
703
  expect(model.symboled_phone).to eql('+33606060606')
704
704
  end
705
+
706
+ context 'conditional normalization' do
707
+ context 'standalone methods' do
708
+ it 'should only normalize if the :if conditional is true' do
709
+ model_klass.phony_normalize :recipient, default_country_code: 'US', if: :use_phone?
710
+
711
+ sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms'
712
+ email_alarm = model_klass.new recipient: 'foo123@example.com', delivery_method: 'email'
713
+ expect(sms_alarm).to be_valid
714
+ expect(email_alarm).to be_valid
715
+ expect(sms_alarm.recipient).to eq('+12223334444')
716
+ expect(email_alarm.recipient).to eq('foo123@example.com')
717
+ end
718
+
719
+ it 'should only normalize if the :unless conditional is false' do
720
+ model_klass.phony_normalize :recipient, default_country_code: 'US', unless: :use_email?
721
+
722
+ sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms'
723
+ email_alarm = model_klass.new recipient: 'foo123@example.com', delivery_method: 'email'
724
+ expect(sms_alarm).to be_valid
725
+ expect(email_alarm).to be_valid
726
+ expect(sms_alarm.recipient).to eq('+12223334444')
727
+ expect(email_alarm.recipient).to eq('foo123@example.com')
728
+ end
729
+ end
730
+
731
+ context 'using lambdas' do
732
+ it 'should only normalize if the :if conditional is true' do
733
+ model_klass.phony_normalize :recipient, default_country_code: 'US', if: -> { delivery_method == 'sms' }
734
+
735
+ sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms'
736
+ email_alarm = model_klass.new recipient: 'foo123@example.com', delivery_method: 'email'
737
+ expect(sms_alarm).to be_valid
738
+ expect(email_alarm).to be_valid
739
+ expect(sms_alarm.recipient).to eq('+12223334444')
740
+ expect(email_alarm.recipient).to eq('foo123@example.com')
741
+ end
742
+
743
+ it 'should only normalize if the :unless conditional is false' do
744
+ model_klass.phony_normalize :recipient, default_country_code: 'US', unless: -> { delivery_method == 'email' }
745
+
746
+ sms_alarm = model_klass.new recipient: '222 333 4444', delivery_method: 'sms'
747
+ email_alarm = model_klass.new recipient: 'foo123@example.com', delivery_method: 'email'
748
+ expect(sms_alarm).to be_valid
749
+ expect(email_alarm).to be_valid
750
+ expect(sms_alarm.recipient).to eq('+12223334444')
751
+ expect(email_alarm.recipient).to eq('foo123@example.com')
752
+ end
753
+ end
754
+ end
705
755
  end
706
756
  end
707
757
 
data/spec/spec_helper.rb CHANGED
@@ -30,7 +30,7 @@ end
30
30
  module SharedModelMethods
31
31
  extend ActiveSupport::Concern
32
32
  included do
33
- attr_accessor :phone_method, :phone1_method, :symboled_phone_method, :country_code, :country_code_attribute
33
+ attr_accessor :phone_method, :phone1_method, :symboled_phone_method, :country_code, :country_code_attribute, :recipient, :delivery_method
34
34
  phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
35
35
  phony_normalized_method :phone_method # adds normalized_phone_method method
36
36
  phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
@@ -38,6 +38,14 @@ module SharedModelMethods
38
38
  phony_normalize :phone_number # normalized on validation
39
39
  phony_normalize :fax_number, default_country_code: 'AU'
40
40
  phony_normalize :symboled_phone, default_country_code: :country_code_attribute
41
+
42
+ def use_phone?
43
+ delivery_method == 'sms'
44
+ end
45
+
46
+ def use_email?
47
+ delivery_method == 'email'
48
+ end
41
49
  end
42
50
  end
43
51
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.5
4
+ version: 0.14.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joost Hietbrink
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-08 00:00:00.000000000 Z
11
+ date: 2017-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phony
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.15'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.15'
27
27
  - !ruby/object:Gem::Dependency