From fabdc586eb3969031cf5c3b590f534f42c370491 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 30 Nov 2018 06:19:38 +0500 Subject: [PATCH] Add model Passport --- app/models/passport.rb | 20 ++++++++++ config/locales/activerecord/en.yml | 18 +++++++++ config/locales/activerecord/ru.yml | 18 +++++++++ db/migrate/20181130010029_create_passports.rb | 21 +++++++++++ db/schema.rb | 18 ++++++++- factories/passports.rb | 19 ++++++++++ spec/models/passport_spec.rb | 37 +++++++++++++++++++ 7 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 app/models/passport.rb create mode 100644 db/migrate/20181130010029_create_passports.rb create mode 100644 factories/passports.rb create mode 100644 spec/models/passport_spec.rb diff --git a/app/models/passport.rb b/app/models/passport.rb new file mode 100644 index 0000000..59d0d10 --- /dev/null +++ b/app/models/passport.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class Passport < ApplicationRecord + enum sexes: %i[male female] + + validates :surname, presence: true + validates :given_name, presence: true + validates :sex, presence: true + validates :date_of_birth, presence: true + validates :place_of_birth, presence: true + validates :series, presence: true + validates :number, presence: true + validates :issued_by, presence: true + validates :unit_code, presence: true + validates :date_of_issue, presence: true + + before_validation do + self.patronymic = nil if patronymic.blank? + end +end diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index 8ee979e..3d3ca99 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -4,6 +4,9 @@ en: membership_application: one: Membership application other: Membership applications + passport: + one: Passport + other: Passports user: one: User other: Users @@ -19,6 +22,21 @@ en: telegram_username: Telegram username organization_membership: Membership in other social organizations comment: Comment + passport: + surname: Surname + given_name: Given names + patronymic: Patronymic + sex: Sex + date_of_birth: Date of birth + place_of_birth: Place of birth + series: Series + number: Number + issued_by: Issued by + unit_code: Unit code + date_of_issue: Date of issue + passport/sex: + male: Male + female: Female user: confirmation_sent_at: Confirmation sent at confirmation_token: Confirmation token diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index cad4dfb..bf9ac6f 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -4,6 +4,9 @@ ru: membership_application: one: Заявление на вступление other: Заявления на вступление + passport: + one: Паспорт + other: Паспорта user: one: Пользователь other: Пользователи @@ -19,6 +22,21 @@ ru: telegram_username: Имя пользователя в Telegram organization_membership: Членство в других общественных организациях comment: Комментарий + passport: + surname: Фамилия + given_name: Имя + patronymic: Отчество + sex: Пол + date_of_birth: Дата рождения + place_of_birth: Место рождения + series: Серия + number: Номер + issued_by: Кем выдан + unit_code: Код подразделения + date_of_issue: Дата выдачи + passport/sex: + male: Мужской + female: Женский user: confirmation_sent_at: Дата отправки подтверждения confirmation_token: Токен подтверждения diff --git a/db/migrate/20181130010029_create_passports.rb b/db/migrate/20181130010029_create_passports.rb new file mode 100644 index 0000000..1628fc1 --- /dev/null +++ b/db/migrate/20181130010029_create_passports.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class CreatePassports < ActiveRecord::Migration[5.2] + def change + create_table :passports do |t| + t.timestamps null: false + + t.string :surname, null: false + t.string :given_name, null: false + t.string :patronymic + t.integer :sex, null: false + t.date :date_of_birth, null: false + t.string :place_of_birth, null: false + t.integer :series, null: false + t.integer :number, null: false + t.string :issued_by, null: false + t.string :unit_code, null: false + t.date :date_of_issue, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 40eb54b..d3f618a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_11_29_231814) do +ActiveRecord::Schema.define(version: 2018_11_30_010029) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -30,6 +30,22 @@ ActiveRecord::Schema.define(version: 2018_11_29_231814) do t.text "comment" end + create_table "passports", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "surname", null: false + t.string "given_name", null: false + t.string "patronymic" + t.integer "sex", null: false + t.date "date_of_birth", null: false + t.string "place_of_birth", null: false + t.integer "series", null: false + t.integer "number", null: false + t.string "issued_by", null: false + t.string "unit_code", null: false + t.date "date_of_issue", null: false + end + create_table "roles", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false diff --git a/factories/passports.rb b/factories/passports.rb new file mode 100644 index 0000000..229d7e0 --- /dev/null +++ b/factories/passports.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :passport do + surname { Faker::Name.last_name } + given_name { Faker::Name.first_name } + patronymic { Faker::Name.first_name } + sex { Passport.sexes.keys.sample } + date_of_birth { Faker::Date.backward } + place_of_birth { Faker::Address.city } + series { rand 0..9999 } + number { rand 0..999_999 } + issued_by { Faker::Lorem.sentence } + unit_code do + "#{rand(0..999).to_s.rjust(3, '0')}-#{rand(0..999).to_s.rjust(3, '0')}" + end + date_of_issue { Faker::Date.backward } + end +end diff --git a/spec/models/passport_spec.rb b/spec/models/passport_spec.rb new file mode 100644 index 0000000..cdfe41f --- /dev/null +++ b/spec/models/passport_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Passport, type: :model do + subject { create :passport } + + it { is_expected.to validate_presence_of :surname } + it { is_expected.to validate_presence_of :given_name } + it { is_expected.not_to validate_presence_of :patronymic } + it { is_expected.to validate_presence_of :sex } + it { is_expected.to validate_presence_of :date_of_birth } + it { is_expected.to validate_presence_of :place_of_birth } + it { is_expected.to validate_presence_of :series } + it { is_expected.to validate_presence_of :number } + it { is_expected.to validate_presence_of :issued_by } + it { is_expected.to validate_presence_of :unit_code } + it { is_expected.to validate_presence_of :date_of_issue } + + describe '#patronymic' do + context 'when it is empty' do + subject { create :passport, patronymic: '' } + + specify do + expect(subject.patronymic).to eq nil + end + end + + context 'when it is blank' do + subject { create :passport, patronymic: ' ' } + + specify do + expect(subject.patronymic).to eq nil + end + end + end +end