Allow nil for Person#sex, #date_of_birth, #place_of_birth
This commit is contained in:
parent
3ce0586ed3
commit
3f94c7656b
|
@ -6,16 +6,17 @@ module Nameable
|
|||
included do
|
||||
pg_enum :sex, %i[male female]
|
||||
|
||||
before_validation :turn_blank_middle_name_into_nil
|
||||
before_validation :turn_blank_nameable_attributes_into_nils
|
||||
|
||||
validates :last_name, presence: true
|
||||
validates :first_name, presence: true
|
||||
validates :last_name, presence: true
|
||||
validates :sex, presence: true
|
||||
validates :date_of_birth, presence: true
|
||||
validates :place_of_birth, presence: true
|
||||
end
|
||||
|
||||
def turn_blank_middle_name_into_nil
|
||||
self.middle_name = nil if middle_name.blank?
|
||||
def turn_blank_nameable_attributes_into_nils
|
||||
%i[
|
||||
last_name first_name middle_name sex date_of_birth place_of_birth
|
||||
].each do |attribute|
|
||||
public_send "#{attribute}=", nil if public_send(attribute).blank?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module RequiredNameable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include Nameable
|
||||
|
||||
included do
|
||||
validates :sex, presence: true
|
||||
validates :date_of_birth, presence: true
|
||||
validates :place_of_birth, presence: true
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Passport < ApplicationRecord
|
||||
include Nameable
|
||||
include RequiredNameable
|
||||
|
||||
FORMAT_RE = /\A[^[:space:]]+(.*[^[:space:]]+)?\z/.freeze
|
||||
|
||||
|
|
|
@ -32,10 +32,12 @@
|
|||
<td><%= truncate person.middle_name, length: 15 %></td>
|
||||
<td><%= truncate person.last_name, length: 15 %></td>
|
||||
<td class="d-none d-lg-table-cell">
|
||||
<%= localize person.date_of_birth %>
|
||||
<%= localize person.date_of_birth if person.date_of_birth %>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell">
|
||||
<%= truncate person.place_of_birth, length: 15 %>
|
||||
<% if person.place_of_birth %>
|
||||
<%= truncate person.place_of_birth, length: 15 %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if policy([:staff, person]).show? %>
|
||||
|
|
|
@ -12,13 +12,13 @@
|
|||
<dd><%= truncate @person.last_name %></dd>
|
||||
|
||||
<dt><%= Person.human_attribute_name :sex %></dt>
|
||||
<dd><%= translate_enum :sex, @person.sex %></dd>
|
||||
<dd><%= translate_enum :sex, @person.sex if @person.sex %></dd>
|
||||
|
||||
<dt><%= Person.human_attribute_name :date_of_birth %></dt>
|
||||
<dd><%= localize @person.date_of_birth %></dd>
|
||||
<dd><%= localize @person.date_of_birth if @person.date_of_birth %></dd>
|
||||
|
||||
<dt><%= Person.human_attribute_name :place_of_birth %></dt>
|
||||
<dd><%= truncate @person.place_of_birth %></dd>
|
||||
<dd><%= truncate @person.place_of_birth if @person.place_of_birth %></dd>
|
||||
|
||||
<dt><%= Person.human_attribute_name :current_regional_office %></dt>
|
||||
<dd>
|
||||
|
|
|
@ -102,9 +102,9 @@ class InitialMigration < ActiveRecord::Migration[6.0]
|
|||
t.string :first_name, null: false
|
||||
t.string :middle_name, null: true
|
||||
t.string :last_name, null: false
|
||||
t.column :sex, :sex, null: false
|
||||
t.date :date_of_birth, null: false
|
||||
t.string :place_of_birth, null: false
|
||||
t.column :sex, :sex, null: true
|
||||
t.date :date_of_birth, null: true
|
||||
t.string :place_of_birth, null: true
|
||||
|
||||
t.references :contacts_list,
|
||||
null: false, index: { unique: true }, foreign_key: true
|
||||
|
|
|
@ -459,9 +459,9 @@ CREATE TABLE public.people (
|
|||
first_name character varying NOT NULL,
|
||||
middle_name character varying,
|
||||
last_name character varying NOT NULL,
|
||||
sex public.sex NOT NULL,
|
||||
date_of_birth date NOT NULL,
|
||||
place_of_birth character varying NOT NULL,
|
||||
sex public.sex,
|
||||
date_of_birth date,
|
||||
place_of_birth character varying,
|
||||
contacts_list_id bigint NOT NULL
|
||||
);
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ require 'rails_helper'
|
|||
RSpec.describe Passport do
|
||||
subject { create :empty_passport }
|
||||
|
||||
it_behaves_like 'nameable'
|
||||
it_behaves_like 'required_nameable'
|
||||
|
||||
describe '#person' do
|
||||
it { is_expected.to belong_to(:person).optional }
|
||||
|
|
|
@ -1,27 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.shared_examples 'nameable' do
|
||||
it { is_expected.to validate_presence_of :first_name }
|
||||
it { is_expected.to validate_presence_of :last_name }
|
||||
it { is_expected.to validate_presence_of :first_name }
|
||||
it { is_expected.not_to validate_presence_of :middle_name }
|
||||
it { is_expected.to validate_presence_of :last_name }
|
||||
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.not_to validate_presence_of :sex }
|
||||
it { is_expected.not_to validate_presence_of :date_of_birth }
|
||||
it { is_expected.not_to validate_presence_of :place_of_birth }
|
||||
|
||||
describe '#middle_name' do
|
||||
context 'when it is empty' do
|
||||
subject { create :member_person, middle_name: '' }
|
||||
%i[
|
||||
last_name
|
||||
first_name
|
||||
middle_name
|
||||
sex
|
||||
date_of_birth
|
||||
place_of_birth
|
||||
].each do |attribute|
|
||||
describe "##{attribute}" do
|
||||
context 'when it is empty' do
|
||||
subject { build :member_person, attribute => '' }
|
||||
|
||||
specify do
|
||||
expect(subject.middle_name).to eq nil
|
||||
before { subject.validate }
|
||||
|
||||
specify do
|
||||
expect(subject.public_send(attribute)).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is blank' do
|
||||
subject { create :member_person, middle_name: ' ' }
|
||||
context 'when it is blank' do
|
||||
subject { build :member_person, attribute => ' ' }
|
||||
|
||||
specify do
|
||||
expect(subject.middle_name).to eq nil
|
||||
before { subject.validate }
|
||||
|
||||
specify do
|
||||
expect(subject.public_send(attribute)).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.shared_examples 'required_nameable' do
|
||||
it { is_expected.to validate_presence_of :last_name }
|
||||
it { is_expected.to validate_presence_of :first_name }
|
||||
it { is_expected.not_to validate_presence_of :middle_name }
|
||||
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 }
|
||||
|
||||
%i[
|
||||
last_name
|
||||
first_name
|
||||
middle_name
|
||||
sex
|
||||
date_of_birth
|
||||
place_of_birth
|
||||
].each do |attribute|
|
||||
describe "##{attribute}" do
|
||||
context 'when it is empty' do
|
||||
subject { build :member_person, attribute => '' }
|
||||
|
||||
before { subject.validate }
|
||||
|
||||
specify do
|
||||
expect(subject.public_send(attribute)).to eq nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is blank' do
|
||||
subject { build :member_person, attribute => ' ' }
|
||||
|
||||
before { subject.validate }
|
||||
|
||||
specify do
|
||||
expect(subject.public_send(attribute)).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -38,6 +38,7 @@ require_relative 'support/devise'
|
|||
require_relative 'support/pundit'
|
||||
|
||||
require_relative 'models/shared_examples/nameable'
|
||||
require_relative 'models/shared_examples/required_nameable'
|
||||
|
||||
# Checks for pending migrations and applies them before tests are run.
|
||||
# If you are not using ActiveRecord, you can remove these lines.
|
||||
|
|
Reference in New Issue