2018-12-09 22:32:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Person < ApplicationRecord
|
2018-12-15 00:54:45 -05:00
|
|
|
belongs_to :regional_office, optional: true
|
2018-12-14 23:09:43 -05:00
|
|
|
|
2018-12-09 22:36:09 -05:00
|
|
|
has_one :account, dependent: :restrict_with_exception
|
2018-12-14 23:20:13 -05:00
|
|
|
|
|
|
|
has_one :own_membership_app,
|
|
|
|
class_name: 'MembershipApp',
|
|
|
|
inverse_of: :person,
|
|
|
|
through: :account,
|
|
|
|
source: :own_membership_app
|
2018-12-15 00:34:46 -05:00
|
|
|
|
2019-01-30 18:47:12 -05:00
|
|
|
validate :possible_member
|
|
|
|
validate :possible_excluded
|
|
|
|
|
2018-12-15 00:51:44 -05:00
|
|
|
validate :supporter_since_not_in_future
|
2019-01-28 20:42:25 -05:00
|
|
|
validate :member_since_not_in_future
|
2019-01-28 21:13:25 -05:00
|
|
|
validate :excluded_since_not_in_future
|
2018-12-15 00:51:44 -05:00
|
|
|
|
2018-12-15 00:34:46 -05:00
|
|
|
def party_supporter?
|
2019-01-28 21:13:25 -05:00
|
|
|
supporter_since.present? && !excluded_from_party?
|
2018-12-15 00:34:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def party_member?
|
2019-01-28 21:13:25 -05:00
|
|
|
member_since.present? && !excluded_from_party?
|
2018-12-15 00:34:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def excluded_from_party?
|
2019-01-28 21:13:25 -05:00
|
|
|
excluded_since.present?
|
2018-12-15 00:34:46 -05:00
|
|
|
end
|
2018-12-15 00:51:44 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-30 18:47:12 -05:00
|
|
|
def possible_member
|
|
|
|
return if member_since.nil?
|
|
|
|
|
2019-01-30 19:20:28 -05:00
|
|
|
return errors.add :member_since if supporter_since.nil?
|
|
|
|
return errors.add :member_since if member_since < supporter_since
|
|
|
|
|
|
|
|
nil
|
2019-01-30 18:47:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def possible_excluded
|
|
|
|
return if excluded_since.nil?
|
|
|
|
|
2019-01-30 19:20:28 -05:00
|
|
|
return errors.add :excluded_since if supporter_since.nil? &&
|
|
|
|
member_since.nil?
|
|
|
|
return errors.add :excluded_since if supporter_since &&
|
|
|
|
excluded_since < supporter_since
|
|
|
|
return errors.add :excluded_since if member_since &&
|
|
|
|
excluded_since < member_since
|
|
|
|
|
|
|
|
nil
|
2019-01-30 18:47:12 -05:00
|
|
|
end
|
|
|
|
|
2018-12-15 00:51:44 -05:00
|
|
|
def supporter_since_not_in_future
|
2019-01-30 19:25:18 -05:00
|
|
|
errors.add :supporter_since unless supporter_since_not_in_future?
|
2018-12-15 00:51:44 -05:00
|
|
|
end
|
2019-01-28 20:42:25 -05:00
|
|
|
|
|
|
|
def member_since_not_in_future
|
2019-01-30 19:25:18 -05:00
|
|
|
errors.add :member_since unless member_since_not_in_future?
|
2019-01-28 20:42:25 -05:00
|
|
|
end
|
2019-01-28 21:13:25 -05:00
|
|
|
|
|
|
|
def excluded_since_not_in_future
|
2019-01-30 19:25:18 -05:00
|
|
|
errors.add :excluded_since unless excluded_since_not_in_future?
|
|
|
|
end
|
|
|
|
|
|
|
|
def supporter_since_not_in_future?
|
|
|
|
supporter_since.nil? || supporter_since <= Time.zone.today
|
|
|
|
end
|
|
|
|
|
|
|
|
def member_since_not_in_future?
|
|
|
|
member_since.nil? || member_since <= Time.zone.today
|
|
|
|
end
|
2019-01-28 21:13:25 -05:00
|
|
|
|
2019-01-30 19:25:18 -05:00
|
|
|
def excluded_since_not_in_future?
|
|
|
|
excluded_since.nil? || excluded_since <= Time.zone.today
|
2019-01-28 21:13:25 -05:00
|
|
|
end
|
2018-12-09 22:32:35 -05:00
|
|
|
end
|