Commit a3ccdf95 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'minor-change-milestone-service-cleanup' into 'master'

Cleanup ChangeMilestoneService

See merge request gitlab-org/gitlab!25691
parents 6668f924 177e345a
......@@ -2,49 +2,35 @@
module ResourceEvents
class ChangeMilestoneService
attr_reader :resource, :user, :event_created_at, :resource_args
attr_reader :resource, :user, :event_created_at, :milestone
def initialize(resource:, user:, created_at: Time.now)
@resource = resource
@user = user
@event_created_at = created_at
@resource_args = {
user_id: user.id,
created_at: event_created_at
}
@milestone = resource&.milestone
end
def execute
args = build_resource_args
action = if milestone.nil?
:remove
else
:add
end
ResourceMilestoneEvent.create(build_resource_args)
record = args.merge(milestone_id: milestone&.id, action: ResourceMilestoneEvent.actions[action])
create_event(record)
resource.expire_note_etag_cache
end
private
def milestone
resource&.milestone
end
def create_event(record)
ResourceMilestoneEvent.create(record)
resource.expire_note_etag_cache
end
def build_resource_args
key = resource.class.name.underscore.foreign_key
action = milestone.blank? ? :remove : :add
key = resource.class.name.foreign_key
resource_args.merge(key => resource.id, state: ResourceMilestoneEvent.states[resource.state])
{
user_id: user.id,
created_at: event_created_at,
milestone_id: milestone&.id,
state: ResourceMilestoneEvent.states[resource.state],
action: ResourceMilestoneEvent.actions[action],
key => resource.id
}
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment