Skip to content

Commit

Permalink
Merge pull request #3 from dougcole/import_candidates
Browse files Browse the repository at this point in the history
import candidates from tacl dataset
  • Loading branch information
dougcole authored Mar 22, 2018
2 parents f6090d4 + c2a3ba0 commit 0e7256b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/candidate.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Candidate < ApplicationRecord
belongs_to :contest
end
1 change: 1 addition & 0 deletions app/models/techandciviclife/candidate.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Techandciviclife::Candidate < ApplicationRecord
belongs_to :person, :primary_key => :internal_id
belongs_to :party, :primary_key => :internal_id
end
8 changes: 8 additions & 0 deletions app/models/techandciviclife/candidate_selection.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class Techandciviclife::CandidateSelection < ApplicationRecord
def primary_candidate
Techandciviclife::Candidate.where(internal_id: candidate_ids.first).first
end

def secondary_candidate
return nil if candidate_ids.length < 2
Techandciviclife::Candidate.where(internal_id: candidate_ids[1]).first
end
end
1 change: 1 addition & 0 deletions db/migrate/20180316222346_create_candidates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CreateCandidates < ActiveRecord::Migration[5.1]
def change
create_table :candidates do |t|
t.text :primary_name
t.integer :contest_id
t.text :primary_party
t.text :secondary_name
t.text :secondary_party
Expand Down
40 changes: 40 additions & 0 deletions lib/candidate_normalizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class CandidateNormalizer
def self.run!
Techandciviclife::CandidateSelection.find_each do |candidate_selection|

primary_candidate = candidate_selection.primary_candidate
secondary_candidate = candidate_selection.secondary_candidate

candidate = Candidate.new(
primary_name: primary_candidate.name,
candidate_selection_id: candidate_selection.internal_id,
contest: contest_id(candidate_selection)
)

if primary_candidate.party.present?
candidate.primary_party = primary_candidate.party.name
end

if secondary_candidate.present?
candidate.secondary_name = secondary_candidate.name
if secondary_candidate.party.present?
candidate.secondary_party = secondary_candidate.party.name
end
end

candidate.save!
end
end

def self.contest_id(candidate_selection)
candidate_contests = Techandciviclife::CandidateContest.where(
"? = ANY (candidate_selections)",
candidate_selection.internal_id
).all

raise "WTF: #{candidate_selection.attributes.insepct}" if candidate_contests.length > 1
candidate_contest = candidate_contests.first
Contest.where(candidate_contest_id: candidate_contest.internal_id).first
end
end

0 comments on commit 0e7256b

Please sign in to comment.