2019-02-27 14:29:53 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Activities
|
|
|
|
class CreateActivityService
|
|
|
|
extend Service
|
|
|
|
|
|
|
|
attr_reader :errors, :activity
|
|
|
|
|
|
|
|
def initialize(activity_type:,
|
|
|
|
owner:,
|
|
|
|
team:,
|
2019-02-28 21:01:58 +08:00
|
|
|
project: nil,
|
2019-02-27 14:29:53 +08:00
|
|
|
subject:,
|
|
|
|
message_items: {})
|
|
|
|
@activity = Activity.new
|
|
|
|
@activity.type_of = activity_type
|
|
|
|
@activity.owner = owner
|
|
|
|
@activity.team = team
|
|
|
|
@activity.subject = subject
|
|
|
|
@activity.project = project if project
|
2019-02-28 21:01:58 +08:00
|
|
|
@message_items = message_items
|
2019-02-27 14:29:53 +08:00
|
|
|
|
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2019-02-28 21:01:58 +08:00
|
|
|
enrich_message_items
|
2019-03-19 20:33:32 +08:00
|
|
|
@activity.generate_breadcrumbs
|
2019-03-28 20:43:50 +08:00
|
|
|
@activity.save!
|
2019-02-27 14:29:53 +08:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed?
|
|
|
|
@errors.none?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-28 21:01:58 +08:00
|
|
|
def enrich_message_items
|
|
|
|
@message_items.each do |k, v|
|
2019-04-02 20:21:06 +08:00
|
|
|
if v.is_a? Time
|
|
|
|
@activity.message_items[k] = {
|
|
|
|
type: 'Time',
|
|
|
|
value: v.to_i
|
|
|
|
}
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-02-28 21:01:58 +08:00
|
|
|
const = try_to_constantize k
|
2019-08-09 15:47:07 +08:00
|
|
|
|
|
|
|
k = k.to_s.sub('tiny_mce_asset', 'asset').to_sym if k.to_s.include? 'tiny_mce_asset'
|
|
|
|
|
2023-11-13 16:34:38 +08:00
|
|
|
if const && (v.is_a?(Hash) || v.to_i != 0 || v.nil?)
|
2021-01-22 18:24:08 +08:00
|
|
|
if v.is_a?(Hash) # Value is Hash, so you have getter specified
|
2019-02-28 21:01:58 +08:00
|
|
|
id = v[:id]
|
|
|
|
getter_method = v[:value_for]
|
2019-04-02 19:15:34 +08:00
|
|
|
value_type = v[:value_type]
|
2019-02-28 21:01:58 +08:00
|
|
|
else
|
|
|
|
id = v
|
|
|
|
getter_method = 'name'
|
|
|
|
end
|
|
|
|
|
2021-01-22 18:24:08 +08:00
|
|
|
if id
|
|
|
|
obj = const.find id
|
|
|
|
@activity.message_items[k] = { type: const.to_s, value: obj.public_send(getter_method).to_s, id: id }
|
|
|
|
@activity.message_items[k][:value_for] = getter_method
|
|
|
|
@activity.message_items[k][:value_type] = value_type unless value_type.nil?
|
|
|
|
else
|
|
|
|
@activity.message_items[k] = { type: const.to_s, value: nil, value_for: 'name', id: nil }
|
|
|
|
end
|
2019-02-28 21:01:58 +08:00
|
|
|
else
|
|
|
|
@activity.message_items[k] = v.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def try_to_constantize(key)
|
|
|
|
const_name = key.to_s.camelize
|
|
|
|
# Remove last part from and with '_xxx..x'
|
2019-03-26 18:39:54 +08:00
|
|
|
const_name_short = key.to_s.sub(/_[^_]*\z/, '').camelize
|
2019-02-28 21:01:58 +08:00
|
|
|
|
|
|
|
if Extends::ACTIVITY_MESSAGE_ITEMS_TYPES.include? const_name
|
|
|
|
const_name.constantize
|
2019-03-26 18:39:54 +08:00
|
|
|
elsif Extends::ACTIVITY_MESSAGE_ITEMS_TYPES.include? const_name_short
|
|
|
|
const_name_short.constantize
|
2019-02-28 21:01:58 +08:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2019-02-27 14:29:53 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|