1
0
Fork 0

Allow nil for Person#sex, #date_of_birth, #place_of_birth

This commit is contained in:
Alex Kotov 2019-07-26 02:15:40 +05:00
parent 3ce0586ed3
commit 3f94c7656b
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
11 changed files with 106 additions and 35 deletions

View File

@ -6,16 +6,17 @@ module Nameable
included do included do
pg_enum :sex, %i[male female] 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 :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 end
def turn_blank_middle_name_into_nil def turn_blank_nameable_attributes_into_nils
self.middle_name = nil if middle_name.blank? %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
end end

View File

@ -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

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class Passport < ApplicationRecord class Passport < ApplicationRecord
include Nameable include RequiredNameable
FORMAT_RE = /\A[^[:space:]]+(.*[^[:space:]]+)?\z/.freeze FORMAT_RE = /\A[^[:space:]]+(.*[^[:space:]]+)?\z/.freeze

View File

@ -32,10 +32,12 @@
<td><%= truncate person.middle_name, length: 15 %></td> <td><%= truncate person.middle_name, length: 15 %></td>
<td><%= truncate person.last_name, length: 15 %></td> <td><%= truncate person.last_name, length: 15 %></td>
<td class="d-none d-lg-table-cell"> <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>
<td class="d-none d-md-table-cell"> <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>
<td> <td>
<% if policy([:staff, person]).show? %> <% if policy([:staff, person]).show? %>

View File

@ -12,13 +12,13 @@
<dd><%= truncate @person.last_name %></dd> <dd><%= truncate @person.last_name %></dd>
<dt><%= Person.human_attribute_name :sex %></dt> <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> <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> <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> <dt><%= Person.human_attribute_name :current_regional_office %></dt>
<dd> <dd>

View File

@ -102,9 +102,9 @@ class InitialMigration < ActiveRecord::Migration[6.0]
t.string :first_name, null: false t.string :first_name, null: false
t.string :middle_name, null: true t.string :middle_name, null: true
t.string :last_name, null: false t.string :last_name, null: false
t.column :sex, :sex, null: false t.column :sex, :sex, null: true
t.date :date_of_birth, null: false t.date :date_of_birth, null: true
t.string :place_of_birth, null: false t.string :place_of_birth, null: true
t.references :contacts_list, t.references :contacts_list,
null: false, index: { unique: true }, foreign_key: true null: false, index: { unique: true }, foreign_key: true

View File

@ -459,9 +459,9 @@ CREATE TABLE public.people (
first_name character varying NOT NULL, first_name character varying NOT NULL,
middle_name character varying, middle_name character varying,
last_name character varying NOT NULL, last_name character varying NOT NULL,
sex public.sex NOT NULL, sex public.sex,
date_of_birth date NOT NULL, date_of_birth date,
place_of_birth character varying NOT NULL, place_of_birth character varying,
contacts_list_id bigint NOT NULL contacts_list_id bigint NOT NULL
); );

View File

@ -5,7 +5,7 @@ require 'rails_helper'
RSpec.describe Passport do RSpec.describe Passport do
subject { create :empty_passport } subject { create :empty_passport }
it_behaves_like 'nameable' it_behaves_like 'required_nameable'
describe '#person' do describe '#person' do
it { is_expected.to belong_to(:person).optional } it { is_expected.to belong_to(:person).optional }

View File

@ -1,27 +1,40 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'nameable' do 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.not_to validate_presence_of :middle_name }
it { is_expected.to validate_presence_of :last_name } it { is_expected.not_to validate_presence_of :sex }
it { is_expected.to validate_presence_of :sex } it { is_expected.not_to validate_presence_of :date_of_birth }
it { is_expected.to validate_presence_of :date_of_birth } it { is_expected.not_to validate_presence_of :place_of_birth }
it { is_expected.to validate_presence_of :place_of_birth }
describe '#middle_name' do %i[
context 'when it is empty' do last_name
subject { create :member_person, middle_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 before { subject.validate }
expect(subject.middle_name).to eq nil
specify do
expect(subject.public_send(attribute)).to eq nil
end
end end
end
context 'when it is blank' do context 'when it is blank' do
subject { create :member_person, middle_name: ' ' } subject { build :member_person, attribute => ' ' }
specify do before { subject.validate }
expect(subject.middle_name).to eq nil
specify do
expect(subject.public_send(attribute)).to eq nil
end
end end
end end
end end

View File

@ -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

View File

@ -38,6 +38,7 @@ require_relative 'support/devise'
require_relative 'support/pundit' require_relative 'support/pundit'
require_relative 'models/shared_examples/nameable' require_relative 'models/shared_examples/nameable'
require_relative 'models/shared_examples/required_nameable'
# Checks for pending migrations and applies them before tests are run. # Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines. # If you are not using ActiveRecord, you can remove these lines.