-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dougcole/import_candidates
import candidates from tacl dataset
- Loading branch information
Showing
5 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Candidate < ApplicationRecord | ||
belongs_to :contest | ||
end |
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
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 |
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
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 |
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
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
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 | ||
|