Commit c4b02642 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Replace milestone observer with services

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent f8ea52c3
......@@ -37,7 +37,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end
def create
@milestone = @project.milestones.new(params[:milestone])
@milestone = Milestones::CreateService.new(project, current_user, params[:milestone]).execute
if @milestone.save
redirect_to project_milestone_path(@project, @milestone)
......@@ -47,7 +47,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end
def update
@milestone.update_attributes(params[:milestone])
@milestone = Milestones::UpdateService.new(project, current_user, params[:milestone]).execute(milestone)
respond_to do |format|
format.js
......
class MilestoneObserver < BaseObserver
def after_create(milestone)
event_service.open_milestone(milestone, current_user)
end
def after_close(milestone, transition)
event_service.close_milestone(milestone, current_user)
end
def after_reopen(milestone, transition)
event_service.reopen_milestone(milestone, current_user)
end
end
module Milestones
class BaseService < ::BaseService
end
end
module Milestones
class CloseService < Milestones::BaseService
def execute(milestone)
if milestone.close
event_service.close_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class CreateService < Milestones::BaseService
def execute
milestone = project.milestones.new(params)
if milestone.save
event_service.open_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class ReopenService < Milestones::BaseService
def execute(milestone)
if milestone.reopen
event_service.reopen_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class UpdateService < Milestones::BaseService
def execute(milestone)
state = params.delete('state_event')
case state
when 'reopen'
Milestones::ReopenService.new(project, current_user, {}).execute(milestone)
when 'close'
Milestones::CloseService.new(project, current_user, {}).execute(milestone)
end
if params.present?
milestone.update_attributes(params)
end
milestone
end
end
end
......@@ -19,8 +19,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :milestone_observer,
:project_activity_cache_observer,
config.active_record.observers = :project_activity_cache_observer,
:note_observer,
:project_observer,
:system_hook_observer,
......
......@@ -40,17 +40,15 @@ module API
# Example Request:
# POST /projects/:id/milestones
post ":id/milestones" do
set_current_user_for_thread do
authorize! :admin_milestone, user_project
required_attributes! [:title]
authorize! :admin_milestone, user_project
required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :due_date]
milestone = ::Milestones::CreateService.new(user_project, current_user, attrs).execute
attrs = attributes_for_keys [:title, :description, :due_date]
@milestone = user_project.milestones.new attrs
if @milestone.save
present @milestone, with: Entities::Milestone
else
not_found!
end
if milestone.valid?
present milestone, with: Entities::Milestone
else
not_found!
end
end
......@@ -66,16 +64,15 @@ module API
# Example Request:
# PUT /projects/:id/milestones/:milestone_id
put ":id/milestones/:milestone_id" do
set_current_user_for_thread do
authorize! :admin_milestone, user_project
authorize! :admin_milestone, user_project
attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
milestone = user_project.milestones.find(params[:milestone_id])
milestone = ::Milestones::UpdateService.new(user_project, current_user, attrs).execute(milestone)
@milestone = user_project.milestones.find(params[:milestone_id])
attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
if @milestone.update_attributes attrs
present @milestone, with: Entities::Milestone
else
not_found!
end
if milestone.valid?
present milestone, with: Entities::Milestone
else
not_found!
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