Move update issue code to separate service

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2014-04-02 13:54:41 +03:00
parent cfd9fd30d6
commit c4e81ed9de
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
8 changed files with 74 additions and 28 deletions

View file

@ -74,8 +74,7 @@ class Projects::IssuesController < Projects::ApplicationController
end
def update
@issue.update_attributes(params[:issue])
@issue.reset_events_cache
@issue = Issues::UpdateService.new(project, current_user, params[:issue]).execute(issue)
respond_to do |format|
format.js

View file

@ -12,16 +12,6 @@ class IssueObserver < BaseObserver
execute_hooks(issue)
end
def after_update(issue)
if issue.is_being_reassigned?
notification.reassigned_issue(issue, current_user)
create_assignee_note(issue)
end
issue.notice_added_references(issue.project, current_user)
execute_hooks(issue)
end
protected
# Create issue note with service comment like 'Status changed to closed'

View file

@ -0,0 +1,19 @@
module Issues
class BaseService < ::BaseService
private
# Create issue note with service comment like 'Status changed to closed'
def create_note(issue)
Note.create_status_change_note(issue, issue.project, current_user, issue.state, current_commit)
end
def create_assignee_note(issue)
Note.create_assignee_change_note(issue, issue.project, current_user, issue.assignee)
end
def execute_hooks(issue)
issue.project.execute_hooks(issue.to_hook_data, :issue_hooks)
end
end
end

View file

@ -13,11 +13,5 @@ module Issues
issue
end
private
def execute_hooks(issue)
issue.project.execute_hooks(issue.to_hook_data, :issue_hooks)
end
end
end

View file

@ -0,0 +1,19 @@
module Issues
class UpdateService < BaseService
def execute(issue)
if issue.update_attributes(params)
issue.reset_events_cache
if issue.is_being_reassigned?
notification.reassigned_issue(issue, current_user)
create_assignee_note(issue)
end
issue.notice_added_references(issue.project, current_user)
execute_hooks(issue)
end
issue
end
end
end

View file

@ -74,18 +74,18 @@ module API
# Example Request:
# PUT /projects/:id/issues/:issue_id
put ":id/issues/:issue_id" do
set_current_user_for_thread do
@issue = user_project.issues.find(params[:issue_id])
authorize! :modify_issue, @issue
issue = user_project.issues.find(params[:issue_id])
authorize! :modify_issue, issue
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event]
attrs[:label_list] = params[:labels] if params[:labels].present?
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event]
attrs[:label_list] = params[:labels] if params[:labels].present?
if @issue.update_attributes attrs
present @issue, with: Entities::Issue
else
not_found!
end
issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue)
if issue.valid?
present issue, with: Entities::Issue
else
not_found!
end
end

View file

@ -17,6 +17,7 @@ describe Issues::CreateService do
end
it { @issue.should be_valid }
it { @issue.title.should == 'Awesome issue' }
end
end
end

View file

@ -0,0 +1,24 @@
require 'spec_helper'
describe Issues::UpdateService do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
let(:issue) { create(:issue) }
describe :execute do
context "valid params" do
before do
project.team << [user, :master]
opts = {
title: 'New title',
description: 'Also please fix'
}
@issue = Issues::UpdateService.new(project, user, opts).execute(issue)
end
it { @issue.should be_valid }
it { @issue.title.should == 'New title' }
end
end
end