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 ...@@ -37,7 +37,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end end
def create def create
@milestone = @project.milestones.new(params[:milestone]) @milestone = Milestones::CreateService.new(project, current_user, params[:milestone]).execute
if @milestone.save if @milestone.save
redirect_to project_milestone_path(@project, @milestone) redirect_to project_milestone_path(@project, @milestone)
...@@ -47,7 +47,7 @@ class Projects::MilestonesController < Projects::ApplicationController ...@@ -47,7 +47,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end end
def update def update
@milestone.update_attributes(params[:milestone]) @milestone = Milestones::UpdateService.new(project, current_user, params[:milestone]).execute(milestone)
respond_to do |format| respond_to do |format|
format.js 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 ...@@ -19,8 +19,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running. # Activate observers that should always be running.
config.active_record.observers = :milestone_observer, config.active_record.observers = :project_activity_cache_observer,
:project_activity_cache_observer,
:note_observer, :note_observer,
:project_observer, :project_observer,
:system_hook_observer, :system_hook_observer,
......
...@@ -40,17 +40,15 @@ module API ...@@ -40,17 +40,15 @@ module API
# Example Request: # Example Request:
# POST /projects/:id/milestones # POST /projects/:id/milestones
post ":id/milestones" do post ":id/milestones" do
set_current_user_for_thread do authorize! :admin_milestone, user_project
authorize! :admin_milestone, user_project required_attributes! [:title]
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] if milestone.valid?
@milestone = user_project.milestones.new attrs present milestone, with: Entities::Milestone
if @milestone.save else
present @milestone, with: Entities::Milestone not_found!
else
not_found!
end
end end
end end
...@@ -66,16 +64,15 @@ module API ...@@ -66,16 +64,15 @@ module API
# Example Request: # Example Request:
# PUT /projects/:id/milestones/:milestone_id # PUT /projects/:id/milestones/:milestone_id
put ":id/milestones/:milestone_id" do 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]) if milestone.valid?
attrs = attributes_for_keys [:title, :description, :due_date, :state_event] present milestone, with: Entities::Milestone
if @milestone.update_attributes attrs else
present @milestone, with: Entities::Milestone not_found!
else
not_found!
end
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