adjust search

pull/696/head
Pete Matsyburka 1 month ago
parent 1eaf4e5bab
commit dc1c386d7e

@ -32,7 +32,9 @@ class TemplateFoldersController < ApplicationController
@pagy, @templates = pagy_auto(@templates, limit:) @pagy, @templates = pagy_auto(@templates, limit:)
load_related_submissions if params[:q].present? && @templates.blank? if params[:q].present? && @templates.blank?
@related_submissions_pagy, @related_submissions = load_related_submissions(@template_folder)
end
else else
@pagy, @template_folders = pagy(@template_folders, limit: FOLDERS_PER_PAGE) @pagy, @template_folders = pagy(@template_folders, limit: FOLDERS_PER_PAGE)
@ -66,20 +68,20 @@ class TemplateFoldersController < ApplicationController
params.require(:template_folder).permit(:name) params.require(:template_folder).permit(:name)
end end
def load_related_submissions def load_related_submissions(template_folder)
@related_submissions = related_submissions =
Submission.accessible_by(current_ability) Submission.accessible_by(current_ability)
.where(archived_at: nil) .where(archived_at: nil)
.where(template_id: current_account.templates.active .where(template_id: current_account.templates.active
.where(folder: [@template_folder, *@template_folder.subfolders]) .where(folder: [template_folder, *template_folder.subfolders])
.select(:id)) .select(:id))
.preload(:template_accesses, :created_by_user, .preload(:template_accesses, :created_by_user,
template: :author, template: :author,
submitters: :start_form_submission_events) submitters: :start_form_submission_events)
@related_submissions = Submissions.search(current_user, @related_submissions, params[:q]) related_submissions = Submissions.search(current_user, related_submissions, params[:q])
.order(id: :desc) .order(id: :desc)
@related_submissions_pagy, @related_submissions = pagy_auto(@related_submissions, limit: 5) pagy_auto(related_submissions, limit: 5)
end end
end end

@ -14,7 +14,13 @@ class TemplatesArchivedController < ApplicationController
return unless params[:q].present? && @templates.blank? return unless params[:q].present? && @templates.blank?
@related_submissions = @related_submissions_pagy, @related_submissions = load_related_submissions
end
private
def load_related_submissions
related_submissions =
Submission.accessible_by(current_ability) Submission.accessible_by(current_ability)
.joins(:template) .joins(:template)
.where.not(templates: { archived_at: nil }) .where.not(templates: { archived_at: nil })
@ -22,9 +28,9 @@ class TemplatesArchivedController < ApplicationController
template: :author, template: :author,
submitters: :start_form_submission_events) submitters: :start_form_submission_events)
@related_submissions = Submissions.search(current_user, @related_submissions, params[:q]) related_submissions = Submissions.search(current_user, related_submissions, params[:q])
.order(id: :desc) .order(id: :desc)
@related_submissions_pagy, @related_submissions = pagy_auto(@related_submissions, limit: 5) pagy_auto(related_submissions, limit: 5)
end end
end end

@ -38,7 +38,9 @@ class TemplatesDashboardController < ApplicationController
@pagy, @templates = load_templates(@templates, @pagy.count) @pagy, @templates = load_templates(@templates, @pagy.count)
load_related_submissions if params[:q].present? && @templates.blank? if params[:q].present? && @templates.blank?
@related_submissions_pagy, @related_submissions = load_related_submissions
end
end end
end end
@ -110,17 +112,17 @@ class TemplatesDashboardController < ApplicationController
end end
def load_related_submissions def load_related_submissions
@related_submissions = Submission.accessible_by(current_ability) related_submissions = Submission.accessible_by(current_ability)
.left_joins(:template) .left_joins(:template)
.where(archived_at: nil) .where(archived_at: nil)
.where(templates: { archived_at: nil }) .where(templates: { archived_at: nil })
.preload(:template_accesses, :created_by_user, .preload(:template_accesses, :created_by_user,
template: :author, template: :author,
submitters: :start_form_submission_events) submitters: :start_form_submission_events)
@related_submissions = Submissions.search(current_user, @related_submissions, params[:q]) related_submissions = Submissions.search(current_user, related_submissions, params[:q])
.order(id: :desc) .order(id: :desc)
@related_submissions_pagy, @related_submissions = pagy_auto(@related_submissions, limit: 5) pagy_auto(related_submissions, limit: 5)
end end
end end

@ -18,5 +18,30 @@ class TemplatesSharedController < ApplicationController
@templates = Templates.search(current_user, @templates, params[:q]) @templates = Templates.search(current_user, @templates, params[:q])
@pagy, @templates = pagy_auto(@templates, limit: 12) @pagy, @templates = pagy_auto(@templates, limit: 12)
return unless params[:q].present? && @templates.blank?
@related_submissions_pagy, @related_submissions = load_related_submissions(is_archived: @is_archived)
end
private
def load_related_submissions(is_archived:)
shared_templates = Templates.shared(current_user)
shared_templates = is_archived ? shared_templates.archived : shared_templates.active
related_submissions =
Submission.accessible_by(current_ability)
.where(template_id: shared_templates.select(:id))
.preload(:template_accesses, :created_by_user,
template: :author,
submitters: :start_form_submission_events)
related_submissions = related_submissions.where(archived_at: nil) unless is_archived
related_submissions = Submissions.search(current_user, related_submissions, params[:q])
.order(id: :desc)
pagy_auto(related_submissions, limit: 5)
end end
end end

@ -30,7 +30,7 @@
<% end %> <% end %>
</h1> </h1>
<div class="flex space-x-2"> <div class="flex space-x-2">
<% if params[:q].present? || @pagy.pages > 1 || @template_folders.present? %> <% if params[:q].present? || @pagy.count.nil? || @pagy.count > 0 || @template_folders.present? %>
<%= render 'shared/search_input' %> <%= render 'shared/search_input' %>
<% end %> <% end %>
<% if can?(:create, ::Template) %> <% if can?(:create, ::Template) %>

@ -23,7 +23,7 @@
</h1> </h1>
</div> </div>
<div class="flex space-x-2"> <div class="flex space-x-2">
<% if params[:q].present? || @pagy.pages > 1 || @template_folders.present? %> <% if params[:q].present? || @pagy.count.nil? || @pagy.count > 1 || @template_folders.present? %>
<%= render 'shared/search_input' %> <%= render 'shared/search_input' %>
<% end %> <% end %>
<% if can?(:create, ::Template) %> <% if can?(:create, ::Template) %>

@ -13,13 +13,13 @@
<% end %> <% end %>
</h1> </h1>
<div class="flex space-x-2"> <div class="flex space-x-2">
<% if params[:q].present? || @pagy.pages > 1 %> <% if params[:q].present? || @pagy.count.nil? || @pagy.count > 1 %>
<%= render 'shared/search_input' %> <%= render 'shared/search_input' %>
<% end %> <% end %>
</div> </div>
</div> </div>
<% view_archived_html = capture do %> <% view_archived_html = capture do %>
<% if @has_archived %> <% if @has_archived && params[:q].blank? %>
<div> <div>
<a href="<%= templates_shared_index_path(archived: true) %>" class="link text-sm"><%= t('view_archived') %></a> <a href="<%= templates_shared_index_path(archived: true) %>" class="link text-sm"><%= t('view_archived') %></a>
</div> </div>
@ -35,6 +35,15 @@
<%= t('templates_not_found') %> <%= t('templates_not_found') %>
</div> </div>
</div> </div>
<% if @related_submissions.present? %>
<h1 class="text-2xl md:text-3xl sm:text-4xl font-bold mt-8 md:mt-4">
<%= t('submissions') %>
</h1>
<div class="space-y-4 mt-4">
<%= render partial: 'templates/submission', collection: @related_submissions, locals: { with_template: true } %>
</div>
<%= render 'shared/pagination', pagy: @related_submissions_pagy, items_name: 'submissions' %>
<% end %>
<% end %> <% end %>
<% if @pagy.pages > 1 %> <% if @pagy.pages > 1 %>
<%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %> <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %>

Loading…
Cancel
Save