phony_rails 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +10 -1
- data/README.md +14 -2
- data/lib/phony_rails.rb +9 -16
- data/lib/phony_rails/locales/en.yml +4 -1
- data/lib/phony_rails/version.rb +1 -1
- data/phony_rails.gemspec +3 -1
- data/spec/lib/phony_rails_spec.rb +125 -112
- data/spec/spec_helper.rb +31 -12
- metadata +35 -3
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
phony_rails (0.3.2)
|
5
|
-
|
5
|
+
activesupport (>= 3.0)
|
6
6
|
countries (>= 0.8.2)
|
7
7
|
phony (>= 1.7.7)
|
8
8
|
|
@@ -41,7 +41,14 @@ GEM
|
|
41
41
|
rb-fchange (~> 0.0.5)
|
42
42
|
rb-fsevent (~> 0.9.1)
|
43
43
|
rb-inotify (~> 0.8.8)
|
44
|
+
mongoid (3.1.4)
|
45
|
+
activemodel (~> 3.2)
|
46
|
+
moped (~> 1.4)
|
47
|
+
origin (~> 1.0)
|
48
|
+
tzinfo (~> 0.3.22)
|
49
|
+
moped (1.5.0)
|
44
50
|
multi_json (1.7.2)
|
51
|
+
origin (1.1.0)
|
45
52
|
phony (1.9.0)
|
46
53
|
rake (10.0.3)
|
47
54
|
rb-fchange (0.0.5)
|
@@ -65,11 +72,13 @@ PLATFORMS
|
|
65
72
|
ruby
|
66
73
|
|
67
74
|
DEPENDENCIES
|
75
|
+
activerecord (>= 3.0)
|
68
76
|
countries
|
69
77
|
growl
|
70
78
|
guard (~> 1.2.0)
|
71
79
|
guard-bundler (~> 1.0.0)
|
72
80
|
guard-rspec (~> 1.2.0)
|
81
|
+
mongoid (>= 3.0)
|
73
82
|
phony_rails!
|
74
83
|
rake
|
75
84
|
rb-fsevent
|
data/README.md
CHANGED
@@ -21,9 +21,9 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
### Normalization /
|
24
|
+
### Normalization / Model Usage
|
25
25
|
|
26
|
-
|
26
|
+
For **ActiveRecord**, in your model add:
|
27
27
|
|
28
28
|
class SomeModel < ActiveRecord::Base
|
29
29
|
|
@@ -37,6 +37,15 @@ In your model add:
|
|
37
37
|
phony_normalized_method :fax_number
|
38
38
|
end
|
39
39
|
|
40
|
+
For **Mongoid**, in keeping with Mongoid plug-in conventions you must include the `Mongoid::Phony` module:
|
41
|
+
|
42
|
+
class SomeModel
|
43
|
+
include Mongoid::Document
|
44
|
+
include Mongoid::Phony
|
45
|
+
|
46
|
+
# methods are same as ActiveRecord usage
|
47
|
+
end
|
48
|
+
|
40
49
|
The `:default_country_code` options is used to specify a country_code when normalizing.
|
41
50
|
|
42
51
|
PhonyRails will also check your model for a country_code method to use when normalizing the number. So `'070-12341234'` with `country_code` 'NL' will get normalized to `'317012341234'`.
|
@@ -101,6 +110,9 @@ Say you want to find a record by a phone number. Best is to normalize user input
|
|
101
110
|
|
102
111
|
## Changelog
|
103
112
|
|
113
|
+
0.4.0
|
114
|
+
* Better Mongoid support by @johnnyshields
|
115
|
+
|
104
116
|
0.3.0
|
105
117
|
* Now ability to force change a country_code.
|
106
118
|
See: https://github.com/joost/phony_rails/pull/23#issuecomment-17480463
|
data/lib/phony_rails.rb
CHANGED
@@ -37,14 +37,9 @@ module PhonyRails
|
|
37
37
|
end
|
38
38
|
|
39
39
|
module Extension
|
40
|
+
extend ActiveSupport::Concern
|
40
41
|
|
41
|
-
|
42
|
-
base.send :include, InstanceMethods
|
43
|
-
base.extend ClassMethods
|
44
|
-
end
|
45
|
-
|
46
|
-
module InstanceMethods
|
47
|
-
|
42
|
+
included do
|
48
43
|
private
|
49
44
|
|
50
45
|
# This methods sets the attribute to the normalized version.
|
@@ -58,7 +53,6 @@ module PhonyRails
|
|
58
53
|
write_attribute(attribute_name, PhonyRails.normalize_number(read_attribute(attribute), options))
|
59
54
|
end
|
60
55
|
end
|
61
|
-
|
62
56
|
end
|
63
57
|
|
64
58
|
module ClassMethods
|
@@ -97,20 +91,19 @@ module PhonyRails
|
|
97
91
|
end
|
98
92
|
end
|
99
93
|
end
|
100
|
-
|
101
94
|
end
|
102
|
-
|
103
95
|
end
|
104
|
-
|
105
96
|
end
|
106
97
|
|
107
98
|
# check whether it is ActiveRecord or Mongoid being used
|
108
|
-
ActiveRecord::Base.send :
|
109
|
-
|
110
|
-
|
111
|
-
|
99
|
+
ActiveRecord::Base.send :include, PhonyRails::Extension if defined?(ActiveRecord)
|
100
|
+
|
101
|
+
if defined?(Mongoid)
|
102
|
+
module Mongoid::Phony
|
103
|
+
extend ActiveSupport::Concern
|
104
|
+
include PhonyRails::Extension
|
112
105
|
end
|
113
|
-
end
|
106
|
+
end
|
114
107
|
|
115
108
|
Dir["#{File.dirname(__FILE__)}/phony_rails/locales/*.yml"].each do |file|
|
116
109
|
I18n.load_path << file
|
data/lib/phony_rails/version.rb
CHANGED
data/phony_rails.gemspec
CHANGED
@@ -17,5 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
|
18
18
|
gem.add_dependency "phony", ">= 1.7.7"
|
19
19
|
gem.add_dependency "countries", ">= 0.8.2"
|
20
|
-
gem.add_dependency "
|
20
|
+
gem.add_dependency "activesupport", ">= 3.0"
|
21
|
+
gem.add_development_dependency "activerecord", ">= 3.0"
|
22
|
+
gem.add_development_dependency "mongoid", ">= 3.0"
|
21
23
|
end
|
@@ -123,143 +123,156 @@ describe PhonyRails do
|
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
126
|
+
shared_examples_for 'model with PhonyRails' do
|
127
|
+
describe 'defining model#phony_normalized_method' do
|
128
|
+
it "should add a normalized_phone_attribute method" do
|
129
|
+
model_klass.new.should respond_to(:normalized_phone_attribute)
|
130
|
+
end
|
130
131
|
|
131
|
-
|
132
|
-
|
133
|
-
|
132
|
+
it "should add a normalized_phone_method method" do
|
133
|
+
model_klass.new.should respond_to(:normalized_phone_method)
|
134
|
+
end
|
134
135
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
136
|
+
it "should raise error on existing methods" do
|
137
|
+
lambda {
|
138
|
+
model_klass.phony_normalized_method(:phone_method)
|
139
|
+
}.should raise_error(StandardError)
|
140
|
+
end
|
140
141
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
142
|
+
it "should raise error on not existing attribute" do
|
143
|
+
model_klass.phony_normalized_method(:phone_non_existing_method)
|
144
|
+
lambda {
|
145
|
+
model_klass.new.normalized_phone_non_existing_method
|
146
|
+
}.should raise_error(ArgumentError)
|
147
|
+
end
|
146
148
|
end
|
147
|
-
end
|
148
149
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
150
|
+
describe 'defining model#phony_normalize' do
|
151
|
+
it "should not accept :as option with multiple attribute names" do
|
152
|
+
lambda {
|
153
|
+
model_klass.phony_normalize(:phone_number, :phone1_method, :as => 'non_existing_attribute')
|
154
|
+
}.should raise_error(ArgumentError)
|
155
|
+
end
|
155
156
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
157
|
+
it "should not accept :as option with unexisting attribute name" do
|
158
|
+
lambda {
|
159
|
+
model_klass.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute')
|
160
|
+
}.should raise_error(ArgumentError)
|
161
|
+
end
|
161
162
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
163
|
+
it "should not accept :as option with single non existing attribute name" do
|
164
|
+
lambda {
|
165
|
+
model_klass.phony_normalize(:phone_number, :as => 'something_else')
|
166
|
+
}.should raise_error(ArgumentError)
|
167
|
+
end
|
167
168
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
169
|
+
it "should accept :as option with single existing attribute name" do
|
170
|
+
lambda {
|
171
|
+
model_klass.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
|
172
|
+
}.should_not raise_error(ArgumentError)
|
173
|
+
end
|
173
174
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
175
|
+
it "should accept a non existing attribute name" do
|
176
|
+
lambda {
|
177
|
+
dummy_klass.phony_normalize(:non_existing_attribute)
|
178
|
+
}.should_not raise_error
|
179
|
+
end
|
178
180
|
end
|
179
|
-
end
|
180
181
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
182
|
+
describe 'using model#phony_normalized_method' do
|
183
|
+
# Following examples have complete number (with country code!)
|
184
|
+
it "should return a normalized version of an attribute" do
|
185
|
+
model = model_klass.new(:phone_attribute => "+31-(0)10-1234123")
|
186
|
+
model.normalized_phone_attribute.should eql('31101234123')
|
187
|
+
end
|
187
188
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
189
|
+
it "should return a normalized version of a method" do
|
190
|
+
model = model_klass.new(:phone_method => "+31-(0)10-1234123")
|
191
|
+
model.normalized_phone_method.should eql('31101234123')
|
192
|
+
end
|
192
193
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
194
|
+
# Following examples have incomplete number
|
195
|
+
it "should return nil if no country_code is known" do
|
196
|
+
model = model_klass.new(:phone_attribute => "(0)10-1234123")
|
197
|
+
model.normalized_phone_attribute.should eql('11234123') # This actually is an incorrect number! (FIXME?)
|
198
|
+
end
|
198
199
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
200
|
+
it "should use country_code option" do
|
201
|
+
model = model_klass.new(:phone_attribute => "(0)10-1234123")
|
202
|
+
model.normalized_phone_attribute(:country_code => 'NL').should eql('31101234123')
|
203
|
+
end
|
203
204
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
205
|
+
it "should use country_code object method" do
|
206
|
+
model = model_klass.new(:phone_attribute => "(0)10-1234123", :country_code => 'NL')
|
207
|
+
model.normalized_phone_attribute.should eql('31101234123')
|
208
|
+
end
|
208
209
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
210
|
+
it "should fallback to default_country_code option" do
|
211
|
+
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
212
|
+
model.normalized_phone1_method.should eql('49308612906')
|
213
|
+
end
|
213
214
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
215
|
+
it "should overwrite default_country_code option with object method" do
|
216
|
+
model = model_klass.new(:phone1_method => "(030) 8 61 29 06", :country_code => 'NL')
|
217
|
+
model.normalized_phone1_method.should eql('31308612906')
|
218
|
+
end
|
218
219
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
220
|
+
it "should overwrite default_country_code option with option" do
|
221
|
+
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
222
|
+
model.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
|
223
|
+
end
|
223
224
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
225
|
+
it "should use last passed options" do
|
226
|
+
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
227
|
+
model.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
|
228
|
+
model.normalized_phone1_method(:country_code => 'DE').should eql('49308612906')
|
229
|
+
model.normalized_phone1_method(:country_code => nil).should eql('49308612906')
|
230
|
+
end
|
230
231
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
232
|
+
it "should use last object method" do
|
233
|
+
model = model_klass.new(:phone1_method => "(030) 8 61 29 06")
|
234
|
+
model.country_code = 'NL'
|
235
|
+
model.normalized_phone1_method.should eql('31308612906')
|
236
|
+
model.country_code = 'DE'
|
237
|
+
model.normalized_phone1_method.should eql('49308612906')
|
238
|
+
model.country_code = nil
|
239
|
+
model.normalized_phone1_method(:country_code => nil).should eql('49308612906')
|
240
|
+
end
|
239
241
|
end
|
240
|
-
end
|
241
242
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
243
|
+
describe 'using model#phony_normalize' do
|
244
|
+
it "should set a normalized version of an attribute" do
|
245
|
+
model = model_klass.new(:phone_number => "+31-(0)10-1234123")
|
246
|
+
model.valid?.should be_true
|
247
|
+
model.phone_number.should eql('31101234123')
|
248
|
+
end
|
248
249
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
250
|
+
it "should set a normalized version of an attribute using :as option" do
|
251
|
+
model_klass.phony_normalize :phone_number, :as => :phone_number_as_normalized
|
252
|
+
model = model_klass.new(:phone_number => "+31-(0)10-1234123")
|
253
|
+
model.valid?.should be_true
|
254
|
+
model.phone_number_as_normalized.should eql('31101234123')
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should raise a RuntimeError at validation if the attribute doesn't exist" do
|
258
|
+
dummy_klass.phony_normalize :non_existing_attribute
|
259
|
+
dummy = dummy_klass.new
|
260
|
+
lambda {
|
261
|
+
dummy.valid?
|
262
|
+
}.should raise_error(RuntimeError)
|
263
|
+
end
|
254
264
|
end
|
265
|
+
end
|
255
266
|
|
256
|
-
|
257
|
-
|
267
|
+
describe 'ActiveRecord' do
|
268
|
+
let(:model_klass){ ActiveRecordModel }
|
269
|
+
let(:dummy_klass){ ActiveRecordDummy }
|
270
|
+
it_behaves_like 'model with PhonyRails'
|
271
|
+
end
|
258
272
|
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
end
|
273
|
+
describe 'Mongoid' do
|
274
|
+
let(:model_klass){ MongoidModel }
|
275
|
+
let(:dummy_klass){ MongoidDummy }
|
276
|
+
it_behaves_like 'model with PhonyRails'
|
264
277
|
end
|
265
278
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
|
4
|
-
|
5
4
|
require 'active_record'
|
5
|
+
require 'mongoid'
|
6
6
|
require 'phony_rails'
|
7
7
|
|
8
8
|
ActiveRecord::Base.establish_connection(
|
@@ -11,22 +11,41 @@ ActiveRecord::Base.establish_connection(
|
|
11
11
|
)
|
12
12
|
|
13
13
|
ActiveRecord::Schema.define do
|
14
|
-
create_table :
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
create_table :active_record_models do |table|
|
15
|
+
table.column :phone_attribute, :string
|
16
|
+
table.column :phone_number, :string
|
17
|
+
table.column :phone_number_as_normalized, :string
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module SharedModelMethods
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
included do
|
24
|
+
attr_accessor :phone_method, :phone1_method, :country_code
|
25
|
+
phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
|
26
|
+
phony_normalized_method :phone_method # adds normalized_phone_method method
|
27
|
+
phony_normalized_method :phone1_method, :default_country_code => 'DE' # adds normalized_phone_method method
|
28
|
+
phony_normalize :phone_number # normalized on validation
|
29
|
+
end
|
18
30
|
end
|
31
|
+
|
32
|
+
class ActiveRecordModel < ActiveRecord::Base
|
33
|
+
include SharedModelMethods
|
34
|
+
end
|
35
|
+
|
36
|
+
class ActiveRecordDummy < ActiveRecordModel
|
19
37
|
end
|
20
38
|
|
21
|
-
class
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
39
|
+
class MongoidModel
|
40
|
+
include Mongoid::Document
|
41
|
+
include Mongoid::Phony
|
42
|
+
field :phone_attribute, :type => String
|
43
|
+
field :phone_number, :type => String
|
44
|
+
field :phone_number_as_normalized, :type => String
|
45
|
+
include SharedModelMethods
|
27
46
|
end
|
28
47
|
|
29
|
-
class
|
48
|
+
class MongoidDummy < MongoidModel
|
30
49
|
end
|
31
50
|
|
32
51
|
RSpec.configure do |config|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: phony
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 0.8.2
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
47
|
+
name: activesupport
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
@@ -59,6 +59,38 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '3.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activerecord
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: mongoid
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '3.0'
|
62
94
|
description: This Gem adds useful methods to your Rails app to validate, display and
|
63
95
|
save phone numbers.
|
64
96
|
email:
|