Skip to content

Commit

Permalink
find closest locations button on region landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Wainstock committed Oct 13, 2015
1 parent 84b8013 commit 331abec
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
23 changes: 22 additions & 1 deletion app/assets/javascripts/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,28 @@ function initSearch(region, locationIDs, locationTypeIDs, zoneIDs, machineIDs, i
}
}

function pinGeo(e) {
function findClosestLocations(region) {
navigator.geolocation.getCurrentPosition(function(position) {
var dfd = $.getJSON('/api/v1/locations/closest_by_lat_lon.json', {lat: position.coords.latitude, lon: position.coords.longitude, max_distance: 20, send_all_within_distance: 1});
dfd.done(function(response) {
if (!response.locations) {
document.getElementById("map_canvas").innerHTML="<p id='intro' class='dark hello'>" + "Sorry, there are no locations within 20 miles of you." + "</p>";
} else {
var locationIds = [];

for (var i = 0; i < response.locations.length; i++) {
locationIds.push(response.locations[i].id)
}

window.location.href = 'http://pinballmap.com/'+ region + '/?by_location_id=' + locationIds.join('_');
}
});
}, function() {
// User has blocked access to their location or something else went wrong
});
}

function findClosestRegion(e) {
navigator.geolocation.getCurrentPosition(function(position) {
var dfd = $.getJSON('/api/v1/regions/closest_by_lat_lon.json', {lat: position.coords.latitude, lon: position.coords.longitude});
dfd.done(function(response) {
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/api/v1/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ def update_phone(location, phone)
param :lat, String, desc: 'Latitude', required: true
param :lon, String, desc: 'Longitude', required: true
param :max_distance, String, desc: 'Closest location within "max_distance" miles', required: false
param :send_all_within_distance, String, desc: "Send all locations within max_distance param, or #{MAX_MILES_TO_SEARCH_FOR_CLOSEST_LOCATION} miles.", required: false
formats ['json']
def closest_by_lat_lon
max_distance = params[:max_distance] ||= MAX_MILES_TO_SEARCH_FOR_CLOSEST_LOCATION

closest_location = Location.near([params[:lat], params[:lon]], max_distance).first

if closest_location
if params[:send_all_within_distance]
closest_locations = Location.near([params[:lat], params[:lon]], max_distance)
return_response(closest_locations, 'locations', [], [:machine_names])
elsif closest_location
return_response(closest_location, 'location', [], [:machine_names])
else
return_response("No locations within #{max_distance} miles.", 'errors')
Expand Down
4 changes: 3 additions & 1 deletion app/views/pages/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
= link_to 'Blog', 'http://blog.pinballmap.com'
%span.darkb.store_header.store_header_region
= link_to 'T-Shirts', store_path
%span.darkb.store_header.store_header_region{:onclick => "findClosestLocations('#{@region.name}')"}
= link_to 'Close', '#', {:id => 'find_closest_locations'}
%div.region_logo_dropdown
%div.other_regions
%span.region_name_page
Expand All @@ -19,7 +21,7 @@
= link_to 'Apps', apps_path
%span.darkb.store_header
= link_to 'T-Shirts', store_path
%span.darkb.geo_header{:onclick => "pinGeo()"}
%span.darkb.geo_header{:onclick => "findClosestRegion()"}
= link_to 'Find Your Closest Map', '#'
- if (@region)
%ul.region_links
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/api/v1/locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@
expect(location['lon']).to eq('-122.63')
expect(location['machine_names']).to eq(%w(Bawb Cleo Sassy))
end

it 'sends you multiple locations if you use the send_all_within_distance flag' do
close_location_1 = FactoryGirl.create(:location, region: @region, lat: 45.49, lon: -122.63)
close_location_2 = FactoryGirl.create(:location, region: @region, lat: 45.49, lon: -122.631)
close_location_3 = FactoryGirl.create(:location, region: @region, lat: 45.491, lon: -122.63)
FactoryGirl.create(:location, region: @region, lat: 5.49, lon: 22.63)

get "/api/v1/locations/closest_by_lat_lon.json?lat=#{close_location_1.lat};lon=#{close_location_1.lon};send_all_within_distance=1"

parsed_body = JSON.parse(response.body)
expect(parsed_body.size).to eq(1)

locations = parsed_body['locations']
expect(locations[0]['id']).to eq(close_location_1.id)
expect(locations[1]['id']).to eq(close_location_2.id)
expect(locations[2]['id']).to eq(close_location_3.id)
end
end

describe '#machine_details' do
Expand Down

0 comments on commit 331abec

Please sign in to comment.