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 CHANGED
@@ -2,7 +2,7 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  phony_rails (0.3.2)
5
- activerecord (>= 3.0)
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 / ActiveRecord Extension
24
+ ### Normalization / Model Usage
25
25
 
26
- In your model add:
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
- def self.extended(base)
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 :extend, PhonyRails::Extension if defined?(ActiveRecord)
109
- Mongoid::Document.module_eval do
110
- def self.included(base)
111
- base.extend PhonyRails::Extension
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 if defined?(Mongoid)
106
+ end
114
107
 
115
108
  Dir["#{File.dirname(__FILE__)}/phony_rails/locales/*.yml"].each do |file|
116
109
  I18n.load_path << file
@@ -1,6 +1,9 @@
1
-
2
1
  en:
3
2
  activerecord:
4
3
  errors:
5
4
  messages:
6
5
  improbable_phone: "is an invalid number"
6
+ activemodel:
7
+ errors:
8
+ messages:
9
+ improbable_phone: "is an invalid number"
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
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 "activerecord", ">= 3.0"
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
- describe 'defining ActiveRecord#phony_normalized_method' do
127
- it "should add a normalized_phone_attribute method" do
128
- Home.new.should respond_to(:normalized_phone_attribute)
129
- end
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
- it "should add a normalized_phone_method method" do
132
- Home.new.should respond_to(:normalized_phone_method)
133
- end
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
- it "should raise error on existing methods" do
136
- lambda {
137
- Home.phony_normalized_method(:phone_method)
138
- }.should raise_error(StandardError)
139
- end
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
- it "should raise error on not existing attribute" do
142
- Home.phony_normalized_method(:phone_non_existing_method)
143
- lambda {
144
- Home.new.normalized_phone_non_existing_method
145
- }.should raise_error(ArgumentError)
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
- describe 'defining ActiveRecord#phony_normalize' do
150
- it "should not accept :as option with multiple attribute names" do
151
- lambda {
152
- Home.phony_normalize(:phone_number, :phone1_method, :as => 'non_existing_attribute')
153
- }.should raise_error(ArgumentError)
154
- end
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
- it "should not accept :as option with unexisting attribute name" do
157
- lambda {
158
- Home.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute')
159
- }.should raise_error(ArgumentError)
160
- end
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
- it "should not accept :as option with single non existing attribute name" do
163
- lambda {
164
- Home.phony_normalize(:phone_number, :as => 'something_else')
165
- }.should raise_error(ArgumentError)
166
- end
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
- it "should accept :as option with single existing attribute name" do
169
- lambda {
170
- Home.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
171
- }.should_not raise_error(ArgumentError)
172
- end
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
- it "should accept a non existing attribute name" do
175
- lambda {
176
- Dummy.phony_normalize(:non_existing_attribute)
177
- }.should_not raise_error
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
- describe 'using ActiveRecord#phony_normalized_method' do
182
- # Following examples have complete number (with country code!)
183
- it "should return a normalized version of an attribute" do
184
- home = Home.new(:phone_attribute => "+31-(0)10-1234123")
185
- home.normalized_phone_attribute.should eql('31101234123')
186
- end
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
- it "should return a normalized version of a method" do
189
- home = Home.new(:phone_method => "+31-(0)10-1234123")
190
- home.normalized_phone_method.should eql('31101234123')
191
- end
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
- # Following examples have incomplete number
194
- it "should return nil if no country_code is known" do
195
- home = Home.new(:phone_attribute => "(0)10-1234123")
196
- home.normalized_phone_attribute.should eql('11234123') # This actually is an incorrect number! (FIXME?)
197
- end
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
- it "should use country_code option" do
200
- home = Home.new(:phone_attribute => "(0)10-1234123")
201
- home.normalized_phone_attribute(:country_code => 'NL').should eql('31101234123')
202
- end
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
- it "should use country_code object method" do
205
- home = Home.new(:phone_attribute => "(0)10-1234123", :country_code => 'NL')
206
- home.normalized_phone_attribute.should eql('31101234123')
207
- end
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
- it "should fallback to default_country_code option" do
210
- home = Home.new(:phone1_method => "(030) 8 61 29 06")
211
- home.normalized_phone1_method.should eql('49308612906')
212
- end
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
- it "should overwrite default_country_code option with object method" do
215
- home = Home.new(:phone1_method => "(030) 8 61 29 06", :country_code => 'NL')
216
- home.normalized_phone1_method.should eql('31308612906')
217
- end
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
- it "should overwrite default_country_code option with option" do
220
- home = Home.new(:phone1_method => "(030) 8 61 29 06")
221
- home.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
222
- end
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
- it "should use last passed options" do
225
- home = Home.new(:phone1_method => "(030) 8 61 29 06")
226
- home.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
227
- home.normalized_phone1_method(:country_code => 'DE').should eql('49308612906')
228
- home.normalized_phone1_method(:country_code => nil).should eql('49308612906')
229
- end
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
- it "should use last object method" do
232
- home = Home.new(:phone1_method => "(030) 8 61 29 06")
233
- home.country_code = 'NL'
234
- home.normalized_phone1_method.should eql('31308612906')
235
- home.country_code = 'DE'
236
- home.normalized_phone1_method.should eql('49308612906')
237
- home.country_code = nil
238
- home.normalized_phone1_method(:country_code => nil).should eql('49308612906')
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
- describe 'using ActiveRecord#phony_normalize' do
243
- it "should set a normalized version of an attribute" do
244
- home = Home.new(:phone_number => "+31-(0)10-1234123")
245
- home.valid?.should be_true
246
- home.phone_number.should eql('31101234123')
247
- end
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
- it "should set a normalized version of an attribute using :as option" do
250
- Home.phony_normalize :phone_number, :as => :phone_number_as_normalized
251
- home = Home.new(:phone_number => "+31-(0)10-1234123")
252
- home.valid?.should be_true
253
- home.phone_number_as_normalized.should eql('31101234123')
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
- it "should raise a RuntimeError at validation if the attribute doesn't exist" do
257
- Dummy.phony_normalize :non_existing_attribute
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
- dummy = Dummy.new
260
- lambda {
261
- dummy.valid?
262
- }.should raise_error(RuntimeError)
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 :homes do |table|
15
- table.column :phone_attribute, :string
16
- table.column :phone_number, :string
17
- table.column :phone_number_as_normalized, :string
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 Home < ActiveRecord::Base
22
- attr_accessor :phone_method, :phone1_method, :country_code
23
- phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
24
- phony_normalized_method :phone_method # adds normalized_phone_method method
25
- phony_normalized_method :phone1_method, :default_country_code => 'DE' # adds normalized_phone_method method
26
- phony_normalize :phone_number # normalized on validation
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 Dummy < Home
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.3.2
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-05-08 00:00:00.000000000 Z
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: activerecord
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: