Created
February 8, 2015 02:54
-
-
Save kahdojay/ef3fe0a374142f4bc1a9 to your computer and use it in GitHub Desktop.
openPAC code samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Legislator < ActiveRecord::Base | |
has_many :legislator_stances | |
has_many :stances, through: :legislator_stances | |
has_many :bill_votes | |
has_many :bills, through: :bill_votes | |
has_many :terms | |
has_one :alias | |
has_many :donations | |
has_many :legislator_issues | |
has_many :issues, through: :legislator_issues | |
validates_uniqueness_of :bioguide_id | |
validates_presence_of :bioguide_id, :first_name, :last_name | |
def name | |
"#{first_name} #{last_name}" | |
end | |
def current_state | |
self.terms.first.state | |
end | |
def current_chamber | |
self.terms.last.chamber.capitalize | |
end | |
def offical_prefix | |
if self.terms.last.chamber == "house" | |
return "Rep." | |
elsif self.terms.last.chamber == "senate" | |
return "Sen." | |
end | |
end | |
def get_issue_score(issue) | |
if self.legislator_issues.for_issue(issue) != [] | |
return self.legislator_issues.for_issue(issue).first.issue_score | |
end | |
end | |
def self.filter_legislators_by_vote_count(issue_id) | |
array = [] | |
BillVote.where(issue_id: issue_id).group(:legislator_id).count.sort_by {|k,v| v}.reverse.slice(0,300).to_h.each do |k,v| | |
array << Legislator.find(k) | |
end | |
array.sample(80) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment