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 @@ ...@@ -2,49 +2,35 @@
module ResourceEvents module ResourceEvents
class ChangeMilestoneService 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) def initialize(resource:, user:, created_at: Time.now)
@resource = resource @resource = resource
@user = user @user = user
@event_created_at = created_at @event_created_at = created_at
@milestone = resource&.milestone
@resource_args = {
user_id: user.id,
created_at: event_created_at
}
end end
def execute def execute
args = build_resource_args ResourceMilestoneEvent.create(build_resource_args)
action = if milestone.nil?
:remove
else
:add
end
record = args.merge(milestone_id: milestone&.id, action: ResourceMilestoneEvent.actions[action]) resource.expire_note_etag_cache
create_event(record)
end end
private private
def milestone
resource&.milestone
end
def create_event(record)
ResourceMilestoneEvent.create(record)
resource.expire_note_etag_cache
end
def build_resource_args 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 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