Skip to content

Commit

Permalink
refactor districts controller
Browse files Browse the repository at this point in the history
  • Loading branch information
turino committed Feb 21, 2015
1 parent fabb07d commit 7a09996
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 283 deletions.
42 changes: 8 additions & 34 deletions app/controllers/v1/districts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
class V1::DistrictsController < V1::BaseController
def index
@results = {}
if address = params[:address].presence
output = get_coords(address, params[:city], params[:state], params[:zip])
if coords = output[:coordinates]
district_info = get_district(coords)
district = District.find_by_state_and_district(district_info)
output[:targeted] = district.try(:targeted?)
output = output.merge(district_info)
@results = get_coords(address, params[:city], params[:state], params[:zip])
if coords = @results[:coordinates]
@district = District.find_by_state_and_district(get_district(coords))
end

elsif zip = params[:zip].presence
output = district_info_for_zip(zip)
else
output = { error: 'zip code is required' }
@zip_code = ZipCode.includes(:target_reps, :target_senators).find_by(zip_code: params[:zip])
@district = @zip_code.try(:single_district)
end

render json: output, status: 200
render
end

private

def district_info_for_zip(zip)
output = {}
if zip_code = ZipCode.includes(:districts, :campaigns).find_by(zip_code: zip)
if zip_code.targeted?
if district = zip_code.single_district
output[:state] = district.state.abbrev
output[:district] = district.district
output[:target_legislators] = { representative: district.representative }
output[:targeted] = true
else
output[:state] = zip_code.state.abbrev
output[:city] = zip_code.city
output[:targeted] = nil
end
else
output[:targeted] = false
end
end
output
end

def get_district(coords)
Integration::MobileCommons.district_from_coords(coords)
end
Expand All @@ -51,4 +25,4 @@ def get_coords(address, city, state, zip)
state: state,
zip: zip )
end
end
end
7 changes: 7 additions & 0 deletions app/models/district.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

class District < ActiveRecord::Base
belongs_to :state
has_many :senators, through: :state
has_many :target_senators, -> { targeted }, through: :state
has_and_belongs_to_many :zip_codes
has_one :representative, class_name: "Legislator"
has_one :target_rep, -> { targeted }, class_name: "Legislator"
has_many :campaigns, through: :representative

validates :state, presence: true
Expand All @@ -30,6 +33,10 @@ def targeted?
campaigns.active.any?
end

def target_legislators
target_senators + [target_rep].compact
end

def targeted_by_campaign?(campaign)
campaigns.include?(campaign)
end
Expand Down
18 changes: 14 additions & 4 deletions app/models/legislator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Legislator < ActiveRecord::Base
scope :senate, -> { where(district: nil) }
scope :house, -> { where(state: nil) }
scope :eligible, -> { where('term_end < ?', 2.years.from_now) }
scope :targeted, -> { joins(:campaigns).merge(Campaign.active) }

def self.fetch_one(bioguide_id: nil, district: nil, state: nil,
senate_class: nil)
Expand Down Expand Up @@ -67,17 +68,26 @@ def representative?
end

def name
first = self.verified_first_name || nickname || first_name
last = self.verified_last_name || last_name
first = verified_first_name || nickname || first_name
last = verified_last_name || last_name
first + ' ' + last
end

def serializable_hash(options)
super( options.merge(methods: [:name], only: [:phone]))
def state_abbrev
state ? state.abbrev : district.state.abbrev
end

def district_code
district.district if district
end

private

def serializable_hash(options)
super(methods: [:name, :state_abbrev, :district_code],
only: [:phone, :party, :chamber, :state_rank]).merge(options || {})
end

def assign_district
if @district_code && representative?
self.district = District.find_by_state_and_district(state: @state_abbrev,
Expand Down
9 changes: 7 additions & 2 deletions app/models/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ class State < ActiveRecord::Base
has_many :districts
has_many :zip_codes
has_many :senators, class_name: "Legislator"
has_many :target_senators, -> { targeted }, class_name: "Legislator"
has_many :campaigns, through: :senators

validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :abbrev, presence: true, uniqueness: { case_sensitive: false }

def eligible_senator
senators.eligible.first
end

def target_senator
target_senators.first
end

def targeted?
campaigns.active.any?
end

def to_s
abbrev
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/zip_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

class ZipCode < ActiveRecord::Base
belongs_to :state
has_many :senators, through: :state
has_many :target_senators, -> { targeted }, through: :state
has_and_belongs_to_many :districts
has_many :representatives, through: :districts
has_many :target_reps, -> { targeted }, through: :districts
has_many :campaigns, through: :districts

validates :state, presence: true
Expand All @@ -34,6 +38,10 @@ def targeted?
campaigns.active.any?
end

def target_legislators
target_senators + target_reps
end

def targeted_by_campaign?(campaign)
campaigns.include?(campaign)
end
Expand Down
14 changes: 14 additions & 0 deletions app/views/v1/districts/index.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if @district
json.state(@district.state.abbrev)
json.district(@district.district)
json.target_legislators(@district.target_legislators.as_json(local: true))
json.targeted(@district.targeted? || @district.state.targeted?)
json.address(@results[:address])
json.confidence(@results[:confidence])
elsif @zip_code && @zip_code.targeted?
json.address_required(true)
json.city(@zip_code.city)
json.state(@zip_code.state)
else
json.address_required(true)
end
68 changes: 0 additions & 68 deletions public/index.html

This file was deleted.

Loading

0 comments on commit 7a09996

Please sign in to comment.