diff --git a/app/controllers/cards_controller.rb b/app/controllers/cards_controller.rb index b6a2833a3..e5a8212d3 100644 --- a/app/controllers/cards_controller.rb +++ b/app/controllers/cards_controller.rb @@ -6,6 +6,8 @@ class CardsController < ApplicationController skip_before_action :set_collection, only: :index before_action :set_card, only: %i[ show edit update destroy ] + enable_collection_filtering only: :index + PAGE_SIZE = 50 def index diff --git a/app/controllers/concerns/day_timelines_scoped.rb b/app/controllers/concerns/day_timelines_scoped.rb index ecbfe1511..fc8530e3c 100644 --- a/app/controllers/concerns/day_timelines_scoped.rb +++ b/app/controllers/concerns/day_timelines_scoped.rb @@ -4,45 +4,14 @@ module DayTimelinesScoped included do include FilterScoped - before_action :normalize_collection_params - before_action :restore_collections_filter_from_cookie before_action :set_day_timeline - after_action :save_collections_filter_to_cookie end private - def normalize_collection_params - if params[:collection_ids].blank? && !params[:clear_filter] - params[:clear_filter] = true - end - end - - def restore_collections_filter_from_cookie - if params[:clear_filter] - delete_collections_filter_cookie - else - set_collections_filter_from_cookie - end - end - - def delete_collections_filter_cookie - cookies.delete(:collection_filter) - end - - def set_collections_filter_from_cookie - if cookies[:collection_filter].present? && @filter.collections.blank? - @filter.collection_ids = cookies[:collection_filter].split(",") - end - end - def set_day_timeline @day_timeline = Current.user.timeline_for(day, filter: @filter) end - def save_collections_filter_to_cookie - cookies[:collection_filter] = @filter.collection_ids.join(",") - end - def day if params[:day].present? Time.zone.parse(params[:day]) diff --git a/app/controllers/concerns/filter_scoped.rb b/app/controllers/concerns/filter_scoped.rb index 6724e3df6..8e342afe7 100644 --- a/app/controllers/concerns/filter_scoped.rb +++ b/app/controllers/concerns/filter_scoped.rb @@ -6,6 +6,12 @@ module FilterScoped before_action :set_user_filtering end + class_methods do + def enable_collection_filtering(**options) + before_action :enable_collection_filtering, **options + end + end + private DEFAULT_PARAMS = { indexed_by: "latest" } @@ -20,4 +26,12 @@ module FilterScoped def set_user_filtering @user_filtering = User::Filtering.new(Current.user, @filter, expanded: params[:expand_all]) end + + def enable_collection_filtering + # We pass a block so that we don't have to pass around the script_name and host + # to the model to make +url_for+ invocable + @user_filtering.enable_collection_filtering do |**options| + url_for(options) + end + end end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 1340def1d..7ce5a5373 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,6 +1,8 @@ class EventsController < ApplicationController include DayTimelinesScoped + enable_collection_filtering only: :index + def index end end diff --git a/app/models/user/filtering.rb b/app/models/user/filtering.rb index 705200b82..fdc5fa415 100644 --- a/app/models/user/filtering.rb +++ b/app/models/user/filtering.rb @@ -1,4 +1,6 @@ class User::Filtering + include Rails.application.routes.url_helpers + attr_reader :user, :filter, :expanded delegate :as_params, to: :filter @@ -66,4 +68,21 @@ class User::Filtering def show_time_window?(name) expanded? || filter.public_send("#{name}_window").present? end + + def enable_collection_filtering(&block) + @collection_filtering_route_resolver = block + end + + def self_filter_path(...) + if supports_collection_filtering? + @collection_filtering_route_resolver.call(...) + else + cards_path(...) + end + end + + private + def supports_collection_filtering? + @collection_filtering_route_resolver.present? + end end diff --git a/app/views/filters/menu/_collections.html.erb b/app/views/filters/menu/_collections.html.erb index 6996bca53..58dc6fff4 100644 --- a/app/views/filters/menu/_collections.html.erb +++ b/app/views/filters/menu/_collections.html.erb @@ -1,4 +1,4 @@ -<%= form_with url: url_for, method: :get, class: "display-contents", +<%= form_with url: user_filtering.self_filter_path, method: :get, class: "display-contents", data: { controller: "form" } do |form| %> <% if user_filtering.collections.any? %> <%= render "filters/menu/custom", user_filtering: user_filtering %> diff --git a/app/views/filters/menu/collections/_all_option.html.erb b/app/views/filters/menu/collections/_all_option.html.erb index 3980cd1ee..5896c0c3b 100644 --- a/app/views/filters/menu/collections/_all_option.html.erb +++ b/app/views/filters/menu/collections/_all_option.html.erb @@ -1,5 +1,7 @@ +<% clear_url_path = user_filtering.self_filter_path(user_filtering.as_params.except(:collection_ids)) %> + <% if Current.user.collections.one? %> - <%= link_to cards_path(user_filtering.as_params.except(:collection_ids)), class: "popup__group", data: { filter_target: "item", navigable_list_target: "item" } do %> + <%= link_to clear_url_path, class: "popup__group", data: { filter_target: "item", navigable_list_target: "item" } do %> <%= tag.div class: "popup__item btn" do %> <%= Current.user.collections.first.name %> GO TO @@ -7,7 +9,7 @@ <% end %> <% elsif Current.user.collections.many? %>