Browse Source

Fixes #1653 - Default value not set for attributes of type input, select,...

Mantas Masalskis 3 years ago
parent
commit
0668e17d18

+ 1 - 1
app/assets/javascripts/app/index.coffee

@@ -53,7 +53,7 @@ class App extends Spine.Controller
   @viewPrintItem: (item, attributeConfig = {}, valueRef, table, object) ->
     return '-' if item is undefined
     return '-' if item is ''
-    return item if item is null
+    return '-' if item is null
     result = ''
     items = [item]
     if _.isArray(item)

+ 1 - 1
app/models/application_model/can_lookup_search_index_attributes.rb

@@ -70,7 +70,7 @@ returns
       attributes[ attribute_ref_name ] = value
     end
 
-    if self.class.include?(HasObjectManagerAttributesValidation)
+    if is_a? HasObjectManagerAttributes
       RequestCache.integer_fields(self.class.to_s).each do |field|
         next if attributes[field].blank?
 

+ 14 - 0
app/models/concerns/has_object_manager_attributes.rb

@@ -0,0 +1,14 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+module HasObjectManagerAttributes
+  extend ActiveSupport::Concern
+
+  included do
+    # Disable table inheritance to allow columns with the name 'type'.
+    self.inheritance_column = nil
+
+    validates_with ObjectManager::Attribute::Validation, on: %i[create update]
+
+    after_initialize ObjectManager::Attribute::SetDefaults.new
+  end
+end

+ 2 - 3
app/models/concerns/has_object_manager_attributes_validation.rb

@@ -5,9 +5,8 @@ module HasObjectManagerAttributesValidation
   extend ActiveSupport::Concern
 
   included do
-    # Disable table inheritance to allow columns with the name 'type'.
-    self.inheritance_column = nil
+    ActiveSupport::Deprecation.warn("Concern 'HasObjectManagerAttributesValidation' is  deprecated. Use 'HasObjectManagerValidation' instead.")
 
-    validates_with ObjectManager::Attribute::Validation, on: %i[create update]
+    include HasObjectManagerAttributes
   end
 end

+ 1 - 1
app/models/group.rb

@@ -7,7 +7,7 @@ class Group < ApplicationModel
   include ChecksHtmlSanitized
   include ChecksLatestChangeObserved
   include HasHistory
-  include HasObjectManagerAttributesValidation
+  include HasObjectManagerAttributes
   include HasCollectionUpdate
   include HasTicketCreateScreenImpact
   include HasSearchIndexBackend

+ 7 - 0
app/models/object_manager/attribute.rb

@@ -38,6 +38,13 @@ class ObjectManager::Attribute < ApplicationModel
 
   before_validation :set_base_options
 
+  scope :active,     -> { where(active:   true) }
+  scope :editable,   -> { where(editable: true) }
+  scope :for_object, lambda { |name_or_klass|
+    id = ObjectLookup.lookup(name: name_or_klass.to_s)
+    where(object_lookup_id: id)
+  }
+
 =begin
 
 list of all attributes

+ 62 - 0
app/models/object_manager/attribute/set_defaults.rb

@@ -0,0 +1,62 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+class ObjectManager
+  class Attribute
+    class SetDefaults
+      def after_initialize(record)
+        return if !record.new_record?
+
+        attributes_for(record).each { |attr, config| set_value(record, attr, config) }
+      end
+
+      private
+
+      def set_value(record, attr, config)
+        method_name = "#{attr}="
+
+        return if !record.respond_to? method_name
+        return if record.send(attr).present?
+
+        record.send method_name, build_value(config)
+      end
+
+      def build_value(config)
+        case config[:data_type]
+        when 'date'
+          config[:diff].days.from_now
+        when 'datetime'
+          config[:diff].hours.from_now
+        else
+          config[:default]
+        end
+      end
+
+      def attributes_for(record)
+        query     = ObjectManager::Attribute.active.editable.for_object(record.class)
+        cache_key = "#{query.cache_key}/attribute_defaults"
+
+        Rails.cache.fetch cache_key do
+          query
+            .map { |attr| { attr.name => config_of(attr) } }
+            .reduce({}, :merge)
+            .compact
+        end
+      end
+
+      def config_of(attr)
+        case attr.data_type
+        when 'date', 'datetime'
+          {
+            data_type: attr.data_type,
+            diff:      attr.data_option&.dig(:diff)
+          }
+        else
+          {
+            data_type: attr.data_type,
+            default:   attr.data_option&.dig(:default)
+          }
+        end
+      end
+    end
+  end
+end

+ 1 - 1
app/models/organization.rb

@@ -8,7 +8,7 @@ class Organization < ApplicationModel
   include HasSearchIndexBackend
   include CanCsvImport
   include ChecksHtmlSanitized
-  include HasObjectManagerAttributesValidation
+  include HasObjectManagerAttributes
   include HasTaskbars
 
   include Organization::Assets

+ 1 - 1
app/models/ticket.rb

@@ -13,7 +13,7 @@ class Ticket < ApplicationModel
   include HasOnlineNotifications
   include HasKarmaActivityLog
   include HasLinks
-  include HasObjectManagerAttributesValidation
+  include HasObjectManagerAttributes
   include HasTaskbars
   include Ticket::CallsStatsTicketReopenLog
   include Ticket::EnqueuesUserTicketCounterJob

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

@@ -8,7 +8,7 @@ class Ticket::Article < ApplicationModel
   include ChecksHtmlSanitized
   include CanCsvImport
   include CanCloneAttachments
-  include HasObjectManagerAttributesValidation
+  include HasObjectManagerAttributes
 
   include Ticket::Article::Assets
   include Ticket::Article::EnqueueCommunicateEmailJob

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