Commit 14cbf509 authored by marcheing's avatar marcheing

Ignore the case when birthdate is nil for profile in validations

Added not null to the profile birthdate field.
The validation can raise an exception if birthdate is nil, but the
complexity of handling this corner case with the framework's design
decision of running every single validation, notwithstanding the result
of the other ones, would be too much to actually be worth it on the
majority of cases.
This type of thing cannot happen with any such model that is created or
updated through any interfaces of this application.
Signed-off-by: Diego de Araújo Martinez Camarinha's avatarDiego Araújo <[email protected]>
parent 2a80ebc6
Pipeline #3585392 passed with stage
in 5 minutes and 47 seconds
......@@ -7,9 +7,6 @@ class Profile < ApplicationRecord
validates :responsible_name, :responsible_document, presence: true, if: :underaged?
def adult?
# Rails will run every validation, even if the previous ones fail. This will ensure that the tests for validations
# will be able to progress even if the birthdate required attribute is nil.
return true if birthdate.nil?
((Time.zone.today - birthdate) / 365).to_i >= 18
end
......
class AddNotNullToProfileBirthdate < ActiveRecord::Migration[5.0]
def change
change_column_null :profiles, :birthdate, false
end
end
......@@ -11,12 +11,12 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160613121154) do
ActiveRecord::Schema.define(version: 20160624142640) do
create_table "profiles", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.date "birthdate"
t.date "birthdate", null: false
t.string "gender"
t.string "state"
t.string "city"
......
......@@ -4,10 +4,12 @@ RSpec.describe Profile, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
describe 'validations' do
subject { build(:profile) }
describe 'fields that are always required' do
%i(first_name last_name country state city zip_code birthdate gender occupation).each do |field|
%i(first_name last_name country state city zip_code gender occupation).each do |field|
it { is_expected.to validate_presence_of field }
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment