Fix: filtering combined with search
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user