Skip to content

Commit

Permalink
refactoring services
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed May 10, 2017
1 parent 9b3a75a commit 103bc35
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/controllers/office_locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class OfficeLocationsController < ApplicationController

def index
if geo_params.keys.any?
geo = GeoLookup.new geo_params
geo = GeoLookup.new geo_params.to_h.symbolize_keys
@office_locations = apply_scopes(geo.find_office_locations).each do |off|
off.calculate_distance(geo.coordinates.latlon)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/reps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RepsController < ApplicationController

# GET /reps
def index
geo = GeoLookup.new geo_params
geo = GeoLookup.new geo_params.to_h.symbolize_keys
if !geo.district.blank?
@reps = apply_scopes(geo.find_reps).each do |rep|
rep.sort_offices(geo.coordinates.latlon)
Expand Down
7 changes: 6 additions & 1 deletion app/services/coordinates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class Coordinates

attr_reader :latlon

def initialize(latlon: nil, lat: 0.0, long: 0.0)
def initialize(latlon: nil, lat: 0.0, long: 0.0, address: '')
@latlon = latlon ? Array(latlon) : [lat.to_f, long.to_f] - [0.0]
@latlon = Array(Geocoder.coordinates(address)) if @latlon.empty?
end

def each(&block)
Expand Down Expand Up @@ -37,6 +38,10 @@ def blank?
latlon.blank?
end

def find_district
find_district_geom.district
end

def find_district_geom
if latlon.empty?
NullObject.new
Expand Down
29 changes: 6 additions & 23 deletions app/services/geo_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ class GeoLookup
# Radius of the gem lookup for office_locations
attr_accessor :radius

def initialize(params = {})
lat = params.fetch(:lat) { 0.0 }
long = params.fetch(:long) { 0.0 }
self.address = params.fetch(:address) { '' }
self.radius = params.fetch(:radius) { 0.0 }.to_f
self.coordinates = Coordinates.new(lat: lat, long: long)
find_coordinates_by_address if coordinates.empty?
find_district_and_state
def initialize(address: '', lat: 0.0, long: 0.0, radius: 0.0)
self.address = address.to_s
self.radius = radius.to_f
self.coordinates = Coordinates.new(lat: lat, long: long, address: self.address)
self.district = coordinates.find_district
self.state = district.state
end

# Find the reps in the db associated to location, and sort the offices by distance.
Expand All @@ -37,19 +35,4 @@ def find_office_locations
return OfficeLocation.none if coordinates.latlon.empty?
self.office_locations = OfficeLocation.active.near coordinates.latlon, radius
end

private

# Geocode address into [lat, lon] coordinates.
def find_coordinates_by_address
self.coordinates = Coordinates.new(latlon: Geocoder.coordinates(address))
end

# Find the district geometry that contains the coordinates,
# and the district and state it belongs to.
def find_district_and_state
district_geom = coordinates.find_district_geom
self.district = district_geom.district
self.state = district.state
end
end

0 comments on commit 103bc35

Please sign in to comment.