Skip to content

Commit

Permalink
Merge pull request #249 from lnu-norge/new-area-filtering
Browse files Browse the repository at this point in the history
New area filtering
  • Loading branch information
DanielJackson-Oslo authored Oct 1, 2024
2 parents 9fb4bb2 + fd96e09 commit f54674f
Show file tree
Hide file tree
Showing 29 changed files with 763 additions and 95 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ Run `rails db:seed` to get sample data into your app.

You can also set the ENV variable ["SEED_FILE"](https://github.com/lnu-norge/lokaler.lnu.no/pull/66) to load a different seed file than the current environment dictates. Useful for deploying tests on Heroku, as Heroku always wants you to run in production mode - but you might want to seed with development data.

To get geographical data for Fylker and Kommuner, run: `rails geo:import_geographical_areas_from_geonorge`. This will hit the APIs of geo norge and add, and update, any fylker and kommuner.

## Sendgrid setup

You need to set the right sendgrid environment variables, and to get sendgrid to work you will also need to set the ENV variable ["HOST"] to equal to the domain you are using, example: `ENV["HOST"] = "app.herokuapp.com`
Expand Down
138 changes: 103 additions & 35 deletions app/controllers/concerns/filterable_spaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,98 +14,142 @@ def set_filterable_space_types
end

def set_filtered_facilities
facility_ids = sanitize_facility_list(params[:facilities])
facility_ids = sanitize_id_list(params[:facilities])
@filtered_facilities = Facility.includes(:facility_categories).find(facility_ids)
end

def set_filtered_geo_area_ids
unless params[:fylker].present? || params[:kommuner].present?
return @filtered_fylke_ids, @filtered_kommune_ids, @filtered_geo_area_ids = []
end

kommune_ids = sanitize_id_list(params[:kommuner])
fylke_ids = fylke_ids_excluding_those_with_a_selected_kommune(sanitize_id_list(params[:fylker]), kommune_ids)

@filtered_fylke_ids = fylke_ids
@filtered_kommune_ids = kommune_ids
@filtered_geo_area_ids = [*fylke_ids, *kommune_ids]
end

def fylke_ids_excluding_those_with_a_selected_kommune(unfiltered_fylke_ids, kommune_ids)
return unfiltered_fylke_ids if kommune_ids.blank?

fylker_for_selected_kommuner = Kommune.where(id: kommune_ids).pluck(:parent_id).uniq

unfiltered_fylke_ids.reject do |fylke_id|
fylker_for_selected_kommuner.include?(fylke_id)
end
end

def filter_spaces
set_filters_from_session_or_params

@filtered_spaces = Space.all
store_filters_in_session

set_filterable_facility_categories
set_filterable_space_types
set_filtered_facilities

filter_by_location
filter_by_title
filter_by_space_types
filter_and_order_by_facilities

store_filters_in_session
@filtered_spaces = Space.all
@filtered_spaces = filter_by_fylker_and_kommuner
@filtered_spaces = filter_by_map_bounds unless filter_by_map_bounds_turned_off?
@filtered_spaces = filter_by_title
@filtered_spaces = filter_by_space_types
@filtered_spaces = filter_and_order_by_facilities
end

def filter_spaces_for_vector_tiles
set_filters_from_session_or_params

@filtered_spaces = Space.all

filter_by_title
filter_by_space_types
filter_and_order_by_facilities
@filtered_spaces = filter_by_fylker_and_kommuner
@filtered_spaces = filter_by_title
@filtered_spaces = filter_by_space_types
@filtered_spaces = filter_and_order_by_facilities
end

def filter_by_title
return if params[:search_for_title].blank?
return @filtered_spaces if params[:search_for_title].blank?

@search_by_title = params[:search_for_title]
@filtered_spaces = @filtered_spaces.filter_on_title(@search_by_title)
@filtered_spaces.filter_on_title(@search_by_title)
end

def filter_by_space_types
return if params[:space_types].blank?
return @filtered_spaces if params[:space_types].blank?

space_types = params[:space_types]&.map(&:to_i)
@filtered_space_types = SpaceType.find(space_types)
@filtered_spaces = @filtered_spaces.filter_on_space_types(space_types)
@filtered_spaces.filter_on_space_types(space_types)
end

def location_params_present?
def fylke_or_kommune_params_present?
params[:fylker].present? || params[:kommuner].present?
end

def map_bounding_box_params_present?
params[:north_west_lat].present? && params[:north_west_lng].present? &&
params[:south_east_lat].present? && params[:south_east_lng].present?
end

def filter_by_location
return unless location_params_present?
def filter_by_fylker_and_kommuner
return @filtered_spaces unless fylke_or_kommune_params_present?

set_filtered_geo_area_ids

@filtered_spaces
.filter_on_fylker_or_kommuner(
fylke_ids: @filtered_fylke_ids,
kommune_ids: @filtered_kommune_ids
)
end

def filter_by_map_bounds
return @filtered_spaces unless map_bounding_box_params_present?

@filtered_spaces = @filtered_spaces.filter_on_location(
@filtered_spaces.filter_on_map_bounds(
params[:north_west_lat].to_f,
params[:north_west_lng].to_f,
params[:south_east_lat].to_f,
params[:south_east_lng].to_f
)
end

def sanitize_facility_list(array_of_facility_ids)
return [] if array_of_facility_ids.blank?
def sanitize_id_list(array_of_ids)
return [] if array_of_ids.blank?

array_of_facility_ids.uniq.select { |id| id.to_i.to_s == id }.map(&:to_i)
array_of_ids.uniq.select { |id| id.to_i.to_s == id }.map(&:to_i)
end

def filter_and_order_by_facilities
sanitized_facility_list = sanitize_facility_list(params[:facilities])
return @filtered_spaces = @filtered_spaces.order_by_star_rating if sanitized_facility_list.blank?
sanitized_facility_list = sanitize_id_list(params[:facilities])
return @filtered_spaces.order_by_star_rating if sanitized_facility_list.blank?

@filtered_spaces = @filtered_spaces.filter_and_order_by_facilities(sanitized_facility_list)
@filtered_spaces.filter_and_order_by_facilities(sanitized_facility_list)
end

def filter_by_map_bounds_turned_off?
params[:filter_by_map_bounds] == "off"
end

def any_filters_set?
filter_keys.detect { |key| params[key] }
all_filter_keys.detect { |key| params[key] }
end

def any_filters_stored_in_session?
session[:last_filter_params].present? &&
filter_keys.any? { |key| session[:last_filter_params][key].present? }
all_filter_keys.any? { |key| session[:last_filter_params][key].present? }
end

def store_filters_in_session
session[:last_filter_params] = params_from_search
session[:last_filter_params] = {}
all_filter_keys.each do |key|
session[:last_filter_params][key] = params[key]
end
end

def filter_keys
def singular_filters
%w[
facilities
space_types
filter_by_map_bounds
search_for_title
north_west_lat
north_west_lng
Expand All @@ -114,6 +158,29 @@ def filter_keys
]
end

def filter_arrays
{
facilities: [],
space_types: [],
fylker: [],
kommuner: []
}
end

def all_filters
[
*singular_filters,
**filter_arrays
]
end

def all_filter_keys
[
*singular_filters,
*filter_arrays.keys.map(&:to_s)
]
end

def set_filters_from_session_or_params
return params_from_search if any_filters_set?
return params_from_session if any_filters_stored_in_session?
Expand All @@ -122,9 +189,10 @@ def set_filters_from_session_or_params
end

def params_from_session
filter_keys.each do |key|
all_filter_keys.each do |key|
params[key] = session[:last_filter_params][key]
end

set_permitted_params
end

Expand All @@ -134,7 +202,7 @@ def params_from_search

def set_permitted_params
remove_duplicate_params
params.permit(*filter_keys, facilities: [], space_types: [])
params.permit(all_filters)
end

def remove_duplicate_params
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/spaces/map_selected_geo_area_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Spaces
class MapSelectedGeoAreaController < ApplicationController
include FilterableSpaces
def show
set_filters_from_session_or_params
set_filtered_geo_area_ids

geo_areas = GeographicalArea.where(id: @filtered_geo_area_ids)

geojson_features = geo_areas.map do |geo_area|
{
type: "Feature",
geometry: RGeo::GeoJSON.encode(geo_area.geo_area),
properties: {
id: geo_area.id,
name: geo_area.name
}
}
end

geojson = {
type: "FeatureCollection",
features: geojson_features
}

render json: geojson
end
end
end
13 changes: 7 additions & 6 deletions app/controllers/spaces/map_vector_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class MapVectorController < ApplicationController

VECTOR_TILE_CACHE_KEY_PREFIX = "spaces_vector_tile/"

def mvt_data_to_use
cached_mvt_data_for_tile(cache_key_prefix: VECTOR_TILE_CACHE_KEY_PREFIX)
# This cache never expires. To clear it, do
# Rails.cache.delete_matched("venstre_score_vector/*")
end
# TODO: Re-enable caching once I understand how it works on Heroku
# def mvt_data_to_use
# cached_mvt_data_for_tile(cache_key_prefix: VECTOR_TILE_CACHE_KEY_PREFIX)
# # This cache never expires. To clear it, do
# # Rails.cache.delete_matched("spaces_vector_tile/*")
# end

def pre_filter_spaces_subquery
filter_spaces_for_vector_tiles
Expand All @@ -24,7 +25,7 @@ def pre_filter_spaces_subquery

def set_permitted_params
remove_duplicate_params
params.permit([*filter_keys, :z, :x, :y, { facilities: [], space_types: [] }])
params.permit([:z, :x, :y, *all_filters])
end

def vector_tile_query # rubocop:disable Metrics/MethodLength
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
include Devise::Controllers::Rememberable

def google_oauth2 # rubocop:disable Metrics/AbcSize
user = User.from_google(email: auth.info.email, first_name: auth.info.first_name, last_name: auth.info.last_name)

if user.present?
remember_me(user)
sign_out_all_scopes
sign_in_and_redirect user
else
Expand Down
Loading

0 comments on commit f54674f

Please sign in to comment.