1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/person.rb

65 lines
1.4 KiB
Ruby
Raw Normal View History

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
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
validate :excluded_since_not_in_future
2018-12-15 00:51:44 -05:00
def party_supporter?
supporter_since.present? && !excluded_from_party?
end
def party_member?
member_since.present? && !excluded_from_party?
end
def excluded_from_party?
excluded_since.present?
end
2018-12-15 00:51:44 -05:00
private
def possible_member
return if member_since.nil?
errors.add :member_since if supporter_since.nil?
end
def possible_excluded
return if excluded_since.nil?
errors.add :excluded_since if supporter_since.nil? && member_since.nil?
end
2018-12-15 00:51:44 -05:00
def supporter_since_not_in_future
return if supporter_since.nil?
errors.add :supporter_since unless supporter_since <= Time.zone.today
end
2019-01-28 20:42:25 -05:00
def member_since_not_in_future
return if member_since.nil?
errors.add :member_since unless member_since <= Time.zone.today
end
def excluded_since_not_in_future
return if excluded_since.nil?
errors.add :excluded_since unless excluded_since <= Time.zone.today
end
2018-12-09 22:32:35 -05:00
end