Support filter by collection in different screens

We want to filter in the same screen for cards/events but not when viewing a card or from other screens. This adds a controller action to flag the actions where this is supported
This commit is contained in:
Jorge Manrubia
2025-08-21 14:18:35 +02:00
parent 109a95ac6f
commit 5896a21499
7 changed files with 43 additions and 35 deletions
+2
View File
@@ -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
@@ -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])
+14
View File
@@ -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
+2
View File
@@ -1,6 +1,8 @@
class EventsController < ApplicationController
include DayTimelinesScoped
enable_collection_filtering only: :index
def index
end
end
+19
View File
@@ -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
+1 -1
View File
@@ -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 %>
@@ -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 %>
<span class="overflow-ellipsis"><%= Current.user.collections.first.name %></span>
<span class="txt-ink translucent flex-item-no-shrink flex-item-justify-end"><span class="txt-x-small txt-uppercase">GO TO</span> </span>
@@ -7,7 +9,7 @@
<% end %>
<% elsif Current.user.collections.many? %>
<div class="popup__group" data-filter-target="item" data-navigable-list-target="item">
<%= link_to events_path(clear_filter: true), class: "btn txt-xx-small flex-item-no-shrink" do %>
<%= link_to clear_url_path, class: "btn txt-xx-small flex-item-no-shrink" do %>
<%= check_box_tag "filter_collections", nil, user_filtering.collections.blank?, class: "form-checkbox" %>
<span class="for-screen-reader">
See everything in all collections
@@ -15,7 +17,7 @@
<%= icon_tag "check", size: 18, class: "checked" %>
<% end %>
<%= link_to cards_path(user_filtering.as_params.except(:collection_ids)), class: "popup__item btn" do %>
<%= link_to clear_url_path, class: "popup__item btn" do %>
<span class="overflow-ellipsis">All collections</span>
<span class="txt-ink translucent flex-item-no-shrink flex-item-justify-end"><span class="txt-x-small txt-uppercase">GO TO</span> </span>
<% end %>