diff --git a/app/controllers/papyrus/admin/templates_controller.rb b/app/controllers/papyrus/admin/templates_controller.rb index d2ecf42..629e978 100644 --- a/app/controllers/papyrus/admin/templates_controller.rb +++ b/app/controllers/papyrus/admin/templates_controller.rb @@ -6,6 +6,7 @@ module Papyrus module Admin class TemplatesController < ApplicationAdminController before_action :set_objects, except: [:index] + before_action :set_template_and_version, only: [:rollback] def index; end @@ -31,6 +32,7 @@ def show def update @template = Papyrus::Template.visible.find(params[:id]) @template.update(template_params) + PaperTrail::Version.where('created_at < ?', 1.year.ago).delete_all respond_with :admin, @template end @@ -46,8 +48,19 @@ def purge_attachment attachment.purge if attachment end + def rollback + reverted_template = @version.reify + @template.update(reverted_template.attributes) + respond_with :admin, @template + end + private + def set_template_and_version + @template = Papyrus::Template.visible.find(params[:id]) + @version = @template.versions.find(params[:version_id]) + end + def set_objects; end def template_params diff --git a/app/javascript/papyrus/controllers/paper_trail_controller.js b/app/javascript/papyrus/controllers/paper_trail_controller.js new file mode 100644 index 0000000..057ca16 --- /dev/null +++ b/app/javascript/papyrus/controllers/paper_trail_controller.js @@ -0,0 +1,34 @@ +import { Controller } from "@hotwired/stimulus"; + +export default class extends Controller { + static targets = ["diffContainer", "dialog"]; + + + toggle(event) { + + const button = event.currentTarget; + const currentRow = button.closest("tr"); + const diffContainer = currentRow.nextElementSibling; + + if (diffContainer) { + diffContainer.classList.toggle("hidden"); + } + } + + show(event) { + event.preventDefault(); + const button = event.currentTarget; + const versionId = button.dataset.versionId; + const dialog = document.getElementById(`rollback-dialog-${versionId}`); + + if (dialog) { + dialog.classList.toggle("hidden"); + } else { + console.error(`Dialog for version ${versionId} not found!`); + } + } + + + + +} diff --git a/app/models/papyrus/template.rb b/app/models/papyrus/template.rb index e260021..1d2f136 100644 --- a/app/models/papyrus/template.rb +++ b/app/models/papyrus/template.rb @@ -1,5 +1,8 @@ module Papyrus class Template < ApplicationRecord + + has_paper_trail if defined?(PaperTrail) + KINDS = [%w[PDF pdf], %w[Liquid liquid]].freeze has_many :papers diff --git a/app/views/papyrus/_paper_trail.html.slim b/app/views/papyrus/_paper_trail.html.slim new file mode 100644 index 0000000..64579af --- /dev/null +++ b/app/views/papyrus/_paper_trail.html.slim @@ -0,0 +1,77 @@ +- if defined?(PaperTrail) && @template.versions.present? + - current_version = YAML.dump(@template.attributes) + - previous_version = @template.versions.last.reify ? YAML.dump(@template.versions.last.reify.attributes) : @template.versions.last.object + + - diff = Diffy::SplitDiff.new(previous_version, current_version, format: :html) + + ruby: + rows = [] + rows << [ + @template.versions.last&.id + 1, + @template.versions.last.created_at.strftime('%Y-%m-%d %H:%M:%S'), + User.find(@template.paper_trail.originator).email, + + diff.left.html_safe, + diff.right.html_safe, + @template.versions.last + ] + + @template.versions.reverse.each_with_index do |version| + if version.previous + version_diff = Diffy::SplitDiff.new( + version.previous.reify ? YAML.dump(version.previous.reify.attributes) : YAML.dump(version.previous.object), + YAML.dump(version.reify ? version.reify.attributes : version.attributes), + format: :html) + + rows << [ + version.id, + version.previous.created_at.strftime('%Y-%m-%d %H:%M:%S'), + User.find(version.whodunnit).email || "Unknown", + + version_diff.left.html_safe, + version_diff.right.html_safe, + version + ] + end + end + + + table.table style="width: 100%;" + thead + tr class="bg-gray-100 dark:bg-gray-900 text-gray-800 dark:text-gray-200" + th Version Number + th Changed At + th Changed By + th + th + - rows.each do |row| + tbody data-controller="paper-trail" + tr class="text-center border-b dark:border-gray-600 bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-300" + td class="px-4" + = row[0] + td class="px-4" + = row[1] + td class="px-4" + = row[2] + td class="px-4" + button type="button" class="button" data-action="click->paper-trail#toggle" View Changes + td class="px-4" + button type="button" class="button" data-action="click->paper-trail#show" data-version-id="#{row[0]}" Rollback + + div id="rollback-dialog-#{row[0]}" class="hidden" + = sts.dialog title: "Are you sure you want to rollback?", icon: "fas fa-triangle-exclamation text-red-600" do |dialog| + | Are you sure you want to revert to version #{row[0]}? + - dialog.with_action + button.button data-action="satis-dialog#close" + | Cancel + - dialog.with_action + = button_to 'Rollback', rollback_admin_template_path(@template, version_id: row[5].id), method: :patch, class: 'button harmful' + + + tr data-paper-trail-target="diffContainer" class="hidden border-b dark:border-gray-600" + td colspan="5" + div class="w-[49%] float-left mr-2" + = row[3] + div class="w-[49%] float-left" + = row[4] + diff --git a/app/views/papyrus/admin/templates/edit.html.slim b/app/views/papyrus/admin/templates/edit.html.slim index a1535b8..dc6a832 100644 --- a/app/views/papyrus/admin/templates/edit.html.slim +++ b/app/views/papyrus/admin/templates/edit.html.slim @@ -80,4 +80,8 @@ .grid.grid-cols-12.gap-4 .col-span-12 = f.input :metadata, as: :editor, input_html: { value: YAML.dump(f.object.metadata) }, lang: 'yaml' - = card.with_table(:papyrus_papers, parameters: {template_id: @template.id}, custom_views: false, tab: :papers) \ No newline at end of file + = card.with_table(:papyrus_papers, parameters: {template_id: @template.id}, custom_views: false, tab: :papers) + - card.with_tab:previous_versions + .grid.grid-cols-12.gap-4 + .col-span-12 + = render partial: 'papyrus/paper_trail' , locals: { card: card } \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ea2534b..9deef3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,5 +36,18 @@ post 'paper' end end + + namespace :admin do + resources :templates do + member do + patch :rollback, to: 'templates#rollback' + end + end + end + + namespace :admin do + get 'dialog/rollback', to: 'papyrus/#rollback', as: :rollback_dialog + end + root to: 'dashboard#show' end