Browse Source

Fixes #3983 - Draft Sharing: Add history entry for updating and deleting of a draft.

Rolf Schmidt 3 years ago
parent
commit
6e42aeb1ee

+ 3 - 1
.rubocop/cop/zammad/exists_reset_column_information.rb

@@ -54,7 +54,9 @@ and check if there are reset_column_information function calls existing for the
         end
 
         def reset_class(node)
-          node.children[0].children[1].to_s
+          # simplify namespaced class names
+          # Rubocop can't reliably convert table names to namespaced class names
+          node.children[0].const_name.gsub '::', ''
         end
 
         def table_class(node)

+ 2 - 0
app/assets/javascripts/app/controllers/_application_controller/_modal_generic_history.coffee

@@ -44,6 +44,8 @@ class App.GenericHistory extends App.ControllerModal
 
       if item.object is 'Ticket::Article'
         item.object = 'Article'
+      if item.object is 'Ticket::SharedDraftZoom'
+        item.object = 'Draft'
 
       data = item
       data.created_by = App.User.find( item.created_by_id )

+ 1 - 1
app/models/ticket.rb

@@ -71,7 +71,7 @@ class Ticket < ApplicationModel
                              :article_count,
                              :preferences
 
-  history_relation_object 'Ticket::Article', 'Mention'
+  history_relation_object 'Ticket::Article', 'Mention', 'Ticket::SharedDraftZoom'
 
   sanitized_html :note
 

+ 2 - 0
app/models/ticket/shared_draft_start.rb

@@ -5,6 +5,8 @@ class Ticket::SharedDraftStart < ApplicationModel
   include ChecksClientNotification
 
   belongs_to :group
+  belongs_to :created_by, class_name: 'User'
+  belongs_to :updated_by, class_name: 'User'
 
   validates :name, presence: true
 

+ 17 - 0
app/models/ticket/shared_draft_zoom.rb

@@ -3,14 +3,31 @@
 class Ticket::SharedDraftZoom < ApplicationModel
   include CanCloneAttachments
   include ChecksClientNotification
+  include HasHistory
 
   belongs_to :ticket, touch: true
+  belongs_to :created_by, class_name: 'User'
+  belongs_to :updated_by, class_name: 'User'
 
   store :new_article
   store :ticket_attributes
 
+  history_attributes_ignored :new_article,
+                             :ticket_attributes
+
   # required by CanCloneAttachments
   def content_type
     'text/html'
   end
+
+  def history_log_attributes
+    {
+      related_o_id:           self['ticket_id'],
+      related_history_object: 'Ticket',
+    }
+  end
+
+  def history_destroy
+    history_log('removed', created_by_id)
+  end
 end

+ 4 - 8
db/migrate/20120101000010_create_ticket.rb

@@ -593,10 +593,8 @@ class CreateTicket < ActiveRecord::Migration[4.2]
       t.references :ticket, null: false, foreign_key: { to_table: :tickets }
       t.text       :new_article
       t.text       :ticket_attributes
-
-      t.column :created_by_id, :integer, null: true
-      t.column :updated_by_id, :integer, null: true
-
+      t.column :created_by_id, :integer, null: false
+      t.column :updated_by_id, :integer, null: false
       t.timestamps limit: 3
     end
 
@@ -604,10 +602,8 @@ class CreateTicket < ActiveRecord::Migration[4.2]
       t.references :group, null: false, foreign_key: { to_table: :groups }
       t.string     :name
       t.text       :content
-
-      t.column :created_by_id, :integer, null: true
-      t.column :updated_by_id, :integer, null: true
-
+      t.column :created_by_id, :integer, null: false
+      t.column :updated_by_id, :integer, null: false
       t.timestamps limit: 3
     end
   end

+ 16 - 0
db/migrate/20220228095014_fix_draft_user_required.rb

@@ -0,0 +1,16 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+class FixDraftUserRequired < ActiveRecord::Migration[6.0]
+  def change
+    # return if it's a new setup
+    return if !Setting.exists?(name: 'system_init_done')
+
+    change_column :ticket_shared_draft_zooms, :created_by_id, :integer, null: false
+    change_column :ticket_shared_draft_zooms, :updated_by_id, :integer, null: false
+    change_column :ticket_shared_draft_starts, :created_by_id, :integer, null: false
+    change_column :ticket_shared_draft_starts, :updated_by_id, :integer, null: false
+
+    Ticket::SharedDraftStart.reset_column_information
+    Ticket::SharedDraftZoom.reset_column_information
+  end
+end

+ 2 - 0
spec/factories/ticket/shared_draft/start.rb

@@ -5,5 +5,7 @@ FactoryBot.define do
     name    { Faker::Name.unique.name }
     group   { create(:group) }
     content { { content: true } }
+    updated_by_id { 1 }
+    created_by_id { 1 }
   end
 end

+ 2 - 0
spec/factories/ticket/shared_draft/zooms.rb

@@ -5,5 +5,7 @@ FactoryBot.define do
     ticket            { create(:ticket) }
     new_article       { { new_article: true } }
     ticket_attributes { { ticket_attributes: true } }
+    updated_by_id { 1 }
+    created_by_id { 1 }
   end
 end

+ 14 - 0
spec/models/ticket/shared_draft_zoom_spec.rb

@@ -8,4 +8,18 @@ RSpec.describe Ticket::SharedDraftZoom, type: :model do
   it { is_expected.to belong_to :ticket }
   it { expect(shared_draft_zoom.new_article).to be_a(Hash) }
   it { expect(shared_draft_zoom.ticket_attributes).to be_a(Hash) }
+
+  describe 'Draft Sharing: Add history entry for updating and deleting of a draft #3983' do
+    it 'does create a history entry for the new draft' do
+      expect(shared_draft_zoom.ticket.history_get)
+        .to include(include('object' => 'Ticket::SharedDraftZoom', 'type' => 'created'))
+    end
+
+    it 'does add a history entry for removing the draft' do
+      shared_draft_zoom.destroy
+
+      expect(shared_draft_zoom.ticket.history_get)
+        .to include(include('object' => 'Ticket::SharedDraftZoom', 'type' => 'removed'))
+    end
+  end
 end

Some files were not shown because too many files changed in this diff