phony_rails 0.1.6 → 0.1.7

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.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /.rvmrc
2
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --drb
3
+ --format documentation
data/Gemfile CHANGED
@@ -2,5 +2,23 @@ source :rubygems
2
2
  gemspec # Specify your gem's dependencies in phony_number.gemspec
3
3
 
4
4
  # For testing
5
- gem 'rspec'
6
5
  gem 'sqlite3'
6
+
7
+ gem 'rspec', '~> 2.11.0'
8
+ gem 'guard', '~> 1.2.0'
9
+ gem 'guard-bundler', '~> 1.0.0'
10
+ gem 'guard-rspec', '~> 1.2.0'
11
+
12
+ case RbConfig::CONFIG['host_os']
13
+ when /darwin/i
14
+ gem 'growl'
15
+ gem 'rb-fsevent'
16
+ when /linux/i
17
+ gem 'libnotify'
18
+ gem 'rb-inotify'
19
+ when /mswin|windows/i
20
+ gem 'rb-fchange'
21
+ gem 'rb-notifu'
22
+ gem 'win32console'
23
+ end
24
+
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.1.5)
4
+ phony_rails (0.1.6)
5
5
  activerecord (~> 3.0)
6
6
  countries (~> 0.8.2)
7
- phony (~> 1.7.4)
7
+ phony (~> 1.7.7)
8
8
 
9
9
  GEM
10
10
  remote: http://rubygems.org/
@@ -27,24 +27,49 @@ GEM
27
27
  currencies (>= 0.2.0)
28
28
  currencies (0.4.0)
29
29
  diff-lcs (1.1.3)
30
+ ffi (1.1.0)
31
+ growl (1.0.3)
32
+ guard (1.2.3)
33
+ listen (>= 0.4.2)
34
+ thor (>= 0.14.6)
35
+ guard-bundler (1.0.0)
36
+ bundler (~> 1.0)
37
+ guard (~> 1.1)
38
+ guard-rspec (1.2.0)
39
+ guard (>= 1.1)
30
40
  i18n (0.6.0)
41
+ listen (0.4.7)
42
+ rb-fchange (~> 0.0.5)
43
+ rb-fsevent (~> 0.9.1)
44
+ rb-inotify (~> 0.8.8)
31
45
  multi_json (1.3.6)
32
- phony (1.7.4)
33
- rspec (2.10.0)
34
- rspec-core (~> 2.10.0)
35
- rspec-expectations (~> 2.10.0)
36
- rspec-mocks (~> 2.10.0)
37
- rspec-core (2.10.1)
38
- rspec-expectations (2.10.0)
46
+ phony (1.7.7)
47
+ rb-fchange (0.0.5)
48
+ ffi
49
+ rb-fsevent (0.9.1)
50
+ rb-inotify (0.8.8)
51
+ ffi (>= 0.5.0)
52
+ rspec (2.11.0)
53
+ rspec-core (~> 2.11.0)
54
+ rspec-expectations (~> 2.11.0)
55
+ rspec-mocks (~> 2.11.0)
56
+ rspec-core (2.11.1)
57
+ rspec-expectations (2.11.1)
39
58
  diff-lcs (~> 1.1.3)
40
- rspec-mocks (2.10.1)
59
+ rspec-mocks (2.11.1)
41
60
  sqlite3 (1.3.6)
61
+ thor (0.15.4)
42
62
  tzinfo (0.3.33)
43
63
 
44
64
  PLATFORMS
45
65
  ruby
46
66
 
47
67
  DEPENDENCIES
68
+ growl
69
+ guard (~> 1.2.0)
70
+ guard-bundler (~> 1.0.0)
71
+ guard-rspec (~> 1.2.0)
48
72
  phony_rails!
49
- rspec
73
+ rb-fsevent
74
+ rspec (~> 2.11.0)
50
75
  sqlite3
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ # More info at https://github.com/guard/guard#readme
2
+
3
+ guard 'bundler' do
4
+ watch('Gemfile')
5
+ watch(/^.+\.gemspec/)
6
+ end
7
+
8
+ guard 'rspec', :version => 2 do
9
+ watch(%r{^spec/.+_spec\.rb$})
10
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
11
+ watch('spec/spec_helper.rb') { "spec" }
12
+ end
data/README.md CHANGED
@@ -21,6 +21,8 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
+ ### Normalization / ActiveRecord Extension
25
+
24
26
  In your model add:
25
27
 
26
28
  class SomeModel < ActiveRecord::Base
@@ -39,14 +41,39 @@ The `:default_country_code` options is used to specify a country_code when norma
39
41
 
40
42
  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'`.
41
43
 
42
- Use the Phony.plausible method to validate an attribute:
44
+ ### Validation
45
+
46
+ In your model use the Phony.plausible method to validate an attribute:
43
47
 
44
48
  validates :phone_number, :phony_plausible => true
45
49
 
50
+ or the helper method:
51
+
52
+ validates_plausible_phone :phone_number
53
+
54
+ this method use other validators under the hood to provide:
55
+ * presence validation using `ActiveModel::Validations::PresenceValidator`
56
+ * format validation using `ActiveModel::Validations::FormatValidator`
57
+
58
+ so we can use:
59
+
60
+ validates_plausible_phone :phone_number, :presence => true
61
+ validates_plausible_phone :phone_number, :with => /^\+\d+/
62
+ validates_plausible_phone :phone_number, :without => /^\+\d+/
63
+ validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/
64
+
65
+ ### Display / Views
66
+
46
67
  In your views use:
47
68
 
48
69
  <%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>
49
70
 
71
+ ### Find by normalized number
72
+
73
+ Say you want to find a record by a phone number. Best is to normalize user input and compare to an attribute stored in the db.
74
+
75
+ Home.find_by_normalized_phone_number(PhonyRails.normalize_number(params[:phone_number]))
76
+
50
77
  ## Changelog
51
78
 
52
79
  0.1.6
@@ -77,6 +104,12 @@ In your views use:
77
104
  0.0.6
78
105
  * Fixed problem with '070-4157134' being parsed as US number
79
106
 
107
+ ## TODO
108
+
109
+ * Make this work: Home.find_by_normalized_phone_number(Home.normalize_number(params[:phone_number]))
110
+ So we use Home.normalize_number instead of PhonyRails.normalize_number. This way we can use the same default_country_code.
111
+ * Make country_code method configurable.
112
+
80
113
  ## Contributing
81
114
 
82
115
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -16,7 +16,12 @@ module ActiveModel
16
16
  module HelperMethods
17
17
 
18
18
  def validates_plausible_phone(*attr_names)
19
- validates_with PhonyPlausibleValidator, _merge_attributes(attr_names)
19
+ # merged attributes are modified somewhere, so we are cloning them for each validator
20
+ merged_attributes = _merge_attributes(attr_names)
21
+
22
+ validates_with PresenceValidator, merged_attributes.clone if merged_attributes[:presence]
23
+ validates_with FormatValidator, merged_attributes.clone if (merged_attributes[:with] or merged_attributes[:without])
24
+ validates_with PhonyPlausibleValidator, merged_attributes.clone
20
25
  end
21
26
 
22
27
  end
data/phony_rails.gemspec CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = PhonyRails::VERSION
17
17
 
18
- gem.add_dependency "phony", "~> 1.7.4"
18
+ gem.add_dependency "phony", "~> 1.7.7"
19
19
  gem.add_dependency "countries", "~> 0.8.2"
20
20
  gem.add_dependency "activerecord", "~> 3.0"
21
21
  end
@@ -0,0 +1,245 @@
1
+ require 'spec_helper'
2
+
3
+ #-----------------------------------------------------------------------------------------------------------------------
4
+ # Model
5
+ #-----------------------------------------------------------------------------------------------------------------------
6
+
7
+ #--------------------
8
+ ActiveRecord::Schema.define do
9
+ create_table :simple_homes do |table|
10
+ table.column :phone_number, :string
11
+ end
12
+
13
+ create_table :helpful_homes do |table|
14
+ table.column :phone_number, :string
15
+ end
16
+
17
+ create_table :required_helpful_homes do |table|
18
+ table.column :phone_number, :string
19
+ end
20
+
21
+ create_table :optional_helpful_homes do |table|
22
+ table.column :phone_number, :string
23
+ end
24
+
25
+ create_table :formatted_helpful_homes do |table|
26
+ table.column :phone_number, :string
27
+ end
28
+
29
+ create_table :not_formatted_helpful_homes do |table|
30
+ table.column :phone_number, :string
31
+ end
32
+
33
+ create_table :big_helpful_homes do |table|
34
+ table.column :phone_number, :string
35
+ end
36
+ end
37
+
38
+ #--------------------
39
+ class SimpleHome < ActiveRecord::Base
40
+ attr_accessor :phone_number
41
+ validates :phone_number, :phony_plausible => true
42
+ end
43
+
44
+ #--------------------
45
+ class HelpfulHome < ActiveRecord::Base
46
+ attr_accessor :phone_number
47
+ validates_plausible_phone :phone_number
48
+ end
49
+
50
+ #--------------------
51
+ class RequiredHelpfulHome < ActiveRecord::Base
52
+ attr_accessor :phone_number
53
+ validates_plausible_phone :phone_number, :presence => true
54
+ end
55
+
56
+ #--------------------
57
+ class OptionalHelpfulHome < ActiveRecord::Base
58
+ attr_accessor :phone_number
59
+ validates_plausible_phone :phone_number, :presence => false
60
+ end
61
+
62
+ #--------------------
63
+ class FormattedHelpfulHome < ActiveRecord::Base
64
+ attr_accessor :phone_number
65
+ validates_plausible_phone :phone_number, :with => /^\+\d+/
66
+ end
67
+
68
+ #--------------------
69
+ class NotFormattedHelpfulHome < ActiveRecord::Base
70
+ attr_accessor :phone_number
71
+ validates_plausible_phone :phone_number, :without => /^\+\d+/
72
+ end
73
+
74
+ #--------------------
75
+ class BigHelpfulHome < ActiveRecord::Base
76
+ attr_accessor :phone_number
77
+ validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/
78
+ end
79
+
80
+ #-----------------------------------------------------------------------------------------------------------------------
81
+ # Tests
82
+ #-----------------------------------------------------------------------------------------------------------------------
83
+
84
+ VALID_NUMBER = '123456789'
85
+ INVALID_NUMBER = '123456789 123456789 123456789 123456789'
86
+
87
+ #-----------------------------------------------------------------------------------------------------------------------
88
+ describe PhonyPlausibleValidator do
89
+
90
+ #--------------------
91
+ describe '#validates' do
92
+
93
+ before(:each) do
94
+ @home = SimpleHome.new
95
+ end
96
+
97
+ it "should validate an empty number" do
98
+ @home.should be_valid
99
+ end
100
+
101
+ it "should validate a valid number" do
102
+ @home.phone_number = VALID_NUMBER
103
+ @home.should be_valid
104
+ end
105
+
106
+ it "should invalidate an invalid number" do
107
+ @home.phone_number = INVALID_NUMBER
108
+ @home.should_not be_valid
109
+ @home.errors.messages.should include(:phone_number => ["is an invalid number"])
110
+ end
111
+
112
+ end
113
+ end
114
+
115
+ #-----------------------------------------------------------------------------------------------------------------------
116
+ describe ActiveModel::Validations::HelperMethods do
117
+
118
+ #--------------------
119
+ describe '#validates_plausible_phone' do
120
+
121
+ #--------------------
122
+ context 'when a number is optional' do
123
+
124
+ before(:each) do
125
+ @home = HelpfulHome.new
126
+ end
127
+
128
+ it "should validate an empty number" do
129
+ @home.should be_valid
130
+ end
131
+
132
+ it "should validate a valid number" do
133
+ @home.phone_number = VALID_NUMBER
134
+ @home.should be_valid
135
+ end
136
+
137
+ it "should invalidate an invalid number" do
138
+ @home.phone_number = INVALID_NUMBER
139
+ @home.should_not be_valid
140
+ @home.errors.messages.should include(:phone_number => ["is an invalid number"])
141
+ end
142
+
143
+ end
144
+
145
+ #--------------------
146
+ context 'when a number is required (:presence = true)' do
147
+
148
+ before(:each) do
149
+ @home = RequiredHelpfulHome.new
150
+ end
151
+
152
+ it "should invalidate an empty number" do
153
+ @home.should_not be_valid
154
+ @home.errors.messages.should include(:phone_number => ["can't be blank"])
155
+ end
156
+
157
+ it "should validate a valid number" do
158
+ @home.phone_number = VALID_NUMBER
159
+ @home.should be_valid
160
+ end
161
+
162
+ it "should invalidate an invalid number" do
163
+ @home.phone_number = INVALID_NUMBER
164
+ @home.should_not be_valid
165
+ @home.errors.messages.should include(:phone_number => ["is an invalid number"])
166
+ end
167
+
168
+ end
169
+
170
+ #--------------------
171
+ context 'when a number is not required (!presence = false)' do
172
+
173
+ before(:each) do
174
+ @home = OptionalHelpfulHome.new
175
+ end
176
+
177
+ it "should validate an empty number" do
178
+ @home.should be_valid
179
+ end
180
+
181
+ it "should validate a valid number" do
182
+ @home.phone_number = VALID_NUMBER
183
+ @home.should be_valid
184
+ end
185
+
186
+ it "should invalidate an invalid number" do
187
+ @home.phone_number = INVALID_NUMBER
188
+ @home.should_not be_valid
189
+ @home.errors.messages.should include(:phone_number => ["is an invalid number"])
190
+ end
191
+
192
+ end
193
+
194
+ #--------------------
195
+ context 'when a number must be formatted (:with)' do
196
+
197
+ before(:each) do
198
+ @home = FormattedHelpfulHome.new
199
+ end
200
+
201
+ it "should invalidate an empty number" do
202
+ @home.should_not be_valid
203
+ @home.errors.messages.should include(:phone_number => ["is invalid"])
204
+ end
205
+
206
+ it "should validate a well formatted valid number" do
207
+ @home.phone_number = "+33 #{VALID_NUMBER}"
208
+ @home.should be_valid
209
+ end
210
+
211
+ it "should invalidate a bad formatted valid number" do
212
+ @home.phone_number = VALID_NUMBER
213
+ @home.should_not be_valid
214
+ @home.errors.messages.should include(:phone_number => ["is invalid"])
215
+ end
216
+
217
+ end
218
+
219
+ #--------------------
220
+ context 'when a number must not be formatted (:without)' do
221
+
222
+ before(:each) do
223
+ @home = NotFormattedHelpfulHome.new
224
+ end
225
+
226
+ it "should validate an empty number" do
227
+ @home.should be_valid
228
+ end
229
+
230
+ it "should validate a well formatted valid number" do
231
+ @home.phone_number = VALID_NUMBER
232
+ @home.should be_valid
233
+ end
234
+
235
+ it "should invalidate a bad formatted valid number" do
236
+ @home.phone_number = "+33 #{VALID_NUMBER}"
237
+ @home.should_not be_valid
238
+ @home.errors.messages.should include(:phone_number => ["is invalid"])
239
+ end
240
+
241
+ end
242
+
243
+ end
244
+
245
+ end
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.1.6
4
+ version: 0.1.7
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: 2012-07-02 00:00:00.000000000 Z
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: phony
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.7.4
21
+ version: 1.7.7
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.7.4
29
+ version: 1.7.7
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: countries
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -67,8 +67,11 @@ executables: []
67
67
  extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
+ - .gitignore
71
+ - .rspec
70
72
  - Gemfile
71
73
  - Gemfile.lock
74
+ - Guardfile
72
75
  - LICENSE
73
76
  - README.md
74
77
  - Rakefile
@@ -78,7 +81,7 @@ files:
78
81
  - lib/validators/phony_validator.rb
79
82
  - phony_rails.gemspec
80
83
  - spec/lib/phony_rails_spec.rb
81
- - spec/lib/phony_validator_spec.rb
84
+ - spec/lib/validators/phony_validator_spec.rb
82
85
  - spec/spec_helper.rb
83
86
  homepage: https://github.com/joost/phony_rails
84
87
  licenses: []
@@ -107,6 +110,6 @@ summary: This Gem adds useful methods to your Rails app to validate, display and
107
110
  phone numbers.
108
111
  test_files:
109
112
  - spec/lib/phony_rails_spec.rb
110
- - spec/lib/phony_validator_spec.rb
113
+ - spec/lib/validators/phony_validator_spec.rb
111
114
  - spec/spec_helper.rb
112
115
  has_rdoc:
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- ActiveRecord::Schema.define do
4
- create_table :validating_homes do |table|
5
- table.column :phone_number, :string
6
- table.column :fax_number, :string
7
- end
8
- end
9
-
10
- class ValidatingHome < ActiveRecord::Base
11
- attr_accessor :phone_method, :fax_number
12
- validates :phone_number, :phony_plausible => true
13
- validates_plausible_phone :fax_number
14
- end
15
-
16
-
17
-
18
- describe PhonyPlausibleValidator do
19
-
20
- describe 'validates' do
21
- before(:each) do
22
- @home = ValidatingHome.new
23
- end
24
-
25
- it "should validate an empty number" do
26
- @home.should be_valid
27
- end
28
-
29
- it "should validate a valid number" do
30
- @home.phone_number = '123456789'
31
- @home.should be_valid
32
- end
33
-
34
- it "should invalidate an invalid number" do
35
- @home.phone_number = '123456789 123456789 123456789 123456789'
36
- @home.should_not be_valid
37
- end
38
- end
39
-
40
- describe 'validates_plausible_phone' do
41
- before(:each) do
42
- @home = ValidatingHome.new
43
- end
44
-
45
- it "should validate an empty number" do
46
- @home.should be_valid
47
- end
48
-
49
- it "should validate a valid number" do
50
- @home.fax_number = '123456789'
51
- @home.should be_valid
52
- end
53
-
54
- it "should invalidate an invalid number" do
55
- @home.fax_number = '123456789 123456789 123456789 123456789'
56
- @home.should_not be_valid
57
- end
58
- end
59
-
60
- end