snils 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -15
- data/README.ru.md +53 -15
- data/lib/snils.rb +3 -4
- data/lib/snils/all.rb +2 -0
- data/lib/snils/rails.rb +26 -0
- data/lib/snils/version.rb +1 -1
- data/spec/snils_spec.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91cdf5ebb2a1c759d2a516c727f57b76608a0c6d
|
4
|
+
data.tar.gz: b09a5d8567ea381ba33266f35675e62f3d9d1f20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68e09826f0a41d26ee283eb7f84c39bf4df40a87662a7830879e465922a4cada26845bdc1780e586832756b3a362e5bc1a478bfdeb61a2b7934105b0ac3e871d
|
7
|
+
data.tar.gz: d190281341892789efde45ecabe5398886bfa57b0825016e9785a6db0856a600a83ff24ee5242cd61cb3d112f5644951960dfc12477ea1b11521640d0003bd14
|
data/README.md
CHANGED
@@ -51,26 +51,63 @@ Snils.new("216471647").errors
|
|
51
51
|
|
52
52
|
Validating Rails model attributes:
|
53
53
|
|
54
|
+
1. Modify your gemfile to require `snils/rails`
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
gem 'snils', require: 'snils/rails'
|
58
|
+
```
|
59
|
+
|
60
|
+
2. Add `:snils` validation to SNILS attributes
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
validates :snils, presence: true, uniqueness: true, snils: true
|
64
|
+
```
|
65
|
+
|
66
|
+
Generating SNILSes in factories for tests:
|
67
|
+
|
54
68
|
```ruby
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
def snils_validation
|
64
|
-
validated_snils = Snils.new(snils)
|
65
|
-
unless validated_snils.valid?
|
66
|
-
validated_snils.errors.each do |error|
|
67
|
-
errors.add(:snils, *error)
|
68
|
-
end
|
69
|
-
end
|
69
|
+
FactoryGirl.define do
|
70
|
+
sequence :snils do |_|
|
71
|
+
Snils.new.to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
factory :user do
|
75
|
+
snils
|
70
76
|
end
|
71
77
|
end
|
72
78
|
```
|
73
79
|
|
80
|
+
### Recommended workflow for Ruby on Rails projects
|
81
|
+
|
82
|
+
1. Use [draper] gem to format SNILS for views
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# app/decorators/user_decorator.rb
|
86
|
+
class UserDecorator < Draper::Decorator
|
87
|
+
delegate_all
|
88
|
+
|
89
|
+
def snils
|
90
|
+
@formatted_snils ||= Snils.new(object.snils).formatted
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
2. Sanitize SNILSes on attribute write
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# app/models/user.rb
|
99
|
+
class User < ActiveRecord::Base
|
100
|
+
validates :snils, presence: true, uniqueness: true, snils: true
|
101
|
+
|
102
|
+
def snils=(value)
|
103
|
+
Snils.new(value).raw
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
With this setup you will always store raw (only digits) value in database and always will show pretty formatted SNILS to users.
|
109
|
+
|
110
|
+
|
74
111
|
## Contributing
|
75
112
|
|
76
113
|
1. Fork it ( https://github.com/Envek/snils/fork )
|
@@ -79,4 +116,5 @@ end
|
|
79
116
|
4. Push to the branch (`git push origin my-new-feature`)
|
80
117
|
5. Create a new Pull Request
|
81
118
|
|
119
|
+
[draper]: https://github.com/drapergem/draper
|
82
120
|
[SNILS]: http://en.wikipedia.org/wiki/SNILS_(Russia) "Insurance individual account number"
|
data/README.ru.md
CHANGED
@@ -46,26 +46,63 @@ Snils.new("216471647").errors
|
|
46
46
|
|
47
47
|
Валидация в Ruby on Rails для атрибутов моделей:
|
48
48
|
|
49
|
+
1. Измените Gemfile, чтобы он подключал `snils/rails`
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
gem 'snils', require: 'snils/rails'
|
53
|
+
```
|
54
|
+
|
55
|
+
2. Добавьте валидацию `:snils` к требуемым атрибутам
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
validates :snils, presence: true, uniqueness: true, snils: true
|
59
|
+
```
|
60
|
+
|
61
|
+
Генерация СНИЛСов в фабриках для тестов:
|
62
|
+
|
49
63
|
```ruby
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
def snils_validation
|
59
|
-
validated_snils = Snils.new(snils)
|
60
|
-
unless validated_snils.valid?
|
61
|
-
validated_snils.errors.each do |error|
|
62
|
-
errors.add(:snils, *error)
|
63
|
-
end
|
64
|
-
end
|
64
|
+
FactoryGirl.define do
|
65
|
+
sequence :snils do |_|
|
66
|
+
Snils.new.to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
factory :user do
|
70
|
+
snils
|
65
71
|
end
|
66
72
|
end
|
67
73
|
```
|
68
74
|
|
75
|
+
### Рекомендуемый рабочий процесс для проектов на Ruby on Rails
|
76
|
+
|
77
|
+
1. Используйте [draper], чтобы форматировать СНИЛС для отображения
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# app/decorators/user_decorator.rb
|
81
|
+
class UserDecorator < Draper::Decorator
|
82
|
+
delegate_all
|
83
|
+
|
84
|
+
def snils
|
85
|
+
@formatted_snils ||= Snils.new(object.snils).formatted if object.snils
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
2. Очищайте СНИЛС при записи в атрибут модели
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# app/models/user.rb
|
94
|
+
class User < ActiveRecord::Base
|
95
|
+
validates :snils, presence: true, uniqueness: true, snils: true
|
96
|
+
|
97
|
+
def snils=(value)
|
98
|
+
Snils.new(value).raw
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
С такой настройкой вы всегда будете хранить в БД значение, состоящее только из цифр, а пользователю всегда будете отображать красиво форматированный СНИЛС.
|
104
|
+
|
105
|
+
|
69
106
|
## Помощь в разработке
|
70
107
|
|
71
108
|
1. Сделайте форк проекта в своём github-аккаунте. ( https://github.com/Envek/snils/fork )
|
@@ -74,4 +111,5 @@ end
|
|
74
111
|
4. Запушьте изменения (`git push origin my-new-feature`)
|
75
112
|
5. Создайте новый Pull Request
|
76
113
|
|
114
|
+
[draper]: https://github.com/drapergem/draper
|
77
115
|
[СНИЛС]: http://ru.wikipedia.org/wiki/%D0%A1%D1%82%D1%80%D0%B0%D1%85%D0%BE%D0%B2%D0%BE%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80_%D0%B8%D0%BD%D0%B4%D0%B8%D0%B2%D0%B8%D0%B4%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B3%D0%BE_%D0%BB%D0%B8%D1%86%D0%B5%D0%B2%D0%BE%D0%B3%D0%BE_%D1%81%D1%87%D1%91%D1%82%D0%B0
|
data/lib/snils.rb
CHANGED
@@ -19,7 +19,7 @@ class Snils
|
|
19
19
|
# Calculates checksum (last 2 digits) of a number
|
20
20
|
def checksum
|
21
21
|
digits = @snils.split('').take(9).map(&:to_i)
|
22
|
-
checksum = digits.each.with_index.
|
22
|
+
checksum = digits.each.with_index.reduce(0) do |sum, (digit, index)|
|
23
23
|
sum + digit * (9 - index)
|
24
24
|
end
|
25
25
|
while checksum > 101 do
|
@@ -59,7 +59,7 @@ class Snils
|
|
59
59
|
|
60
60
|
# Generates new random valid SNILS
|
61
61
|
def self.generate
|
62
|
-
digits = Array.new(9).map{ rand(10) }.join
|
62
|
+
digits = Array.new(9).map { rand(10) }.join
|
63
63
|
sum = self.new(digits).checksum
|
64
64
|
"#{digits}#{sum}"
|
65
65
|
end
|
@@ -67,10 +67,9 @@ class Snils
|
|
67
67
|
protected
|
68
68
|
|
69
69
|
def validate
|
70
|
-
@errors << [:wrong_length, {:count => 11}] unless @snils.length == 11
|
70
|
+
@errors << [:wrong_length, { :count => 11 }] unless @snils.length == 11
|
71
71
|
@errors << :invalid unless @snils[-2..-1] == self.checksum
|
72
72
|
@validated = true
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|
76
|
-
|
data/lib/snils/all.rb
ADDED
data/lib/snils/rails.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'snils'
|
2
|
+
|
3
|
+
# SNILS validation for Active Model (Active Record and Ruby on Rails)
|
4
|
+
#
|
5
|
+
# Usage:
|
6
|
+
#
|
7
|
+
# 1. Modify your gemfile to require 'snils/rails'
|
8
|
+
#
|
9
|
+
# gem 'snils', require: 'snils/rails'
|
10
|
+
#
|
11
|
+
# 2. Add +:snils+ validation to SNILS attributes
|
12
|
+
#
|
13
|
+
# validates :snils, presence: true, uniqueness: true, snils: true
|
14
|
+
#
|
15
|
+
class SnilsValidator < ActiveModel::EachValidator
|
16
|
+
def validate_each(record, attribute, value)
|
17
|
+
snils = Snils.new(value)
|
18
|
+
if snils.errors.any? && options[:message]
|
19
|
+
record.errors.add(attribute, options[:message])
|
20
|
+
else
|
21
|
+
snils.errors.each do |error|
|
22
|
+
record.errors.add(attribute, *error)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/snils/version.rb
CHANGED
data/spec/snils_spec.rb
CHANGED
@@ -68,12 +68,12 @@ describe Snils do
|
|
68
68
|
|
69
69
|
it 'should not validate invalid snils (with wrong length)' do
|
70
70
|
snils = described_class.new('963-117-158 080')
|
71
|
-
expect(snils.errors).to include [:wrong_length, {:count => 11}]
|
71
|
+
expect(snils.errors).to include [:wrong_length, { :count => 11 }]
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'should not validate invalid snils (with wrong length)' do
|
75
75
|
snils = described_class.new('963-117-158')
|
76
|
-
expect(snils.errors).to include [:wrong_length, {:count => 11}]
|
76
|
+
expect(snils.errors).to include [:wrong_length, { :count => 11 }]
|
77
77
|
end
|
78
78
|
|
79
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Novikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,6 +85,8 @@ files:
|
|
85
85
|
- README.ru.md
|
86
86
|
- Rakefile
|
87
87
|
- lib/snils.rb
|
88
|
+
- lib/snils/all.rb
|
89
|
+
- lib/snils/rails.rb
|
88
90
|
- lib/snils/version.rb
|
89
91
|
- snils.gemspec
|
90
92
|
- spec/snils_spec.rb
|