Browse Source

Refactoring: Eliminate explicit coupling of classes in ChecksImport concern

Ryan Lue 6 years ago
parent
commit
46615333f9

+ 8 - 4
app/models/application_model/checks_import.rb

@@ -6,13 +6,17 @@ module ApplicationModel::ChecksImport
     before_create :check_attributes_protected
   end
 
-  def check_attributes_protected
-
-    import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ]
+  class_methods do
+    # Use `include CanBeImported` in a class to override this method
+    def importable?
+      false
+    end
+  end
 
+  def check_attributes_protected
     # do noting, use id as it is
     return if !Setting.get('system_init_done')
-    return if Setting.get('import_mode') && import_class_list.include?(self.class.to_s)
+    return if Setting.get('import_mode') && self.class.importable?
     return if !has_attribute?(:id)
 
     self[:id] = nil

+ 10 - 0
app/models/concerns/can_be_imported.rb

@@ -0,0 +1,10 @@
+module CanBeImported
+  extend ActiveSupport::Concern
+
+  # methods defined here are going to extend the class, not the instance of it
+  class_methods do
+    def importable?
+      true
+    end
+  end
+end

+ 1 - 0
app/models/group.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 
 class Group < ApplicationModel
+  include CanBeImported
   include HasActivityStreamLog
   include ChecksClientNotification
   include ChecksLatestChangeObserved

+ 1 - 0
app/models/history.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 
 class History < ApplicationModel
+  include CanBeImported
   include History::Assets
 
   self.table_name = 'histories'

+ 1 - 0
app/models/role.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 
 class Role < ApplicationModel
+  include CanBeImported
   include HasActivityStreamLog
   include ChecksClientNotification
   include ChecksLatestChangeObserved

+ 1 - 0
app/models/ticket.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 
 class Ticket < ApplicationModel
+  include CanBeImported
   include HasActivityStreamLog
   include ChecksClientNotification
   include ChecksLatestChangeObserved

+ 1 - 0
app/models/ticket/article.rb

@@ -1,5 +1,6 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 class Ticket::Article < ApplicationModel
+  include CanBeImported
   include HasActivityStreamLog
   include ChecksClientNotification
   include HasHistory

+ 1 - 0
app/models/ticket/priority.rb

@@ -1,5 +1,6 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 class Ticket::Priority < ApplicationModel
+  include CanBeImported
   self.table_name = 'ticket_priorities'
   validates :name, presence: true
 

+ 1 - 0
app/models/ticket/state.rb

@@ -1,5 +1,6 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 class Ticket::State < ApplicationModel
+  include CanBeImported
   include ChecksLatestChangeObserved
 
   belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states

+ 1 - 0
app/models/ticket/state_type.rb

@@ -1,5 +1,6 @@
 # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
 class Ticket::StateType < ApplicationModel
+  include CanBeImported
   include ChecksLatestChangeObserved
 
   has_many :states, class_name: 'Ticket::State', inverse_of: :state_type

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