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

85 lines
2.1 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
if const
if v.is_a?(Hash) # Value is array, 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
obj = const.find id
@activity.message_items[k] = { type: const.to_s, value: obj.public_send(getter_method).to_s, id: id }
2019-04-02 19:15:34 +08:00
@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] = 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