diff --git a/app/controllers/api/templates_controller.rb b/app/controllers/api/templates_controller.rb index b3d05cec..d20defb3 100644 --- a/app/controllers/api/templates_controller.rb +++ b/app/controllers/api/templates_controller.rb @@ -48,7 +48,7 @@ module Api archived = params.key?(:archived) ? params[:archived] : params.dig(:template, :archived) - if archived.in?([true, false]) + if archived.in?([true, false]) && current_ability.can?(:destroy, @template) @template.archived_at = archived == true ? Time.current : nil end diff --git a/app/controllers/templates_restore_controller.rb b/app/controllers/templates_restore_controller.rb index 422b69cf..64afea11 100644 --- a/app/controllers/templates_restore_controller.rb +++ b/app/controllers/templates_restore_controller.rb @@ -4,7 +4,7 @@ class TemplatesRestoreController < ApplicationController load_and_authorize_resource :template def create - authorize!(:update, @template) + authorize!(:destroy, @template) @template.update!(archived_at: nil) diff --git a/app/controllers/templates_shared_controller.rb b/app/controllers/templates_shared_controller.rb index ea6193aa..90b5d159 100644 --- a/app/controllers/templates_shared_controller.rb +++ b/app/controllers/templates_shared_controller.rb @@ -4,10 +4,16 @@ class TemplatesSharedController < ApplicationController def index authorize!(:read, Template) + @is_archived = params[:archived] == 'true' + @templates = Templates.shared(current_user) - .active - .preload(:author, :template_accesses, :template_sharings) - .order(id: :desc) + + @has_archived = !@is_archived && @templates.archived.exists? + + @templates = @is_archived ? @templates.archived : @templates.active + + @templates = @templates.preload(:author, :template_accesses, :template_sharings) + .order(id: :desc) @templates = Templates.search(current_user, @templates, params[:q]) diff --git a/app/views/shared/_search_input.html.erb b/app/views/shared/_search_input.html.erb index 8457b2af..1868c658 100644 --- a/app/views/shared/_search_input.html.erb +++ b/app/views/shared/_search_input.html.erb @@ -4,6 +4,9 @@ <% end %> <% end %> + <% if params[:archived].present? %> + + <% end %> <% if params[:q].present? %>
diff --git a/app/views/templates/_template.html.erb b/app/views/templates/_template.html.erb index 80282def..5e705441 100644 --- a/app/views/templates/_template.html.erb +++ b/app/views/templates/_template.html.erb @@ -19,7 +19,7 @@ <%= svg_icon('calendar', class: 'w-4 h-4') %> <%= l(template.created_at.in_time_zone(current_account.timezone), format: :short, locale: current_account.locale) %> - <% if template.archived_at? %> + <% if local_assigns[:with_folder] %> <%= svg_icon('folder', class: 'w-4 h-4 flex-shrink-0') %> <%= template.folder.full_name %> @@ -37,14 +37,14 @@ <% end %> - <% if template.archived_at? && can?(:update, template) %> + <% if template.archived_at? && can?(:destroy, template) %> <%= button_to template_restore_index_path(template), class: 'btn btn-xs hover:btn-outline bg-base-200 btn-circle' do %> <%= svg_icon('rotate', class: 'w-4 h-4 enabled') %> <%= svg_icon('loader', class: 'w-4 h-4 animate-spin disabled') %> <% end %> - <% elsif can?(:update, template) %> + <% elsif !template.archived_at? && can?(:update, template) %> <%= svg_icon('pencil', class: 'w-4 h-4') %> diff --git a/app/views/templates/_title.html.erb b/app/views/templates/_title.html.erb index d429ff5e..c694bb30 100644 --- a/app/views/templates/_title.html.erb +++ b/app/views/templates/_title.html.erb @@ -91,7 +91,7 @@ <% end %> <% end %> <% if template.archived_at? %> - <% if can?(:create, template) %> + <% if can?(:destroy, template) %> <%= button_to button_title(title: t('restore'), disabled_with: t('restoring')[..-4], icon: svg_icon('rotate', class: 'w-6 h-6')), template_restore_index_path(template), class: 'btn btn-outline btn-sm flex-1' %> <% end %> <%= link_to template_preview_path(template), class: 'btn btn-outline btn-sm flex-1' do %> diff --git a/app/views/templates_archived/index.html.erb b/app/views/templates_archived/index.html.erb index 995a63ad..233e1463 100644 --- a/app/views/templates_archived/index.html.erb +++ b/app/views/templates_archived/index.html.erb @@ -14,7 +14,7 @@
<% if @pagy.count.nil? || @pagy.count > 0 %>
- <%= render partial: 'templates/template', collection: @templates %> + <%= render partial: 'templates/template', collection: @templates, locals: { with_folder: true } %>
<% elsif params[:q].present? %>
diff --git a/app/views/templates_shared/index.html.erb b/app/views/templates_shared/index.html.erb index c7bf456d..5320558b 100644 --- a/app/views/templates_shared/index.html.erb +++ b/app/views/templates_shared/index.html.erb @@ -1,13 +1,16 @@
- <%= link_to root_path, class: 'flex items-center' do %> + <%= link_to(@is_archived ? templates_shared_index_path : root_path, class: 'flex items-center') do %> <%= svg_icon('chevron_left', class: 'w-5 h-5') %> - <%= t('home') %> + <%= @is_archived ? t('back_to_active') : t('home') %> <% end %>

<%= svg_icon('folder', class: 'w-9 h-9 flex-shrink-0') %> <%= t('shared') %> + <% if @is_archived %> + <%= t('archived') %> + <% end %>

<% if params[:q].present? || @pagy.pages > 1 %> @@ -15,6 +18,13 @@ <% end %>
+<% view_archived_html = capture do %> + <% if @has_archived %> +
+ <%= t('view_archived') %> +
+ <% end %> +<% end %> <% if @pagy.count.nil? || @pagy.count > 0 %>
<%= render partial: 'templates/template', collection: @templates %> @@ -26,4 +36,10 @@
<% end %> -<%= render 'shared/pagination', pagy: @pagy, items_name: 'templates' %> +<% if @pagy.pages > 1 %> + <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %> +<% else %> +
+ <%= view_archived_html %> +
+<% end %>