2018-12-09 22:32:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Person < ApplicationRecord
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
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
|
|
|
|
2019-01-30 22:04:42 -05:00
|
|
|
has_many :passports, dependent: :restrict_with_exception
|
|
|
|
|
2019-03-25 19:11:52 -04:00
|
|
|
has_many :resident_registrations, 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-03-25 20:56:31 -04:00
|
|
|
#############
|
|
|
|
# Callbacks #
|
|
|
|
#############
|
|
|
|
|
2019-03-25 20:53:57 -04:00
|
|
|
before_validation :turn_blanks_into_nils
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2019-03-25 20:53:57 -04:00
|
|
|
validates :first_name, presence: true
|
|
|
|
validates :last_name, presence: true
|
|
|
|
|
2019-01-30 19:35:46 -05:00
|
|
|
validate :membership_is_possible
|
|
|
|
validate :membership_dates_are_not_in_future
|
2018-12-15 00:51:44 -05:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###########
|
|
|
|
# Methods #
|
|
|
|
###########
|
|
|
|
|
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-03-25 20:53:57 -04:00
|
|
|
def turn_blanks_into_nils
|
|
|
|
self.middle_name = nil if middle_name.blank?
|
|
|
|
end
|
|
|
|
|
2019-01-30 19:35:46 -05:00
|
|
|
def membership_is_possible
|
2019-01-30 19:33:14 -05:00
|
|
|
errors.add :member_since unless member_since_not_before_supporter_since?
|
2019-01-30 18:47:12 -05:00
|
|
|
|
2019-01-30 19:33:14 -05:00
|
|
|
return if excluded_since_not_before_member_since? ||
|
|
|
|
excluded_since_not_before_supporter_since?
|
2019-01-30 19:20:28 -05:00
|
|
|
|
2019-01-30 19:33:14 -05:00
|
|
|
errors.add :excluded_since
|
2019-01-30 18:47:12 -05:00
|
|
|
end
|
|
|
|
|
2019-01-30 19:35:46 -05:00
|
|
|
def membership_dates_are_not_in_future
|
2019-01-30 19:25:18 -05:00
|
|
|
errors.add :supporter_since unless supporter_since_not_in_future?
|
|
|
|
errors.add :member_since unless member_since_not_in_future?
|
|
|
|
errors.add :excluded_since unless excluded_since_not_in_future?
|
|
|
|
end
|
|
|
|
|
2019-01-30 19:33:14 -05:00
|
|
|
def member_since_not_before_supporter_since?
|
|
|
|
member_since.nil? || supporter_since && member_since >= supporter_since
|
|
|
|
end
|
|
|
|
|
|
|
|
def excluded_since_not_before_supporter_since?
|
|
|
|
excluded_since.nil? || supporter_since && excluded_since >= supporter_since
|
|
|
|
end
|
|
|
|
|
|
|
|
def excluded_since_not_before_member_since?
|
|
|
|
excluded_since.nil? || member_since && excluded_since >= member_since
|
|
|
|
end
|
|
|
|
|
2019-01-30 19:25:18 -05:00
|
|
|
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
|