Skip to content

Commit

Permalink
Fix bugs leading to broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJackson-Oslo committed Sep 25, 2024
1 parent 27500a4 commit 149a8be
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 38 deletions.
42 changes: 41 additions & 1 deletion app/controllers/concerns/filterable_spaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def set_filtered_facilities
end

def filter_spaces
# set_filters_from_session_or_params

@spaces = spaces_from_facilities

set_filterable_facility_categories
Expand All @@ -28,6 +30,8 @@ def filter_spaces
filter_by_location
filter_by_title
filter_by_space_types

# store_filters_in_session
end

def filter_by_title
Expand All @@ -41,7 +45,7 @@ def filter_by_space_types
return if params[:space_types].blank?

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

Expand Down Expand Up @@ -73,4 +77,40 @@ def spaces_from_facilities

Space.filter_and_order_by_facilities(sanitized_facility_list)
end

def any_filters_set?
filter_keys.any? { |key| params[key].present? }
end

def store_filters_in_session
session[:last_filter_params] = params_from_search
end

def filter_keys
%i[
facilities
space_types
search_for_title
north_west_lat
north_west_lng
south_east_lat
south_east_lng
]
end

def set_filters_from_session_or_params
return params_from_search if any_filters_set?

params_from_session
end

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

def params_from_search
params.permit(*filter_keys)
end
end
19 changes: 4 additions & 15 deletions app/controllers/spaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ class SpacesController < BaseControllers::AuthenticateController # rubocop:disab
before_action :access_active_personal_list, only: %i[index show]

def index
params_for_search

set_filterable_facility_categories
set_filterable_space_types
filter_spaces

@space_count = @spaces.to_a.size
@page_size = SPACE_SEARCH_PAGE_SIZE
@spaces = @spaces.limit(@page_size).includes_data_for_filter_list
# TODO: Make sure we can use .includes_data_for_filter_list here. That will mean we need
# to rewrite the filter_and_order_by_facilities method. Probably that will happen if we
# switch to a new search library.
@spaces = @spaces.limit(@page_size)
@markers = @spaces.map(&:render_map_marker)
end

Expand Down Expand Up @@ -183,18 +184,6 @@ def get_address_params(params)
) || {}
end

def params_for_search
params.permit(
:search_for_title,
:facilities,
:space_types,
:north_west_lat,
:north_west_lng,
:south_east_lat,
:south_east_lng
)
end

def space_params # rubocop:disable Metrics/MethodLength
params.require(:space).permit(
:title,
Expand Down
2 changes: 2 additions & 0 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class Space < ApplicationRecord # rubocop:disable Metrics/ClassLength

scope :filter_and_order_by_facilities, lambda { |facility_ids|
# NB: This grouping means that the results are not countable with .size or .count
# And cannot be used with includes...
# We really need to fix this in the current rewrite of search
group(:id)
.joins(:space_facilities)
.where(space_facilities: { relevant: true, facility_id: facility_ids })
Expand Down
2 changes: 1 addition & 1 deletion app/views/spaces/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
id="space-listing"
data-controller="set-selected-space"
class="px-2 md:py-8 md:view-as-table:p-0 view-as-table:mt-16 md:view-as-table:mt-0 loading-spaces-when-empty">
<%= render 'spaces/index/space_listings', view_as: "map", spaces: @spaces, space_count: @space_count, page_size: @page_size %>
<%= render 'spaces/index/space_listings', spaces: @spaces, space_count: @space_count, page_size: @page_size %>
<div
class="hidden"
data-mapbox-target="markerContainer"
Expand Down
12 changes: 9 additions & 3 deletions app/views/spaces/index/_filter_capsules.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<span class="flex overflow-x-auto gap-1">
<% if @search_by_title.present? %>
<%= render 'spaces/index/filter_capsule', title: @search_by_title, id_to_clear: 'search_for_title' %>
<%= render 'spaces/index/filter_capsule',
title: @search_by_title,
id_to_clear: 'search_for_title' %>
<% end %>
<% @filtered_facilities&.each do |facility| %>
<%= render 'spaces/index/filter_capsule', title: facility.title, id_to_clear: "facilities_#{facility.id}" %>
<%= render 'spaces/index/filter_capsule',
title: facility.title,
id_to_clear: "facilities_#{facility.id}" %>
<% end %>
<% @filtered_space_types&.each do |space_type| %>
<%= render 'spaces/index/filter_capsule', title: space_type.title, id_to_clear: "space_types_#{space_type.id}" %>
<%= render 'spaces/index/filter_capsule',
title: space_type.type_name,
id_to_clear: "space_types_#{space_type.id}" %>
<% end %>
</span>
2 changes: 1 addition & 1 deletion app/views/spaces/index/_space_listings.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%# locals: (view_as:, spaces:, space_count:, page_size: %>
<%# locals: (spaces:, space_count:, page_size:) %>
<% if spaces.any? %>
<%= render 'spaces/index/space_listing_header', space_count: %>
<ul id="space-list" class="flex flex-col gap-8">
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/spaces_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Spaces", type: :request do
let(:user) { Fabricate(:user) }

before do
sign_in user
end

describe "GET /" do
it "loads the index page" do
get spaces_path
expect(response).to have_http_status(:success)
end
end
end
17 changes: 0 additions & 17 deletions spec/views/spaces/index.html.erb_spec.rb

This file was deleted.

0 comments on commit 149a8be

Please sign in to comment.