From ca3a8a444f76bba3556fa7a72d49c2b21e5eac09 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Tue, 6 May 2025 11:40:57 +0200 Subject: [PATCH] Fix: filtering combined with search --- app/models/command/parser.rb | 4 ++-- app/models/command/parser/context.rb | 33 +++++++++++++++------------- app/models/command/search.rb | 4 ++-- app/models/filter/params.rb | 2 +- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/models/command/parser.rb b/app/models/command/parser.rb index 3d4954ac5..df5af978a 100644 --- a/app/models/command/parser.rb +++ b/app/models/command/parser.rb @@ -1,7 +1,7 @@ class Command::Parser attr_reader :context - delegate :user, :cards, to: :context + delegate :user, :cards, :filter, to: :context def initialize(context) @context = context @@ -38,7 +38,7 @@ class Command::Parser if card = user.accessible_cards.find_by_id(string) Command::GoToCard.new(card_id: card.id) else - Command::Search.new(query: string) + Command::Search.new(query: string, params: filter.as_params) end end end diff --git a/app/models/command/parser/context.rb b/app/models/command/parser/context.rb index 12b92293c..49a8a651a 100644 --- a/app/models/command/parser/context.rb +++ b/app/models/command/parser/context.rb @@ -1,28 +1,31 @@ class Command::Parser::Context - attr_reader :user, :url + attr_reader :user def initialize(user, url:) @user = user - @url = url + extract_url_components(url) end def cards - route = Rails.application.routes.recognize_path(url) + if controller == "cards" && action == "show" + user.accessible_cards.where id: params[:id] + elsif controller == "cards" && action == "index" + filter.cards + end + end - controller = route[:controller] - action = route[:action] - params = route.except(:controller, :action) - - cards_from(controller, action, params) + def filter + user.filters.from_params params.reverse_merge(**FilterScoped::DEFAULT_PARAMS) end private - def cards_from(controller, action, params) - if controller == "cards" && action == "show" - user.accessible_cards.where id: params[:id] - elsif controller == "cards" && action == "index" - filter = user.filters.from_params params.reverse_merge(**FilterScoped::DEFAULT_PARAMS) - filter.cards - end + def extract_url_components(url) + uri = URI.parse(url) + route = Rails.application.routes.recognize_path(uri.path) + @controller = route[:controller] + @action = route[:action] + @params = Rack::Utils.parse_nested_query(uri.query) end + + attr_reader :controller, :action, :params end diff --git a/app/models/command/search.rb b/app/models/command/search.rb index 4de60b3e1..237bc2586 100644 --- a/app/models/command/search.rb +++ b/app/models/command/search.rb @@ -1,11 +1,11 @@ class Command::Search < Command - store_accessor :data, :query + store_accessor :data, :query, :params def title "Search '#{query}'" end def execute - redirect_to cards_path("terms[]": query.presence) + redirect_to cards_path(**params.merge("terms[]": query.presence)) end end diff --git a/app/models/filter/params.rb b/app/models/filter/params.rb index 7a4b85d60..0166b17e7 100644 --- a/app/models/filter/params.rb +++ b/app/models/filter/params.rb @@ -23,7 +23,7 @@ module Filter::Params end def normalize_params(params) - params.to_h.compact_blank.reject(&method(:default_value?)).sort + params.to_h.compact_blank.reject(&method(:default_value?)).sort_by { |k, _| k.to_s } end end