Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ def index
.paginate(page: params[:page])
elsif params[:collection_id]
@collection = Collection.find_by!(name: params[:collection_id])
@search = CollectionSearchForm.new({ parent_id: @collection.id, sort_column: "title.keyword" }.merge(page: params[:page]))
@search = CollectionSearchForm.new({ parent_id: @collection.id, sort_column: "title.keyword" }.merge(page: params[:page]), logged_in_as_admin?)
@collections = @search.search_results.scope(:for_search)
flash_search_warnings(@collections)
@page_subtitle = t(".subcollections_page_title", collection_title: @collection.title)
elsif params[:user_id]
@user = User.find_by!(login: params[:user_id])
@search = CollectionSearchForm.new({ maintainer_id: @user.id, sort_column: "title.keyword" }.merge(page: params[:page]))
@search = CollectionSearchForm.new({ maintainer_id: @user.id, sort_column: "title.keyword" }.merge(page: params[:page]), logged_in_as_admin?)
@collections = @search.search_results.scope(:for_search)
flash_search_warnings(@collections)
@page_subtitle = ts("%{username} - Collections", username: @user.login)
else
@sort_and_filter = true
@search = CollectionSearchForm.new(collection_filter_params.merge(page: params[:page]))
@search = CollectionSearchForm.new(collection_filter_params.merge(page: params[:page]), logged_in_as_admin?)
@collections = @search.search_results.scope(:for_search)
flash_search_warnings(@collections)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/search/collection_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def sort_column
end

def sort
options[:sort_column] = sort_column.sub("public", "general") if (@user.present? || options[:admin_logged_in]) && sort_column.include?("public")
direction = options[:sort_direction].presence
direction ||= if sort_column.include?("title") || sort_column.include?("signups_close_at")
"asc"
Expand Down
7 changes: 5 additions & 2 deletions app/models/search/collection_search_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class CollectionSearchForm
define_method(filterable) { options[filterable] }
end

def initialize(opts = {})
def initialize(opts = {}, admin_logged_in = false) # rubocop:disable Style/OptionalBooleanParameter
@options = opts
@options[:admin_logged_in] = admin_logged_in
process_options
@searcher = CollectionQuery.new(@options)
end
Expand Down Expand Up @@ -64,7 +65,9 @@ def sort_direction
def sort_options
[
["Date Created", "created_at"],
["Title", "title.keyword"]
%w[Title title.keyword],
["Bookmarked Items", "public_bookmarked_items_count"],
%w[Works public_works_count]
].freeze
end

Expand Down
46 changes: 46 additions & 0 deletions features/collections/collection_browse.feature
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,52 @@ Feature: Collection
And I should see "Surprise Presents"
But I should not see "Another Gift Swap"

Scenario: Sort collections by Works and Bookmarks

Given I have a collection "Privates"
And I have a collection "Publics"
When I am logged in as the owner of "Privates"
And I post the work "Private 1" in the collection "Privates"
And I lock the work "Private 1"
And I post the work "Private 2" in the collection "Privates"
And I lock the work "Private 2"
And I bookmark the work "Private 1" to the collection "Publics"
And I bookmark the work "Private 2" to the collection "Publics"
And I post the work "Public 1" in the collection "Publics"
And I bookmark the work "Public 1" to the collection "Privates"
And all indexing jobs have been run
And I go to the collections page
And I select "Works" from "collection_search_sort_column"
And I press "Sort and Filter"
Then I should see the text with tags '<a href="/collections/Privates/works">2</a>'
And I should see the text with tags '<a href="/collections/Publics/works">1</a>'
When I log out
Then I should see the text with tags '<a href="/collections/Privates/works">0</a>'
And I should see the text with tags '<a href="/collections/Publics/works">1</a>'
When I am logged in as a super admin
And I go to the collections page
And I select "Works" from "collection_search_sort_column"
And I press "Sort and Filter"
Then I should see the text with tags '<a href="/collections/Privates/works">2</a>'
And I should see the text with tags '<a href="/collections/Publics/works">1</a>'
When I go to the collections page
And I select "Bookmarked Items" from "collection_search_sort_column"
And I press "Sort and Filter"
Then I should see the text with tags '<a href="/collections/Privates/bookmarks">1</a>'
And I should see the text with tags '<a href="/collections/Publics/bookmarks">2</a>'
When I log out
And I go to the collections page
And I select "Bookmarked Items" from "collection_search_sort_column"
And I press "Sort and Filter"
Then I should see the text with tags '<a href="/collections/Privates/bookmarks">1</a>'
And I should not see the text with tags '<a href="/collections/Publics/bookmarks">2</a>'
When I am logged in as a super admin
And I go to the collections page
And I select "Bookmarked Items" from "collection_search_sort_column"
And I press "Sort and Filter"
Then I should see the text with tags '<a href="/collections/Privates/bookmarks">1</a>'
And I should see the text with tags '<a href="/collections/Publics/bookmarks">2</a>'

Scenario: Look at a collection, see the rules and intro and FAQ

Given a set of collections for searching
Expand Down
32 changes: 32 additions & 0 deletions spec/controllers/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@
expect(response).to have_http_status(:success)
expect(assigns(:collections).map(&:title)).to eq sorted_collection_titles
end

it "sorts collections by Works, DESC by default" do
sorted_collection_titles = Collection.all.sort_by(&:public_works_count).reverse.map(&:title)

get :index, params: { collection_search: { sort_column: "public_works_count" } }
expect(response).to have_http_status(:success)
expect(assigns(:collections).map(&:title)).to eq sorted_collection_titles
end

it "sorts collections by Works" do
sorted_collection_titles = Collection.all.sort_by(&:public_works_count).map(&:title)

get :index, params: { collection_search: { sort_column: "public_works_count", sort_direction: "ASC" } }
expect(response).to have_http_status(:success)
expect(assigns(:collections).map(&:title)).to eq sorted_collection_titles
end

it "sorts collections by Bookmarks, DESC by default" do
sorted_collection_titles = Collection.all.sort_by(&:public_bookmarked_items_count).reverse.map(&:title)

get :index, params: { collection_search: { sort_column: "public_bookmarked_items_count" } }
expect(response).to have_http_status(:success)
expect(assigns(:collections).map(&:title)).to eq sorted_collection_titles
end

it "sorts collections by Bookmarks" do
sorted_collection_titles = Collection.all.sort_by(&:public_bookmarked_items_count).map(&:title)

get :index, params: { collection_search: { sort_column: "public_bookmarked_items_count", sort_direction: "ASC" } }
expect(response).to have_http_status(:success)
expect(assigns(:collections).map(&:title)).to eq sorted_collection_titles
end
end

context "collections index for user collections" do
Expand Down
20 changes: 20 additions & 0 deletions spec/models/search/collection_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
expect(q.generated_query[:sort]).to eq([{ "title.keyword" => { order: "desc" } }, { "id" => { order: "desc" } }])
end

it "sorts by bookmarked items" do
q = CollectionQuery.new(sort_column: "public_bookmarked_items_count", sort_direction: "desc")
expect(q.generated_query[:sort]).to eq([{ "public_bookmarked_items_count" => { order: "desc" } }, { "id" => { order: "desc" } }])
end

it "sorts by works" do
q = CollectionQuery.new(sort_column: "public_works_count", sort_direction: "desc")
expect(q.generated_query[:sort]).to eq([{ "public_works_count" => { order: "desc" } }, { "id" => { order: "desc" } }])
end

it "sorts by general bookmarked items when logged in" do
q = CollectionQuery.new(sort_column: "public_bookmarked_items_count", sort_direction: "desc", admin_logged_in: true)
expect(q.generated_query[:sort]).to eq([{ "general_bookmarked_items_count" => { order: "desc" } }, { "id" => { order: "desc" } }])
end

it "sorts by general works when logged in" do
q = CollectionQuery.new(sort_column: "public_works_count", sort_direction: "desc", admin_logged_in: true)
expect(q.generated_query[:sort]).to eq([{ "general_works_count" => { order: "desc" } }, { "id" => { order: "desc" } }])
end

describe "filtering", collection_search: true do
let!(:gift_exchange) { create(:gift_exchange, signup_open: true, signups_open_at: Time.current - 2.days, signups_close_at: Time.current + 1.week) }
let!(:gift_exchange_collection) { create(:collection, challenge: gift_exchange, challenge_type: "GiftExchange") }
Expand Down
Loading