debian-mirror-gitlab/app/services/concerns/work_items/widgetable_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.8 KiB
Ruby
Raw Normal View History

2022-08-13 15:12:31 +05:30
# frozen_string_literal: true
module WorkItems
module WidgetableService
2023-06-20 00:43:36 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def initialize_callbacks!(work_item)
@callbacks = work_item.widgets.filter_map do |widget|
callback_class = widget.class.try(:callback_class)
callback_params = @widget_params[widget.class.api_symbol]
if new_type_excludes_widget?(widget)
callback_params = {} if callback_params.nil?
callback_params[:excluded_in_new_type] = true
end
next if callback_class.nil? || callback_params.blank?
callback_class.new(issuable: work_item, current_user: current_user, params: callback_params)
end
@callbacks.each(&:after_initialize)
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2022-11-25 23:54:43 +05:30
def execute_widgets(work_item:, callback:, widget_params: {}, service_params: {})
2022-08-13 15:12:31 +05:30
work_item.widgets.each do |widget|
2023-06-20 00:43:36 +05:30
widget_service(widget, service_params).try(callback, params: widget_params[widget.class.api_symbol] || {})
2022-08-13 15:12:31 +05:30
end
end
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2022-11-25 23:54:43 +05:30
def widget_service(widget, service_params)
2022-08-13 15:12:31 +05:30
@widget_services ||= {}
return @widget_services[widget] if @widget_services.has_key?(widget)
2022-11-25 23:54:43 +05:30
@widget_services[widget] = widget_service_class(widget)&.new(
widget: widget,
current_user: current_user,
service_params: service_params
)
2022-08-13 15:12:31 +05:30
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
def widget_service_class(widget)
2022-08-27 11:52:29 +05:30
"WorkItems::Widgets::#{widget.type.to_s.camelize}Service::#{self.class.name.demodulize}".constantize
2022-08-13 15:12:31 +05:30
rescue NameError
nil
end
2023-06-20 00:43:36 +05:30
private
def new_type_excludes_widget?(widget)
return false unless params[:work_item_type]
params[:work_item_type].widgets.exclude?(widget.class)
end
2022-08-13 15:12:31 +05:30
end
end