1
0
Fork 0

Add columns Person#first_name, #middle_name, #last_name

This commit is contained in:
Alex Kotov 2019-03-26 05:53:57 +05:00
parent 835005106a
commit 44f3759887
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
5 changed files with 51 additions and 2 deletions

View File

@ -15,6 +15,11 @@ class Person < ApplicationRecord
through: :account,
source: :own_membership_app
before_validation :turn_blanks_into_nils
validates :first_name, presence: true
validates :last_name, presence: true
validate :membership_is_possible
validate :membership_dates_are_not_in_future
@ -32,6 +37,10 @@ class Person < ApplicationRecord
private
def turn_blanks_into_nils
self.middle_name = nil if middle_name.blank?
end
def membership_is_possible
errors.add :member_since unless member_since_not_before_supporter_since?

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
class AddNamesToPeople < ActiveRecord::Migration[6.0]
def change
# rubocop:disable Rails/NotNullColumn
add_column :people, :first_name, :string, null: false
add_column :people, :middle_name, :string
add_column :people, :last_name, :string, null: false
# rubocop:enable Rails/NotNullColumn
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_03_25_230751) do
ActiveRecord::Schema.define(version: 2019_03_26_004506) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -130,6 +130,9 @@ ActiveRecord::Schema.define(version: 2019_03_25_230751) do
t.date "supporter_since"
t.date "member_since"
t.date "excluded_since"
t.string "first_name", null: false
t.string "middle_name"
t.string "last_name", null: false
t.index ["regional_office_id"], name: "index_people_on_regional_office_id"
end

View File

@ -1,7 +1,11 @@
# frozen_string_literal: true
FactoryBot.define do
factory :initial_person, class: Person
factory :initial_person, class: Person do
first_name { Faker::Name.first_name }
middle_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
end
factory :supporter_person, parent: :initial_person do
supporter_since { rand(1000..10_000).days.ago.to_date }

View File

@ -32,6 +32,28 @@ RSpec.describe Person do
it { is_expected.not_to validate_presence_of :regional_office }
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 }
describe '#middle_name' do
context 'when it is empty' do
subject { create :member_person, middle_name: '' }
specify do
expect(subject.middle_name).to eq nil
end
end
context 'when it is blank' do
subject { create :member_person, middle_name: ' ' }
specify do
expect(subject.middle_name).to eq nil
end
end
end
describe '#supporter_since' do
def allow_value(*)
super.for :supporter_since