Skip to content

Commit

Permalink
add MapsController
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanTG committed Jan 8, 2025
1 parent 2c23eb0 commit 9f5e6b5
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 461 deletions.
125 changes: 125 additions & 0 deletions app/controllers/maps_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
class MapsController < InheritedResources::Base
respond_to :xml, :json, :html, :js, :rss, only: %i[index show]
has_scope :by_location_name, :by_location_id, :by_machine_name, :by_machine_id, :by_machine_single_id, :by_at_least_n_machines, :by_type_id, :by_operator_id, :user_faved, :by_city_name, :by_state_name

def params
request.parameters
end

def map_location_data
@locations = []

if params[:address].blank? && params[:by_machine_id].blank? && params[:by_machine_single_id].blank? && params[:by_machine_name].blank? && params[:by_location_name].blank? && params[:user_faved].blank? && params[:by_city_name].blank? && params[:by_state_name].blank?
@locations = []
else
params.delete(:by_machine_name) unless params[:by_machine_id].blank? && params[:by_machine_single_id].blank?

@lat, @lon = ''
if Rails.env.test?
# hardcode a PDX lat/lon during tests
@lat = 45.590502800000
@lon = -122.754940100000
end
if params[:address].blank? || !params[:by_city_name].blank?
@locations = apply_scopes(Location).order('locations.name').includes(:location_machine_xrefs, :machines, :location_type)
if @locations.blank? && !params[:by_city_name].blank?
params.delete(:by_city_name)
params.delete(:by_state_name)
end
end
if @locations.blank?
geocode unless params[:address].blank? || Rails.env.test?
find_nearby
end
end

@location_data = LocationsController.locations_javascript_data(@locations)

render partial: 'locations/locations', layout: false
end

def operator_location_data
@locations = Location.where(operator_id: params[:by_operator_id]).includes(:location_type, :machines)

@location_data = LocationsController.locations_javascript_data(@locations)

render partial: 'locations/locations', layout: false
end

def geocode
results = Geocoder.search(params[:address], lookup: :here)
results = Geocoder.search(params[:address]) if results.blank?
results = Geocoder.search(params[:address], lookup: :nominatim) if results.blank?
@lat, @lon = results.first.coordinates
end

def find_nearby
@near_distance = 15
while @locations.blank? && @near_distance < 600
@locations = apply_scopes(Location.near([@lat, @lon], @near_distance)).order('locations.name').includes(:location_machine_xrefs, :machines, :location_type)
@near_distance += 100
end
end

def region
@locations = Location.where('region_id = ?', @region.id).includes(:location_type, :operator)
@location_count = @locations.count
@lmx_count = @region.machines_count

if !params[:by_location_id].blank? && (loc = Location.where(id: params[:by_location_id]).first)
@title_params[:title] = "#{loc.name} - #{@region.full_name} Pinball Map"
machine_length = ' - ' + loc.machines.length.to_s + ' ' + 'machine'.pluralize(loc.machines.length) unless loc.machines.empty?
machine_list = ' - ' + loc.machine_names_first_no_year.join(', ') unless loc.machine_names_first_no_year.empty?
@title_params[:title_meta] = "#{loc.name} on Pinball Map! " + loc.full_street_address + machine_length.to_s + machine_list.to_s
end

cities = {}
location_types = {}
operators = {}

@locations.each do |l|
location_types[l.location_type_id] = l if l.location_type_id

cities[l.city] = l

operators[l.operator_id] = l if l.operator_id
end

@search_options = {
'type' => {
'id' => 'id',
'name' => 'name',
'search_collection' => location_types.values.map(&:location_type).sort_by(&:name)
},
'location' => {
'id' => 'id',
'name' => 'name_and_city',
'search_collection' => @locations.sort_by(&:massaged_name),
'autocomplete' => 1
},
'machine' => {
'id' => 'id',
'name' => 'name_and_year',
'search_collection' => @region.machines.sort_by(&:massaged_name),
'autocomplete' => 1
},
'zone' => {
'id' => 'id',
'name' => 'name',
'search_collection' => Zone.where('region_id = ?', @region.id).order('name')
},
'operator' => {
'id' => 'id',
'name' => 'name',
'search_collection' => operators.values.map(&:operator).sort_by(&:name)
},
'city' => {
'id' => 'city',
'name' => 'city',
'search_collection' => cities.values.sort_by(&:city)
}
}

render "#{@region.name}/region" if lookup_context.find_all("#{@region.name}/region").any?
end
end
122 changes: 0 additions & 122 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,5 @@
class PagesController < ApplicationController
respond_to :xml, :json, :html, :js, :rss
has_scope :by_location_name, :by_location_id, :by_machine_name, :by_machine_id, :by_machine_single_id, :by_at_least_n_machines, :by_type_id, :by_operator_id, :user_faved, :by_city_name, :by_state_name

def params
request.parameters
end

def map_location_data
@locations = []

if params[:address].blank? && params[:by_machine_id].blank? && params[:by_machine_single_id].blank? && params[:by_machine_name].blank? && params[:by_location_name].blank? && params[:user_faved].blank? && params[:by_city_name].blank? && params[:by_state_name].blank?
@locations = []
else
params.delete(:by_machine_name) unless params[:by_machine_id].blank? && params[:by_machine_single_id].blank?

@lat, @lon = ''
if Rails.env.test?
# hardcode a PDX lat/lon during tests
@lat = 45.590502800000
@lon = -122.754940100000
end
if params[:address].blank? || !params[:by_city_name].blank?
@locations = apply_scopes(Location).order('locations.name').includes(:location_machine_xrefs, :machines, :location_type)
if @locations.blank? && !params[:by_city_name].blank?
params.delete(:by_city_name)
params.delete(:by_state_name)
end
end
if @locations.blank?
geocode unless params[:address].blank? || Rails.env.test?
find_nearby
end
end

@location_data = LocationsController.locations_javascript_data(@locations)

render partial: 'locations/locations', layout: false
end

def operator_location_data
@locations = Location.where(operator_id: params[:by_operator_id]).includes(:location_type, :machines)

@location_data = LocationsController.locations_javascript_data(@locations)

render partial: 'locations/locations', layout: false
end

def geocode
results = Geocoder.search(params[:address], lookup: :here)
results = Geocoder.search(params[:address]) if results.blank?
results = Geocoder.search(params[:address], lookup: :nominatim) if results.blank?
@lat, @lon = results.first.coordinates
end

def find_nearby
@near_distance = 15
while @locations.blank? && @near_distance < 600
@locations = apply_scopes(Location.near([@lat, @lon], @near_distance)).order('locations.name').includes(:location_machine_xrefs, :machines, :location_type)
@near_distance += 100
end
end

def map
user = current_user.nil? ? nil : current_user
Expand All @@ -83,68 +23,6 @@ def map
@big_cities_placeholder = @big_cities_sample.nil? ? 'e.g. Portland, OR' : 'e.g. ' + @big_cities_sample.city + ', ' + @big_cities_sample.state
end

def region
@locations = Location.where('region_id = ?', @region.id).includes(:location_type, :operator)
@location_count = @locations.count
@lmx_count = @region.machines_count

if !params[:by_location_id].blank? && (loc = Location.where(id: params[:by_location_id]).first)
@title_params[:title] = "#{loc.name} - #{@region.full_name} Pinball Map"
machine_length = ' - ' + loc.machines.length.to_s + ' ' + 'machine'.pluralize(loc.machines.length) unless loc.machines.empty?
machine_list = ' - ' + loc.machine_names_first_no_year.join(', ') unless loc.machine_names_first_no_year.empty?
@title_params[:title_meta] = "#{loc.name} on Pinball Map! " + loc.full_street_address + machine_length.to_s + machine_list.to_s
end

cities = {}
location_types = {}
operators = {}

@locations.each do |l|
location_types[l.location_type_id] = l if l.location_type_id

cities[l.city] = l

operators[l.operator_id] = l if l.operator_id
end

@search_options = {
'type' => {
'id' => 'id',
'name' => 'name',
'search_collection' => location_types.values.map(&:location_type).sort_by(&:name)
},
'location' => {
'id' => 'id',
'name' => 'name_and_city',
'search_collection' => @locations.sort_by(&:massaged_name),
'autocomplete' => 1
},
'machine' => {
'id' => 'id',
'name' => 'name_and_year',
'search_collection' => @region.machines.sort_by(&:massaged_name),
'autocomplete' => 1
},
'zone' => {
'id' => 'id',
'name' => 'name',
'search_collection' => Zone.where('region_id = ?', @region.id).order('name')
},
'operator' => {
'id' => 'id',
'name' => 'name',
'search_collection' => operators.values.map(&:operator).sort_by(&:name)
},
'city' => {
'id' => 'city',
'name' => 'city',
'search_collection' => cities.values.sort_by(&:city)
}
}

render "#{@region.name}/region" if lookup_context.find_all("#{@region.name}/region").any?
end

def contact_sent
user = current_user.nil? ? nil : current_user
return if params['contact_msg'].blank? || (!user && params['contact_email'].blank?) || params['contact_msg'].match?(/vape/) || params['contact_msg'].match?(/seo/) || params['contact_msg'].match?(/Ezoic/)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 9 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
get '/faq' => 'pages#faq'
get '/store' => 'pages#store'
get '/donate' => 'pages#donate'
get '/map' => 'pages#map'
get '/operators' => 'pages#operators'
get '.well-known/apple-app-site-association' => 'pages#apple_app_site_association'
get '/apple-app-site-association' => 'pages#apple_app_site_association'
get '/robots.txt' => 'pages#robots'
Expand All @@ -119,7 +121,7 @@
get ':region' + '.rss' => 'location_machine_xrefs#index', format: 'xml'
get ':region' + '_scores.rss' => 'machine_score_xrefs#index', format: 'xml'

get '/' => 'pages#region', as: 'region_homepage'
get '/' => 'maps#region', as: 'region_homepage'
get '/about' => 'pages#about'
get '/contact' => 'pages#contact'
post '/contact_sent' => 'pages#contact_sent'
Expand Down Expand Up @@ -185,9 +187,9 @@
end
resources :machine_conditions
resources :suggested_locations, only: [] do
member do
post :convert_to_location
end
member do
post :convert_to_location
end
end

resources :users, only: [:profile, :toggle_fave_location, :update_user_flag, :render_user_flag] do
Expand All @@ -202,10 +204,10 @@
get 'inspire_profile' => 'pages#inspire_profile'
get 'pages/home'
get 'map' => 'pages#map'
get 'operators' => 'pages#operators'
get 'operator_location_data' => 'pages#operator_location_data'
get 'operators' => 'maps#operators'
get 'operator_location_data' => 'maps#operator_location_data'
get 'saved' => 'pages#map', user_faved: true
get 'map_location_data' => 'pages#map_location_data'
get 'map_location_data' => 'maps#map_location_data'
get 'suggest' => 'pages#suggest_new_location', as: 'map_location_suggest'
post 'submitted_new_location' => 'pages#submitted_new_location', as: 'map_submitted_new_location'
get 'flier' => 'pages#flier', as: 'map_flier'
Expand Down
Loading

0 comments on commit 9f5e6b5

Please sign in to comment.