1
0
Fork 0

Set nil to blank attributes

This commit is contained in:
Alex Kotov 2018-11-28 19:05:58 +05:00
parent 86250c8125
commit 02e234f564
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
3 changed files with 99 additions and 0 deletions

View File

@ -19,6 +19,7 @@ Layout/EmptyLinesAroundArguments:
Metrics/BlockLength:
Exclude:
- 'config/initializers/*.rb'
- 'spec/**/*.rb'
Metrics/LineLength:
Exclude:

View File

@ -6,4 +6,12 @@ class MembershipApplication < ApplicationRecord
validates :date_of_birth, presence: true
validates :email, presence: true
validates :phone_number, presence: true
before_validation do
self.middle_name = nil if middle_name.blank?
self.occupation = nil if occupation.blank?
self.telegram_username = nil if telegram_username.blank?
self.organization_membership = nil if organization_membership.blank?
self.comment = nil if comment.blank?
end
end

View File

@ -15,4 +15,94 @@ RSpec.describe MembershipApplication, type: :model do
it { is_expected.not_to validate_presence_of :telegram_username }
it { is_expected.not_to validate_presence_of :organization_membership }
it { is_expected.not_to validate_presence_of :comment }
describe '#middle_name' do
context 'when it is empty' do
subject { create :membership_application, middle_name: '' }
specify do
expect(subject.middle_name).to eq nil
end
end
context 'when it is blank' do
subject { create :membership_application, middle_name: ' ' }
specify do
expect(subject.middle_name).to eq nil
end
end
end
describe '#occupation' do
context 'when it is empty' do
subject { create :membership_application, occupation: '' }
specify do
expect(subject.occupation).to eq nil
end
end
context 'when it is blank' do
subject { create :membership_application, occupation: ' ' }
specify do
expect(subject.occupation).to eq nil
end
end
end
describe '#telegram_username' do
context 'when it is empty' do
subject { create :membership_application, telegram_username: '' }
specify do
expect(subject.telegram_username).to eq nil
end
end
context 'when it is blank' do
subject { create :membership_application, telegram_username: ' ' }
specify do
expect(subject.telegram_username).to eq nil
end
end
end
describe '#organization_membership' do
context 'when it is empty' do
subject { create :membership_application, organization_membership: '' }
specify do
expect(subject.organization_membership).to eq nil
end
end
context 'when it is blank' do
subject { create :membership_application, organization_membership: ' ' }
specify do
expect(subject.organization_membership).to eq nil
end
end
end
describe '#comment' do
context 'when it is empty' do
subject { create :membership_application, comment: '' }
specify do
expect(subject.comment).to eq nil
end
end
context 'when it is blank' do
subject { create :membership_application, comment: ' ' }
specify do
expect(subject.comment).to eq nil
end
end
end
end