Fix: filtering combined with search

This commit is contained in:
Jorge Manrubia
2025-05-06 11:40:57 +02:00
parent d7164356f1
commit ca3a8a444f
4 changed files with 23 additions and 20 deletions
+2 -2
View File
@@ -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
+18 -15
View File
@@ -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
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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