File: widgetable_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (65 lines) | stat: -rw-r--r-- 2,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module WorkItems
  module WidgetableService
    extend ActiveSupport::Concern

    included do
      attr_reader :widget_params
    end

    # rubocop:disable Gitlab/ModuleWithInstanceVariables
    def initialize_callbacks!(work_item)
      @callbacks = work_item.widgets.filter_map do |widget|
        callback_class = widget.class.try(:callback_class)
        next if callback_class.nil?

        callback_params = widget_params[widget.class.api_symbol] || {}
        callback_params[:excluded_in_new_type] = true if new_type_excludes_widget?(widget, work_item.resource_parent)

        if callback_class.const_defined?(:ALLOWED_PARAMS)
          callback_params.reverse_merge!(params.slice(*callback_class::ALLOWED_PARAMS))
        end

        next if callback_params.blank?

        callback_class.new(issuable: work_item, current_user: current_user, params: callback_params)
      end

      remove_callback_params
      @callbacks.each(&:after_initialize)
    end
    # rubocop:enable Gitlab/ModuleWithInstanceVariables

    def handle_quick_actions(work_item)
      # Do not handle quick actions from params[:description] unless the work item is the default Issue.
      super if work_item.work_item_type == WorkItems::Type.default_by_type(:issue)

      # Handle quick actions from description widget depending on the available widgets for the type
      handle_widget_quick_actions!(work_item)
    end

    private

    def new_type_excludes_widget?(widget, resource_parent)
      return false unless params[:work_item_type]

      params[:work_item_type].widget_classes(resource_parent).exclude?(widget.class)
    end

    def handle_widget_quick_actions!(work_item)
      return unless work_item.has_widget?(:description)

      description_widget_params = widget_params[::WorkItems::Widgets::Description.api_symbol]
      return unless description_widget_params

      merge_quick_actions_into_params!(work_item, params: description_widget_params)

      # exclude `description` param so that it is not passed into common params after transform_quick_action_params
      parsed_params = work_item.transform_quick_action_params(description_widget_params.except(:description))

      widget_params.merge!(parsed_params[:widgets])
      params.merge!(parsed_params[:common])
    end
  end
end