Project

snils

0.0
No commit activity in last 3 years
No release in over 3 years
Generating, validating and formatting SNILS number (Russian pension insurance number)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0
~> 3.0
 Project Readme

SNILS

Gem Version Continuous Integration status PullReview stats

Generating, validating and formatting SNILS number (Russian pension insurance individual account number).

Генерация, валидация и форматирование СНИЛС (Страхового номера индивидуального лицевого счёта).

Read this README in Russian (Читать это README на русском)

Installation

Add this line to your application's Gemfile:

gem 'snils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install snils

Usage

Generate new SNILS:

Snils.new.formatted
#=> "216-471-647 63"

Validate SNILS:

Snils.new("21647164763").valid?
#=> true

Snils.new("21647164760").valid?
#=> false

Snils.new("21647164760").errors
#=> [:invalid]

Snils.new("216471647").errors
#=> [[:wrong_length, {:count=>11}], :invalid]

Validating Rails model attributes:

  1. Modify your gemfile to require snils/rails

    gem 'snils', require: 'snils/rails'
  2. Add :snils validation to SNILS attributes

    validates :snils, presence: true, uniqueness: true, snils: true

Generating SNILSes in factories for tests:

FactoryGirl.define do
  # You can generate random valid SNILSes
  sequence :snils do |_|
    Snils.generate
  end
  # Or sequenced ones
  sequence :snils do |counter|
    Snils.generate(counter)
  end

  factory :user do
    snils
  end
end

Recommended workflow for Ruby on Rails projects

  1. Use draper gem to format SNILS for views

    # app/decorators/user_decorator.rb
    class UserDecorator < Draper::Decorator
      delegate_all
    
      def snils
        @formatted_snils ||= Snils.new(object.snils).formatted
      end
    end
  2. Sanitize SNILSes on attribute write

    # app/models/user.rb
    class User < ActiveRecord::Base
      validates :snils, presence: true, uniqueness: true, snils: true
    
      def snils=(value)
        write_attribute(:snils, Snils.new(value).raw)
      end
    end

With this setup you will always store raw (only digits) value in database and always will show pretty formatted SNILS to users.

Contributing

  1. Fork it ( https://github.com/Envek/snils/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request