diff --git a/app/models/person.rb b/app/models/person.rb index 66462d8..cbc3c8f 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -11,6 +11,8 @@ class Person < ApplicationRecord through: :account, source: :own_membership_app + validate :supporter_since_not_in_future + def related_to_party? party_supporter? || party_member? || excluded_from_party? end @@ -26,4 +28,12 @@ class Person < ApplicationRecord def excluded_from_party? false end + +private + + def supporter_since_not_in_future + return if supporter_since.nil? + + errors.add :supporter_since unless supporter_since <= Time.zone.today + end end diff --git a/db/migrate/20181215053720_add_supporter_since_to_people.rb b/db/migrate/20181215053720_add_supporter_since_to_people.rb new file mode 100644 index 0000000..d96d03d --- /dev/null +++ b/db/migrate/20181215053720_add_supporter_since_to_people.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddSupporterSinceToPeople < ActiveRecord::Migration[5.2] + def change + add_column :people, :supporter_since, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 65bd8c8..e7eed19 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_12_15_040559) do +ActiveRecord::Schema.define(version: 2018_12_15_053720) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -127,6 +127,7 @@ ActiveRecord::Schema.define(version: 2018_12_15_040559) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "regional_office_id" + t.date "supporter_since" t.index ["regional_office_id"], name: "index_people_on_regional_office_id" end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index b9a2221..c1fcb29 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -24,4 +24,20 @@ RSpec.describe Person do pending '#party_supporter?' pending '#party_member?' pending '#excluded_from_party?' + + describe '#supporter_since' do + def allow_value(*) + super.for :supporter_since + end + + it { is_expected.not_to validate_presence_of :supporter_since } + + it { is_expected.to allow_value Time.zone.today } + it { is_expected.to allow_value Time.zone.yesterday } + it { is_expected.to allow_value rand(10_000).days.ago.to_date } + + it { is_expected.not_to allow_value Time.zone.tomorrow } + it { is_expected.not_to allow_value 1.day.from_now.to_date } + it { is_expected.not_to allow_value rand(10_000).days.from_now.to_date } + end end