diff --git a/app/models/orphan.rb b/app/models/orphan.rb index bd94ae2a..63a6dfb0 100644 --- a/app/models/orphan.rb +++ b/app/models/orphan.rb @@ -13,17 +13,25 @@ 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 @@ -31,7 +39,7 @@ class Orphan < ActiveRecord::Base 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 @@ -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 @@ -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 @@ -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 @@ -186,4 +193,4 @@ def being_inactivated? orphan_status_id_changed? && (OrphanStatus.find(orphan_status_id_was).name == 'Active') end end -end +end \ No newline at end of file diff --git a/db/migrate/20141110225313_add_father_alive_to_pending_orphans.rb b/db/migrate/20141110225319_add_father_alive_to_pending_orphans.rb similarity index 100% rename from db/migrate/20141110225313_add_father_alive_to_pending_orphans.rb rename to db/migrate/20141110225319_add_father_alive_to_pending_orphans.rb diff --git a/db/schema.rb b/db/schema.rb index 4c6e6e85..291e86c3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/features/admin/orphans.feature b/features/admin/orphans.feature index 4a87f53a..bc4809aa 100644 --- a/features/admin/orphans.feature +++ b/features/admin/orphans.feature @@ -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 @@ -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" diff --git a/spec/factories/orphans.rb b/spec/factories/orphans.rb index 4150a0be..b27546cc 100644 --- a/spec/factories/orphans.rb +++ b/spec/factories/orphans.rb @@ -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 } diff --git a/spec/fixtures/one_orphan_xls.xls b/spec/fixtures/one_orphan_xls.xls index ace07510..88b81d44 100644 Binary files a/spec/fixtures/one_orphan_xls.xls and b/spec/fixtures/one_orphan_xls.xls differ diff --git a/spec/fixtures/one_orphan_xlsx.xlsx b/spec/fixtures/one_orphan_xlsx.xlsx index c63bf5b2..2c9e479e 100644 Binary files a/spec/fixtures/one_orphan_xlsx.xlsx and b/spec/fixtures/one_orphan_xlsx.xlsx differ diff --git a/spec/models/orphan_spec.rb b/spec/models/orphan_spec.rb index 76821c88..38b07b54 100644 --- a/spec/models/orphan_spec.rb +++ b/spec/models/orphan_spec.rb @@ -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) } @@ -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