diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 0877f89f6cf..66546e40deb 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -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 diff --git a/app/models/search/collection_query.rb b/app/models/search/collection_query.rb index 61792360dad..deb4afeb92d 100644 --- a/app/models/search/collection_query.rb +++ b/app/models/search/collection_query.rb @@ -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" diff --git a/app/models/search/collection_search_form.rb b/app/models/search/collection_search_form.rb index 7dd45aadf29..d9e453a3676 100644 --- a/app/models/search/collection_search_form.rb +++ b/app/models/search/collection_search_form.rb @@ -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 @@ -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 diff --git a/features/collections/collection_browse.feature b/features/collections/collection_browse.feature index 223fdbe13e2..8e6aef1e31b 100644 --- a/features/collections/collection_browse.feature +++ b/features/collections/collection_browse.feature @@ -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 '2' + And I should see the text with tags '1' + When I log out + Then I should see the text with tags '0' + And I should see the text with tags '1' + 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 '2' + And I should see the text with tags '1' + 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 '1' + And I should see the text with tags '2' + 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 '1' + And I should not see the text with tags '2' + 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 '1' + And I should see the text with tags '2' + Scenario: Look at a collection, see the rules and intro and FAQ Given a set of collections for searching diff --git a/spec/controllers/collections_controller_spec.rb b/spec/controllers/collections_controller_spec.rb index fe38dc191eb..c2e9c3c0457 100644 --- a/spec/controllers/collections_controller_spec.rb +++ b/spec/controllers/collections_controller_spec.rb @@ -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 diff --git a/spec/models/search/collection_query_spec.rb b/spec/models/search/collection_query_spec.rb index 0220422822b..9cac8f81f20 100644 --- a/spec/models/search/collection_query_spec.rb +++ b/spec/models/search/collection_query_spec.rb @@ -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") }