Skip to content

Commit

Permalink
Added validations for father_alive, father_is_martyr and father_date_…
Browse files Browse the repository at this point in the history
…of_death values
  • Loading branch information
motasem-salem committed Nov 11, 2014
1 parent ff9a885 commit 5bde386
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
35 changes: 21 additions & 14 deletions app/models/orphan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,33 @@ class Orphan < ActiveRecord::Base

validates :name, presence: true
validates :father_name, presence: true
validates :father_is_martyr, inclusion: {in: [true, false] }, exclusion: { in: [nil]}
# Temporarily disabling validation to make tests pass.
# Proper validation will be submitted in another PR.
#validates :father_date_of_death, presence: true, date_not_in_future: true

validates :father_alive, inclusion: { in: [true, false] }, exclusion: { in: [nil] }
validates :father_alive, inclusion: { in: [false] }, exclusion: { in: [true] }, if: :father_is_martyr
validates :father_alive, inclusion: { in: [true] }, exclusion: { in: [false] }, if: 'father_date_of_death.nil?'
validates :father_alive, inclusion: { in: [false] }, exclusion: { in: [true] }, unless: 'father_date_of_death.nil?'

validates :father_is_martyr, inclusion: { in: [true, false] }, exclusion: { in: [nil] }
validates :father_is_martyr, inclusion: { in: [false] }, exclusion: { in: [true] }, if: :father_alive
validates :father_is_martyr, inclusion: { in: [false] }, exclusion: { in: [true] }, if: 'father_date_of_death.nil?'

validates :father_date_of_death, presence: true, date_not_in_future: true, unless: :father_alive
validates :father_date_of_death, absence: true, if: :father_alive

validates :mother_name, presence: true
validates :mother_alive, inclusion: {in: [true, false] }, exclusion: { in: [nil]}
validates :father_alive, inclusion: {in: [true, false] }, exclusion: { in: [nil]}
validates :mother_alive, inclusion: { in: [true, false] }, exclusion: { in: [nil] }
validates :date_of_birth, presence: true, date_not_in_future: true
validates :gender, presence: true, inclusion: {in: Settings.lookup.gender }
validates :gender, presence: true, inclusion: { in: Settings.lookup.gender }
validates :contact_number, presence: true
validates :sponsored_by_another_org, inclusion: {in: [true, false] }, exclusion: { in: [nil]}
validates :sponsored_by_another_org, inclusion: { in: [true, false] }, exclusion: { in: [nil] }
validates :minor_siblings_count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :original_address, presence: true
validates :current_address, presence: true
validates :orphan_status, presence: true
validates :priority, presence: true, inclusion: { in: %w(Normal High) }
validates :orphan_sponsorship_status, presence: true
validates :orphan_list, presence: true
validate :orphans_dob_within_1yr_of_fathers_death
validate :orphans_dob_within_1yr_of_fathers_death, unless: :father_alive
validate :less_than_22_yo_when_joined_osra
validate :can_be_inactivated, if: :being_inactivated?, on: :update

Expand Down Expand Up @@ -108,11 +116,11 @@ def default_sponsorship_status_unsponsored
end

def default_orphan_status_active
self.orphan_status ||= OrphanStatus.find_by_name 'Active'
self.orphan_status ||= OrphanStatus.find_by_name 'Active'
end

def valid_date? date
begin
begin
Date.parse(date.to_s)
rescue ArgumentError
return false
Expand All @@ -122,7 +130,6 @@ def valid_date? date
def default_priority_to_normal
self.priority ||= 'Normal'
end


def set_province_code
self.province_code = partner_province_code
Expand Down Expand Up @@ -171,7 +178,7 @@ def previously_sponsored?
end

def set_sponsorship_status(status_name)
sponsorship_status = OrphanSponsorshipStatus.find_by_name(status_name)
sponsorship_status = OrphanSponsorshipStatus.find_by_name(status_name)
self.orphan_sponsorship_status = sponsorship_status
end

Expand All @@ -186,4 +193,4 @@ def being_inactivated?
orphan_status_id_changed? && (OrphanStatus.find(orphan_status_id_was).name == 'Active')
end
end
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141110225318) do
ActiveRecord::Schema.define(version: 20141110225319) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -241,8 +241,8 @@
t.integer "requested_orphan_count"
t.boolean "request_fulfilled", default: false, null: false
t.integer "agent_id"
t.string "payment_plan", default: "", null: false
t.string "city"
t.string "payment_plan", default: "", null: false
end

add_index "sponsors", ["agent_id"], name: "index_sponsors_on_agent_id", using: :btree
Expand Down
4 changes: 2 additions & 2 deletions features/admin/orphans.feature
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Feature:
And I fill in "Father occupation" with "Another Occupation"
And I fill in "Father place of death" with "Another Place"
And I fill in "Father cause of death" with "Another Cause"
And I fill in "Father date of death" with "2013-03-15"
And I fill in "Father date of death" with ""
And I fill in "Mother name" with "Mother N"
And I select "Male" from the drop down box for "Gender"
And I uncheck the "Father is martyr" checkbox
Expand Down Expand Up @@ -89,7 +89,7 @@ Feature:
And I should see "Father Occupation" set to "Another Occupation"
And I should see "Father Place Of Death" set to "Another Place"
And I should see "Father Cause Of Death" set to "Another Cause"
And I should see "Father Date Of Death" set to "March 15, 2013"
And I should see "Father Date Of Death" set to ""
And I should see "Mother Name" set to "Mother N"
And I should see "Gender" set to "Male"
And I should see "Father Is Martyr" set to "No"
Expand Down
5 changes: 2 additions & 3 deletions spec/factories/orphans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
factory :orphan do
name { Faker::Name.name }
father_name { Faker::Name.name }
father_is_martyr { [true, false].sample }
father_date_of_death { 4.days.ago }
father_is_martyr { false }
mother_name { Faker::Name.name }
mother_alive { [true, false].sample }
father_alive { [true, false].sample }
father_alive { true }
date_of_birth { 10.years.ago }
gender { %w(Male Female).sample }
contact_number { Faker::PhoneNumber.phone_number }
Expand Down
Binary file modified spec/fixtures/one_orphan_xls.xls
Binary file not shown.
Binary file modified spec/fixtures/one_orphan_xlsx.xlsx
Binary file not shown.
35 changes: 24 additions & 11 deletions spec/models/orphan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@
it { is_expected.to validate_presence_of :father_name }
it { is_expected.to_not allow_value(nil).for(:father_is_martyr) }

# Temporarily disabling validation to make tests pass.
# Proper validation will be submitted in another PR.

# it { is_expected.to validate_presence_of :father_date_of_death }
# it { is_expected.to allow_value(Date.today - 5, Date.current).for(:father_date_of_death) }
# it { is_expected.to_not allow_value(Date.today + 5).for(:father_date_of_death) }
# [7, 'yes', true].each do |bad_date_value|
# it { is_expected.to_not allow_value(bad_date_value).for :father_date_of_death }
# end

it { is_expected.to validate_presence_of :mother_name }
it { is_expected.to_not allow_value(nil).for(:mother_alive) }
it { is_expected.to_not allow_value(nil).for(:father_alive) }
Expand Down Expand Up @@ -77,8 +67,31 @@

it { is_expected.to have_one(:partner).through(:orphan_list).autosave(false) }


describe 'validate father_alive, father_is_martyr, father_date_of_death values' do
let(:orphan) { create :orphan }

it 'if father_alive then father_is_martyr must be false & father_date_of_birth must be blank' do
orphan.father_alive = true

expect(orphan).to allow_value(false).for :father_is_martyr
expect(orphan).to_not allow_value(true).for :father_is_martyr
expect(orphan).to validate_absence_of :father_date_of_death
end

it 'if father_alive is false, then father_date_of_death must be present and should be in the past' do
orphan.father_alive = false

expect(orphan).to validate_presence_of :father_date_of_death
expect(orphan).to allow_value(5.days.ago, Date.current).for :father_date_of_death
[7, 'yes', true, 5.days.from_now].each do |bad_date_value|
expect(orphan).to_not allow_value(bad_date_value).for :father_date_of_death
end
end
end

describe '#orphans_dob_within_1yr_of_fathers_death' do
let(:orphan) { create :orphan, :father_date_of_death => (1.year + 1.day).ago }
let(:orphan) { create :orphan, father_alive: false, :father_date_of_death => (1.year + 1.day).ago }

it "is valid when orphan is born a year after fathers death" do
orphan.date_of_birth = 1.day.ago
Expand Down

0 comments on commit 5bde386

Please sign in to comment.