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
13 changes: 13 additions & 0 deletions app/controllers/papyrus/admin/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions app/javascript/papyrus/controllers/paper_trail_controller.js
Original file line number Diff line number Diff line change
@@ -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!`);
}
}




}
3 changes: 3 additions & 0 deletions app/models/papyrus/template.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
77 changes: 77 additions & 0 deletions app/views/papyrus/_paper_trail.html.slim
Original file line number Diff line number Diff line change
@@ -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]

6 changes: 5 additions & 1 deletion app/views/papyrus/admin/templates/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
= 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 }
13 changes: 13 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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