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
+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