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
8 changes: 7 additions & 1 deletion app/controllers/autocomplete_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ def open_collection_names

# For creating collections, autocomplete the name of a parent collection owned by the user only
def collection_parent_name
render_output(current_user.maintained_collections.top_level.with_name_like(params[:term]).pluck(:name).sort)
results = current_user.maintained_collections.top_level
.with_name_or_title_like(params[:term])
.limit(10)
.order(:title)
.pluck(:name, :title)
.map { |name, title| { id: name, name: "#{title} (#{name})" } }
respond_with(results)
end

# for looking up existing urls for external works to avoid duplication
Expand Down
9 changes: 2 additions & 7 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,8 @@ def cleanup_url
end
end

scope :with_name_like, lambda { |name|
where("collections.name LIKE ?", "%#{name}%")
.limit(10)
}

scope :with_title_like, lambda { |title|
where("collections.title LIKE ?", "%#{title}%")
scope :with_name_or_title_like, lambda { |term|
where("collections.name LIKE ? OR collections.title LIKE ?", "%#{term}%", "%#{term}%")
}

scope :with_item_count, lambda {
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/autocomplete_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "spec_helper"

describe AutocompleteController do
include LoginMacros

describe "tag" do
let!(:tag1) { create(:canonical_fandom, name: "Match") }
let!(:tag2) { create(:canonical_fandom, name: "Blargh") }
Expand Down Expand Up @@ -41,4 +43,20 @@
end
end
end

describe "GET #collection_parent_name" do
let!(:user) { create(:user) }
let!(:collection1) { create(:collection, name: "some_name", title: "Unrelated Title", owner: user.default_pseud) }
let!(:collection2) { create(:collection, name: "unrelated_name", title: "Some Title", owner: user.default_pseud) }

before { fake_login_known_user(user) }

it "matches by name or title and formats suggestions as 'Title (name)'" do
get :collection_parent_name, params: { term: "some", format: :json }
expect(JSON.parse(response.body)).to contain_exactly(
{ "id" => "some_name", "name" => "Unrelated Title (some_name)" },
{ "id" => "unrelated_name", "name" => "Some Title (unrelated_name)" }
)
end
end
end
Loading