scinote-web/app/services/activities/create_activity_service.rb

92 lines
2.4 KiB
Ruby
Raw Normal View History

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:,
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
@message_items = message_items
2019-02-27 14:29:53 +08:00
@errors = {}
end
def call
enrich_message_items
@activity.generate_breadcrumbs
@activity.save!
2019-02-27 14:29:53 +08:00
self
end
def succeed?
@errors.none?
end
private
def enrich_message_items
@message_items.each do |k, v|
if v.is_a? Time
@activity.message_items[k] = {
type: 'Time',
value: v.to_i
}
next
end
const = try_to_constantize k
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?)
if v.is_a?(Hash) # Value is Hash, so you have getter specified
id = v[:id]
getter_method = v[:value_for]
2019-04-02 19:15:34 +08:00
value_type = v[:value_type]
else
id = v
getter_method = 'name'
end
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
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'
const_name_short = key.to_s.sub(/_[^_]*\z/, '').camelize
if Extends::ACTIVITY_MESSAGE_ITEMS_TYPES.include? const_name
const_name.constantize
elsif Extends::ACTIVITY_MESSAGE_ITEMS_TYPES.include? const_name_short
const_name_short.constantize
else
false
end
2019-02-27 14:29:53 +08:00
end
end
end