-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlegislator_vote.rb
101 lines (87 loc) · 2.06 KB
/
legislator_vote.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true
# typed: strict
# == Schema Information
#
# Table name: legislator_votes
#
# id :integer not null, primary key
# support :string not null
# created_at :datetime not null
# updated_at :datetime not null
# bill_id :integer not null
# legislator_id :integer not null
#
# Indexes
#
# index_legislator_votes_on_bill_id (bill_id)
# index_legislator_votes_on_legislator_id (legislator_id)
#
# Foreign Keys
#
# bill_id (bill_id => bills.id)
# legislator_id (legislator_id => legislators.id)
#
class LegislatorVote < ApplicationRecord
extend T::Sig
belongs_to :legislator
belongs_to :bill
after_initialize :transform_support_to_for_against_abstain, :upcase_support
validates :support, :bill_id, :legislator_id, presence: {message: "%{attribute} can't be blank"}
validates :support, inclusion: {in: %w[FOR AGAINST ABSTAIN]}
sig { returns(Bill) }
def bill
T.cast(super, Bill)
end
sig { returns(Legislator) }
def legislator
T.cast(super, Legislator)
end
sig { returns(T::Boolean) }
def for?
support == "FOR"
end
sig { returns(T::Boolean) }
def against?
support == "AGAINST"
end
sig { returns(T::Boolean) }
def abstain?
support.present? && !%w[FOR AGAINST].include?(support)
end
sig { returns(Jbuilder) }
def to_builder
Jbuilder.new do |lv|
lv.id id
lv.legislator_id legislator_id
lv.bill_id bill_id
lv.support support
end
end
private
sig { void }
def upcase_support
self.support = support.upcase.strip
end
sig { void }
def transform_support_to_for_against_abstain
s = support.downcase
case s
when "yea"
self.support = "FOR"
when "yes"
self.support = "FOR"
when "aye"
self.support = "FOR"
when "nay"
self.support = "AGAINST"
when "no"
self.support = "AGAINST"
when "not voting"
self.support = "ABSTAIN"
when "present"
self.support = "ABSTAIN"
else
s
end
end
end