Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to maps per region #879

Merged
merged 3 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/controllers/maps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ def exports
def region
area = params[:id] || 'this area'
@title = "Maps in #{area}"
ids = Map.bbox(params[:minlat], params[:minlon], params[:maxlat], params[:maxlon]).collect(&:id)
@maps = Map.where(password: '')
.where('id IN (?)', ids)
.paginate(page: params[:page], per_page: 24)
.except(:styles, :email)
ids = Map.bbox(params[:minlat], params[:minlon], params[:maxlat], params[:maxlon], params[:tag])
.collect(&:id)
cache_key = "#{params[:minlat]}-#{params[:maxlat]}-#{params[:minlon]}-#{params[:maxlon]}-#{params[:tag]}"
@maps =
Rails.cache.fetch(cache_key, expires_in: 1.day) do
Map.where(password: '')
.where('id IN (?)', ids)
.paginate(page: params[:page], per_page: 50)
.except(:styles, :email)
end
@maps.each do |map|
map.image_urls = map.warpables.map { |warpable| warpable.image.url }
end
Expand Down
12 changes: 9 additions & 3 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ def self.anonymous
Map.where(user_id: 0)
end

def self.bbox(minlat, minlon, maxlat, maxlon)
Map.where(['lat > ? AND lat < ? AND lon > ? AND lon < ?',
minlat, maxlat, minlon, maxlon])
def self.bbox(minlat, minlon, maxlat, maxlon, tag = nil)
if tag.nil?
Map.where(['lat > ? AND lat < ? AND lon > ? AND lon < ?',
minlat, maxlat, minlon, maxlon])
else
Map.where(['lat > ? AND lat < ? AND lon > ? AND lon < ?',
minlat, maxlat, minlon, maxlon])
.joins(:tags).where("tags.name = ?", tag)
end
end

def exporting?
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ nice:
created_at: '2015-02-12 16:33:51'
map_id: 1
user_id: 1


featured:
name: featured
created_at: <%= Time.now %>
map_id: 2
user_id: 1
7 changes: 7 additions & 0 deletions test/functional/maps_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ def teardown
assert @maps.collect(&:name).include?('Saugus Landfill Incinerator')
end

test 'displays maps by region filter by tag if present' do
get :region, { minlat: 10, maxlat: 30, minlon: 60, maxlon: 80, tag: 'featured' }
@maps= assigns(:maps)
assert_response :success
assert @maps.collect(&:name).include?('Cubbon Park')
end

test 'should annotate maps' do
get :annotate, id: @map.slug
assert_response :success
Expand Down
10 changes: 10 additions & 0 deletions test/unit/map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ class MapTest < ActiveSupport::TestCase
assert_includes(Map.anonymous, map)
assert map.anonymous?
end

test 'filter bbox with tag if present' do
maps = Map.bbox(10,60,30,80,'featured')
assert maps.collect(&:name).include?('Cubbon Park')
end

test 'bbox without tag returns results' do
maps = Map.bbox(40,-80,50,-60)
assert maps.collect(&:name).include?('Saugus Landfill Incinerator')
end
end