Move passport mappings into passports
This commit is contained in:
parent
0d39c24a86
commit
ab2bb16ccb
10 changed files with 40 additions and 120 deletions
|
@ -1,33 +1,21 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Passport < ApplicationRecord
|
||||
include Nameable
|
||||
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
||||
belongs_to :person, optional: true
|
||||
|
||||
has_many :passport_maps,
|
||||
dependent: :restrict_with_exception
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
||||
accepts_nested_attributes_for :passport_maps,
|
||||
reject_if: :blank_passport_map?
|
||||
|
||||
###########
|
||||
# Methods #
|
||||
###########
|
||||
|
||||
def passport_map
|
||||
passport_maps.order(created_at: :asc).last
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def blank_passport_map?(passport_map_attributes)
|
||||
passport_map_attributes.all? do |key, value|
|
||||
next true if key.start_with? 'date_'
|
||||
|
||||
value.blank?
|
||||
end
|
||||
end
|
||||
validates :series, presence: true
|
||||
validates :number, presence: true
|
||||
validates :issued_by, presence: true
|
||||
validates :unit_code, presence: true
|
||||
validates :date_of_issue, presence: true
|
||||
end
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class PassportMap < ApplicationRecord
|
||||
include Nameable
|
||||
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
||||
belongs_to :passport
|
||||
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
||||
validates :series, presence: true
|
||||
validates :number, presence: true
|
||||
validates :issued_by, presence: true
|
||||
validates :unit_code, presence: true
|
||||
validates :date_of_issue, presence: true
|
||||
end
|
|
@ -9,16 +9,16 @@
|
|||
<%= Passport.human_attribute_name :id %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= PassportMap.human_attribute_name :series %>
|
||||
<%= Passport.human_attribute_name :series %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= PassportMap.human_attribute_name :number %>
|
||||
<%= Passport.human_attribute_name :number %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= PassportMap.human_attribute_name :unit_code %>
|
||||
<%= Passport.human_attribute_name :unit_code %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= PassportMap.human_attribute_name :date_of_issue %>
|
||||
<%= Passport.human_attribute_name :date_of_issue %>
|
||||
</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
|
@ -28,10 +28,10 @@
|
|||
<% @passports.each do |passport| %>
|
||||
<tr>
|
||||
<td scope="row"><%= passport.id %></td>
|
||||
<td><%= passport.passport_map&.series %></td>
|
||||
<td><%= passport.passport_map&.number %></td>
|
||||
<td><%= passport.passport_map&.unit_code %></td>
|
||||
<td><%= passport.passport_map&.date_of_issue %></td>
|
||||
<td><%= passport.series %></td>
|
||||
<td><%= passport.number %></td>
|
||||
<td><%= passport.unit_code %></td>
|
||||
<td><%= passport.date_of_issue %></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
|
|
@ -10,9 +10,6 @@ en:
|
|||
passport:
|
||||
one: Passport
|
||||
many: Passports
|
||||
passport_map:
|
||||
one: Passport mapping
|
||||
many: Passport mappings
|
||||
person:
|
||||
one: Person
|
||||
many: People
|
||||
|
@ -41,8 +38,6 @@ en:
|
|||
native_name: Name
|
||||
passport:
|
||||
id: ID
|
||||
passport_map:
|
||||
id: ID
|
||||
first_name: First name
|
||||
middle_name: Middle name
|
||||
last_name: Last name
|
||||
|
@ -54,7 +49,7 @@ en:
|
|||
issued_by: Issued by
|
||||
unit_code: Unit code
|
||||
date_of_issue: Date of issue
|
||||
passport_map/sex:
|
||||
passport/sex:
|
||||
male: Male
|
||||
female: Female
|
||||
person:
|
||||
|
|
|
@ -10,9 +10,6 @@ ru:
|
|||
passport:
|
||||
one: Паспорт
|
||||
many: Паспорта
|
||||
passport_map:
|
||||
one: Отображение паспорта
|
||||
many: Отображения паспортов
|
||||
person:
|
||||
one: Человек
|
||||
many: Люди
|
||||
|
@ -41,8 +38,6 @@ ru:
|
|||
native_name: Название
|
||||
passport:
|
||||
id: ID
|
||||
passport_map:
|
||||
id: ID
|
||||
first_name: Имя
|
||||
middle_name: Отчество
|
||||
last_name: Фамилия
|
||||
|
@ -54,7 +49,7 @@ ru:
|
|||
issued_by: Кем выдан
|
||||
unit_code: Код подразделения
|
||||
date_of_issue: Дата выдачи
|
||||
passport_map/sex:
|
||||
passport/sex:
|
||||
male: Мужской
|
||||
female: Женский
|
||||
person:
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :passport_map do
|
||||
association :passport, factory: :empty_passport
|
||||
|
||||
first_name { Faker::Name.first_name }
|
||||
middle_name { Faker::Name.first_name }
|
||||
last_name { Faker::Name.last_name }
|
||||
sex { PassportMap.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
|
|
@ -4,14 +4,18 @@ FactoryBot.define do
|
|||
factory :empty_passport, class: Passport do
|
||||
association :person, factory: :initial_person
|
||||
|
||||
trait :with_map do
|
||||
after :create do |passport, _evaluator|
|
||||
create :passport_map, passport: passport
|
||||
end
|
||||
first_name { Faker::Name.first_name }
|
||||
middle_name { Faker::Name.first_name }
|
||||
last_name { Faker::Name.last_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
|
||||
|
||||
factory :passport_with_map,
|
||||
parent: :empty_passport,
|
||||
traits: %i[with_map]
|
||||
end
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe PassportMap do
|
||||
subject { create :passport_map }
|
||||
|
||||
it_behaves_like 'nameable'
|
||||
|
||||
it { is_expected.to belong_to :passport }
|
||||
|
||||
it { is_expected.to validate_presence_of(:passport).with_message(:required) }
|
||||
|
||||
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 }
|
||||
end
|
|
@ -5,13 +5,13 @@ require 'rails_helper'
|
|||
RSpec.describe Passport do
|
||||
subject { create :empty_passport }
|
||||
|
||||
it_behaves_like 'nameable'
|
||||
|
||||
it { is_expected.to belong_to(:person).optional }
|
||||
|
||||
it do
|
||||
is_expected.to \
|
||||
have_many(:passport_maps)
|
||||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
|
||||
pending '#passport_map'
|
||||
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 }
|
||||
end
|
||||
|
|
|
@ -8,8 +8,7 @@ RSpec.describe 'GET /staff/people/:person_id/passports' do
|
|||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
|
||||
create :empty_passport, person: person
|
||||
create :passport_with_map, person: person
|
||||
create :empty_passport, person: person
|
||||
|
||||
get "/staff/people/#{person.to_param}/passports"
|
||||
end
|
||||
|
|
Reference in a new issue