Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added document sets to search attempts for #3775 #3781

Merged
merged 2 commits into from
Sep 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion app/controllers/display_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def paged_search
session[:search_attempt_id] = @search_attempt.id
end
# restrict to pages that include that subject
@collection = @search_attempt.collection
@collection = @search_attempt.collection || @search_attempt.document_set || @search_attempt.work.collection
@work = @search_attempt&.work
pages = @search_attempt.results
@pages = pages.paginate(page: params[:page])
Expand Down
74 changes: 25 additions & 49 deletions app/controllers/search_attempt_controller.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,35 @@
class SearchAttemptController < ApplicationController
def create
user_id = current_user.nil? ? nil : current_user.id
owner = current_user.nil? ? false : current_user.owner
query = params[:search]
# Some of these objects may be nil, based on the search type
work = Work.find(params[:work_id]) if params[:work_id].present?
collection = Collection.find(params[:collection_id]) if params[:collection_id].present?
document_set = DocumentSet.find(params[:document_set_id]) if params[:document_set_id].present?

if params[:work_id].present?
work = Work.find(params[:work_id])
@search_attempt = SearchAttempt.new(
query: params[:search],
search_type: "work",
work_id: work.id,
collection_id: work.collection_id,
user_id: user_id,
owner: owner
)
@search_attempt.save
session[:search_attempt_id] = @search_attempt.id
ajax_redirect_to(paged_search_path(@search_attempt))

elsif params[:collection_id].present? && params[:search_by_title].present?
collection = Collection.find(params[:collection_id])
@search_attempt = SearchAttempt.new(
query: params[:search_by_title],
search_type: "collection-title",
collection_id: collection.id,
user_id: user_id,
owner: owner
)
@search_attempt.save
session[:search_attempt_id] = @search_attempt.id
ajax_redirect_to(collection_path(collection.owner, collection.slug, search_attempt_id: @search_attempt.id))

elsif params[:collection_id].present?
collection_id = Collection.find(params[:collection_id]).id
@search_attempt = SearchAttempt.new(
query: params[:search],
search_type: "collection",
collection_id: collection_id,
user_id: user_id,
owner: owner
)
@search_attempt.save
session[:search_attempt_id] = @search_attempt.id
ajax_redirect_to(paged_search_path(@search_attempt))

search_type = "work"
elsif (params[:collection_id].present? || params[:document_set_id].present?) && params[:search_by_title].present?
search_type = "collection-title"
query = params[:search_by_title]
elsif params[:collection_id].present? || params[:document_set_id].present?
search_type = "collection"
else # Find a Project search
@search_attempt = SearchAttempt.new(
query: params[:search],
search_type: "findaproject",
user_id: user_id,
owner: owner
)
@search_attempt.save
session[:search_attempt_id] = @search_attempt.id
ajax_redirect_to(search_attempt_show_path(@search_attempt))
search_type = "findaproject"
end

@search_attempt = SearchAttempt.new(
query: query,
search_type: search_type,
work_id: work&.id,
collection_id: collection&.id,
document_set_id: document_set&.id,
user_id: current_user&.id,
owner: owner
)
@search_attempt.save
session[:search_attempt_id] = @search_attempt.id
ajax_redirect_to(@search_attempt.results_link)
end

def show
Expand Down
27 changes: 24 additions & 3 deletions app/models/search_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class SearchAttempt < ApplicationRecord
belongs_to :user, optional: true
belongs_to :collection, optional: true
belongs_to :work, optional: true
belongs_to :document_set, optional: true
visitable class_name: "Visit" # ahoy integration

after_create :update_slug
Expand All @@ -14,6 +15,24 @@ def update_slug
update_attribute(:slug, to_param)
end

def results_link
paths = Rails.application.routes.url_helpers
case search_type
when "work"
paths.paged_search_path(self)
when "collection"
paths.paged_search_path(self)
when "collection-title"
if collection.present?
paths.collection_path(collection.owner, collection, search_attempt_id: id)
else # document_set
paths.collection_path(document_set.owner, document_set, search_attempt_id: id)
end
when "findaproject"
paths.search_attempt_show_path(self)
end
end

def results
query = sanitize_and_format_search_string(self.query)

Expand All @@ -30,18 +49,20 @@ def results
end

when "collection"
if collection.present? && query.present?
collection_or_document_set = collection || document_set
if collection_or_document_set.present? && query.present?
query = precise_search_string(query)
results = Page.order('work_id, position')
.joins(:work)
.where(work_id: collection.works.ids)
.where(work_id: collection_or_document_set.works.ids)
.where("MATCH(search_text) AGAINST(? IN BOOLEAN MODE)", query)
else
results = Page.none
end

when "collection-title"
results = collection.search_works(query).includes(:work_statistic)
collection_or_document_set = collection || document_set
results = collection_or_document_set.search_works(query).includes(:work_statistic)

when "findaproject"
results = Collection.search(query).unrestricted + DocumentSet.search(query).unrestricted
Expand Down
13 changes: 3 additions & 10 deletions app/views/admin/searches.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,13 @@ table.admin-grid.datagrid.striped
-@searches.each do |search|
tr
td.toleft
-if search.search_type == 'findaproject'
-link = search_attempt_show_path(search)
-elsif search.search_type == 'collection'
-link = paged_search_path(search)
-elsif search.search_type == 'collection-title'
-link = collection_path(search.collection.owner, search.collection.slug, search_attempt_id: search.id)
-else #Work
-link = paged_search_path(search)

div =link_to search.query, link
div =link_to search.query, search.results_link
td.nowrap =t(".search_type.#{search.search_type}")
td
-if search.collection
=link_to search.collection.title, collection_path(search.collection.owner, search.collection)
-elsif search.document_set
=link_to search.document_set.title, collection_path(search.document_set.owner, search.document_set)
-elsif search.work
=link_to search.work.collection.title, collection_path(search.work.collection.owner, search.work.collection)
td
Expand Down
10 changes: 8 additions & 2 deletions app/views/collection/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@
=link_to @collection.owner.display_name, user_profile_path(@collection.owner)

=form_tag({:controller => 'search_attempt', :action => 'create'}, :method => :get, class: 'collection-search') do
=hidden_field_tag('collection_id', @collection.slug)
-if @collection.is_a?(DocumentSet)
=hidden_field_tag('document_set_id', @collection.slug)
-else
=hidden_field_tag('collection_id', @collection.slug)
=search_field_tag :search, nil, placeholder: t('.search_the_text'), "aria-label" => t('.search_the_text_1')
=button_tag t('.search')
=label_tag 'search_string', t('.search_the_text_1'), class: 'hidden'

=form_tag({:controller => 'search_attempt', :action => 'create'}, method: :get, enforce_utf8: false, class: 'collection-search') do
=hidden_field_tag('collection_id', @collection.slug, {:id => "collection id for search work"})
-if @collection.is_a?(DocumentSet)
=hidden_field_tag('document_set_id', @collection.slug, {:id => "document set id for search work"})
-else
=hidden_field_tag('collection_id', @collection.slug, {:id => "collection id for search work"})
=search_field_tag :search_by_title, @search_attemp&.query, placeholder: t('.search_by_title'), "aria-label" => t('.search_by_title_1')
=button_tag t('.search')
=label_tag 'search', t('.search_by_title_1'), class: 'hidden'
Expand Down
7 changes: 2 additions & 5 deletions app/views/shared/_breadcrumbs.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
-if @work && @work.title.present? && (@page || @article)
-path.push(link_to @work.title, collection_read_work_path(@collection.owner, @collection, @work))

-if search_attempt && (@page || @article)
-if search_attempt.search_type == "collection"
-path.push(link_to t('.search_results'), paged_search_path(search_attempt))
-elsif search_attempt.search_type == "work" && (@page || @article)
-path.push(link_to t('.search_results'), paged_search_path(search_attempt))
-if search_attempt && (@page || @article) && (search_attempt.search_type == "collection" || search_attempt.search_type == "work")
-path.push(link_to t('.search_results'), paged_search_path(search_attempt))

-if path.present? && @quality_sampling.nil?
-unless (controller_name == 'transcribe' && @collection && @work && @page && current_page?(collection_oneoff_review_page_path(@collection.owner, @collection, @page))) || (@work && @page && @user && current_page?(collection_user_review_page_path))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDocumentSetsToSearchAttempts < ActiveRecord::Migration[6.0]
def change
add_reference :search_attempts, :document_set, null: true
end
end