Merge pull request #674 from basecamp/search-context
Make search results screen a "cards context for Fizzy do
This commit is contained in:
@@ -11,23 +11,21 @@ class Command::Parser::Context
|
||||
def cards
|
||||
if viewing_card_contents?
|
||||
user.accessible_cards.where id: params[:id]
|
||||
elsif viewing_list_of_cards?
|
||||
elsif viewing_cards_index?
|
||||
filter.cards.published
|
||||
elsif viewing_search_results?
|
||||
user.accessible_cards.where(id: user.search(params[:q]).select(:card_id))
|
||||
else
|
||||
Card.none
|
||||
end
|
||||
end
|
||||
|
||||
def filter
|
||||
user.filters.from_params(params.permit(*Filter::Params::PERMITTED_PARAMS).reverse_merge(**FilterScoped::DEFAULT_PARAMS))
|
||||
end
|
||||
|
||||
def viewing_card_contents?
|
||||
controller == "cards" && action == "show"
|
||||
viewing_card_perma?
|
||||
end
|
||||
|
||||
def viewing_list_of_cards?
|
||||
controller == "cards" && action == "index"
|
||||
viewing_cards_index? || viewing_search_results?
|
||||
end
|
||||
|
||||
private
|
||||
@@ -36,6 +34,22 @@ class Command::Parser::Context
|
||||
MAX_CARDS = 20
|
||||
MAX_CLOSED_CARDS = 10
|
||||
|
||||
def filter
|
||||
user.filters.from_params(params.permit(*Filter::Params::PERMITTED_PARAMS).reverse_merge(**FilterScoped::DEFAULT_PARAMS))
|
||||
end
|
||||
|
||||
def viewing_card_perma?
|
||||
controller == "cards" && action == "show"
|
||||
end
|
||||
|
||||
def viewing_cards_index?
|
||||
controller == "cards" && action == "index"
|
||||
end
|
||||
|
||||
def viewing_search_results?
|
||||
controller == "searches" && action == "show"
|
||||
end
|
||||
|
||||
def extract_url_components
|
||||
uri = URI.parse(url || "")
|
||||
route = Rails.application.routes.recognize_path(uri.path)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
require "test_helper"
|
||||
|
||||
class Command::Parser::ContextTest < ActiveSupport::TestCase
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
setup do
|
||||
@user = users(:david)
|
||||
|
||||
Card.find_each(&:reindex)
|
||||
Comment.find_each(&:reindex)
|
||||
end
|
||||
|
||||
test "viewing a single card" do
|
||||
context = Command::Parser::Context.new(@user, url: card_path(cards(:layout)))
|
||||
|
||||
assert context.viewing_card_contents?
|
||||
assert_not context.viewing_list_of_cards?
|
||||
|
||||
assert_equal 1, context.cards.count
|
||||
assert_equal cards(:layout).id, context.cards.first.id
|
||||
end
|
||||
|
||||
test "viewing cards index" do
|
||||
context = Command::Parser::Context.new(@user, url: cards_path)
|
||||
|
||||
assert context.viewing_list_of_cards?
|
||||
assert_not context.viewing_card_contents?
|
||||
|
||||
assert_equal @user.filters.from_params(FilterScoped::DEFAULT_PARAMS).cards.published.count, context.cards.count
|
||||
end
|
||||
|
||||
test "viewing search results" do
|
||||
context = Command::Parser::Context.new(@user, url: search_path(q: "layout"))
|
||||
|
||||
assert context.viewing_list_of_cards?
|
||||
assert_not context.viewing_card_contents?
|
||||
|
||||
expected_cards = @user.accessible_cards.where(id: @user.search("layout").select(:card_id))
|
||||
|
||||
assert_equal expected_cards.count, context.cards.count
|
||||
assert_equal expected_cards.pluck(:id).sort, context.cards.pluck(:id).sort
|
||||
end
|
||||
|
||||
test "unrecognized URL pattern" do
|
||||
context = Command::Parser::Context.new(@user, url: filters_path)
|
||||
|
||||
assert_not context.viewing_card_contents?
|
||||
assert_not context.viewing_list_of_cards?
|
||||
|
||||
assert_empty context.cards
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user