russian_phone 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -58,7 +58,7 @@ Or install it yourself as:
58
58
 
59
59
  class User
60
60
  include Mongoid::Document
61
- field :phone, type: RussianPhone.field(default_country: 7, allowed_cities: [495]), validate: true
61
+ field :phone, type: RussianPhone.field(default_country: 7, allowed_cities: [495]), validate: true, required: true
62
62
  end
63
63
 
64
64
  # u = User.new(phone: '495 1111111')
@@ -10,7 +10,13 @@ module RussianPhone
10
10
 
11
11
  ::Mongoid::Fields.option :validate do |model, field, value|
12
12
  if value
13
- model.validates_with(RussianPhone::Validator, fields: [field.name])
13
+ model.validates_with(RussianPhone::FormatValidator, fields: [field.name])
14
+ end
15
+ end
16
+
17
+ ::Mongoid::Fields.option :required do |model, field, value|
18
+ if value
19
+ model.validates_with(RussianPhone::PresenceValidator, fields: [field.name])
14
20
  end
15
21
  end
16
22
 
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ module RussianPhone
4
+ class FormatValidator < ActiveModel::Validator
5
+ def validate(record)
6
+ options[:fields].each do |field|
7
+ unless record.send(field).phone.blank?
8
+ record.errors[field] << 'Неверный телефонный номер' unless record.send(field).valid? && record.send(field).city_allowed?
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ module RussianPhone
4
+ class PresenceValidator < ActiveModel::Validator
5
+ def validate(record)
6
+ options[:fields].each do |field|
7
+ if record.send(field).phone.blank?
8
+ record.errors[field] << 'Необходимо заполнить'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module RussianPhone
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/russian_phone.rb CHANGED
@@ -3,10 +3,11 @@
3
3
  require "russian_phone/version"
4
4
 
5
5
  module RussianPhone
6
- autoload :Field, 'russian_phone/field'
7
- autoload :Number, 'russian_phone/number'
8
- autoload :Codes, 'russian_phone/codes'
9
- autoload :Validator, 'russian_phone/validator'
6
+ autoload :Field, 'russian_phone/field'
7
+ autoload :Number, 'russian_phone/number'
8
+ autoload :Codes, 'russian_phone/codes'
9
+ autoload :PresenceValidator, 'russian_phone/presence_validator'
10
+ autoload :FormatValidator, 'russian_phone/format_validator'
10
11
 
11
12
  def self.field(options = {})
12
13
  RussianPhone::Field.new(options)
data/spec/phone_spec.rb CHANGED
@@ -505,6 +505,30 @@ describe RussianPhone do
505
505
  u.valid?.should be_true
506
506
  u.save.should be_true
507
507
  end
508
+
509
+ it 'should pass validation when required but not validated' do
510
+ u = UserWithRequired.new(phone: '906 121 11 11')
511
+
512
+ u.valid?.should be_true
513
+ u.save.should be_true
514
+ end
515
+
516
+ it 'should pass validation when required but not validated' do
517
+ u = UserWithRequired.new(phone: '11 11')
518
+
519
+ u.valid?.should be_true
520
+ u.save.should be_true
521
+ end
522
+
523
+ it 'should fail validation when required and not present' do
524
+ u = UserWithRequired.new()
525
+ u.valid?.should be_false
526
+ u.save.should be_false
527
+
528
+ u = UserWithRequired.new(phone: '')
529
+ u.valid?.should be_false
530
+ u.save.should be_false
531
+ end
508
532
  end
509
533
 
510
534
  end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+
3
+ class UserWithRequired
4
+ include Mongoid::Document
5
+
6
+ field :phone, type: RussianPhone.field(default_country: 7, allowed_cities: [495]), validate: false, required: true
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian_phone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.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-02-03 00:00:00.000000000 Z
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -107,14 +107,16 @@ files:
107
107
  - lib/russian_phone.rb
108
108
  - lib/russian_phone/codes.rb
109
109
  - lib/russian_phone/field.rb
110
+ - lib/russian_phone/format_validator.rb
110
111
  - lib/russian_phone/number.rb
111
- - lib/russian_phone/validator.rb
112
+ - lib/russian_phone/presence_validator.rb
112
113
  - lib/russian_phone/version.rb
113
114
  - russian_phone.gemspec
114
115
  - spec/phone_spec.rb
115
116
  - spec/spec_helper.rb
116
117
  - spec/support/user.rb
117
118
  - spec/support/user_with_any_code.rb
119
+ - spec/support/user_with_required.rb
118
120
  - spec/support/user_with_validation.rb
119
121
  - spec/support/user_without_validation.rb
120
122
  - validate.sh
@@ -147,5 +149,6 @@ test_files:
147
149
  - spec/spec_helper.rb
148
150
  - spec/support/user.rb
149
151
  - spec/support/user_with_any_code.rb
152
+ - spec/support/user_with_required.rb
150
153
  - spec/support/user_with_validation.rb
151
154
  - spec/support/user_without_validation.rb
@@ -1,11 +0,0 @@
1
- # coding: utf-8
2
-
3
- module RussianPhone
4
- class Validator < ActiveModel::Validator
5
- def validate(record)
6
- options[:fields].each do |field|
7
- record.errors[field] << 'Неверный телефонный номер' unless record.send(field).valid? && record.send(field).city_allowed?
8
- end
9
- end
10
- end
11
- end