Commit f0d2d281 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'refactoring/context_into_services' into 'master'

Refactoring context Into services

To prevent confusion where to write logic. Context or Service?
parents 7527408a 4d82e9ed
...@@ -19,7 +19,7 @@ class Admin::ProjectsController < Admin::ApplicationController ...@@ -19,7 +19,7 @@ class Admin::ProjectsController < Admin::ApplicationController
end end
def transfer def transfer
result = ::Projects::TransferContext.new(@project, current_user, project: params).execute(:admin) result = ::Projects::TransferService.new(@project, current_user, project: params).execute(:admin)
if result if result
redirect_to [:admin, @project] redirect_to [:admin, @project]
......
...@@ -13,7 +13,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -13,7 +13,7 @@ class Projects::BlobController < Projects::ApplicationController
end end
def destroy def destroy
result = Files::DeleteContext.new(@project, current_user, params, @ref, @path).execute result = Files::DeleteService.new(@project, current_user, params, @ref, @path).execute
if result[:status] == :success if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed" flash[:notice] = "Your changes have been successfully committed"
......
...@@ -7,7 +7,7 @@ class Projects::EditTreeController < Projects::BaseTreeController ...@@ -7,7 +7,7 @@ class Projects::EditTreeController < Projects::BaseTreeController
end end
def update def update
result = Files::UpdateContext.new(@project, current_user, params, @ref, @path).execute result = Files::UpdateService.new(@project, current_user, params, @ref, @path).execute
if result[:status] == :success if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed" flash[:notice] = "Your changes have been successfully committed"
......
...@@ -89,7 +89,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -89,7 +89,7 @@ class Projects::IssuesController < Projects::ApplicationController
end end
def bulk_update def bulk_update
result = Issues::BulkUpdateContext.new(project, current_user, params).execute result = Issues::BulkUpdateService.new(project, current_user, params).execute
redirect_to :back, notice: "#{result[:count]} issues updated" redirect_to :back, notice: "#{result[:count]} issues updated"
end end
......
...@@ -6,7 +6,7 @@ class Projects::NewTreeController < Projects::BaseTreeController ...@@ -6,7 +6,7 @@ class Projects::NewTreeController < Projects::BaseTreeController
def update def update
file_path = File.join(@path, File.basename(params[:file_name])) file_path = File.join(@path, File.basename(params[:file_name]))
result = Files::CreateContext.new(@project, current_user, params, @ref, file_path).execute result = Files::CreateService.new(@project, current_user, params, @ref, file_path).execute
if result[:status] == :success if result[:status] == :success
flash[:notice] = "Your changes have been successfully committed" flash[:notice] = "Your changes have been successfully committed"
......
...@@ -5,7 +5,7 @@ class Projects::NotesController < Projects::ApplicationController ...@@ -5,7 +5,7 @@ class Projects::NotesController < Projects::ApplicationController
before_filter :authorize_admin_note!, only: [:update, :destroy] before_filter :authorize_admin_note!, only: [:update, :destroy]
def index def index
@notes = Notes::LoadContext.new(project, current_user, params).execute @notes = Notes::LoadService.new(project, current_user, params).execute
notes_json = { notes: [] } notes_json = { notes: [] }
...@@ -20,7 +20,7 @@ class Projects::NotesController < Projects::ApplicationController ...@@ -20,7 +20,7 @@ class Projects::NotesController < Projects::ApplicationController
end end
def create def create
@note = Notes::CreateContext.new(project, current_user, params).execute @note = Notes::CreateService.new(project, current_user, params).execute
respond_to do |format| respond_to do |format|
format.json { render_note_json(@note) } format.json { render_note_json(@note) }
......
...@@ -20,7 +20,7 @@ class ProjectsController < ApplicationController ...@@ -20,7 +20,7 @@ class ProjectsController < ApplicationController
end end
def create def create
@project = ::Projects::CreateContext.new(current_user, params[:project]).execute @project = ::Projects::CreateService.new(current_user, params[:project]).execute
respond_to do |format| respond_to do |format|
flash[:notice] = 'Project was successfully created.' if @project.saved? flash[:notice] = 'Project was successfully created.' if @project.saved?
...@@ -36,7 +36,7 @@ class ProjectsController < ApplicationController ...@@ -36,7 +36,7 @@ class ProjectsController < ApplicationController
end end
def update def update
status = ::Projects::UpdateContext.new(@project, current_user, params).execute status = ::Projects::UpdateService.new(@project, current_user, params).execute
respond_to do |format| respond_to do |format|
if status if status
...@@ -51,7 +51,7 @@ class ProjectsController < ApplicationController ...@@ -51,7 +51,7 @@ class ProjectsController < ApplicationController
end end
def transfer def transfer
::Projects::TransferContext.new(project, current_user, params).execute ::Projects::TransferService.new(project, current_user, params).execute
end end
def show def show
...@@ -89,7 +89,7 @@ class ProjectsController < ApplicationController ...@@ -89,7 +89,7 @@ class ProjectsController < ApplicationController
end end
def fork def fork
@forked_project = ::Projects::ForkContext.new(project, current_user).execute @forked_project = ::Projects::ForkService.new(project, current_user).execute
respond_to do |format| respond_to do |format|
format.html do format.html do
......
...@@ -5,9 +5,9 @@ class SearchController < ApplicationController ...@@ -5,9 +5,9 @@ class SearchController < ApplicationController
if @project if @project
return access_denied! unless can?(current_user, :download_code, @project) return access_denied! unless can?(current_user, :download_code, @project)
@search_results = Search::ProjectContext.new(@project, current_user, params).execute @search_results = Search::ProjectService.new(@project, current_user, params).execute
else else
@search_results = Search::GlobalContext.new(current_user, params).execute @search_results = Search::GlobalService.new(current_user, params).execute
end end
end end
end end
class BaseContext class BaseService
attr_accessor :project, :current_user, :params attr_accessor :project, :current_user, :params
def initialize(project, user, params) def initialize(project, user, params)
......
module Files module Files
class BaseContext < ::BaseContext class BaseService < ::BaseService
attr_reader :ref, :path attr_reader :ref, :path
def initialize(project, user, params, ref, path = nil) def initialize(project, user, params, ref, path = nil)
......
require_relative "base_context" require_relative "base_service"
module Files module Files
class CreateContext < BaseContext class CreateService < BaseService
def execute def execute
allowed = if project.protected_branch?(ref) allowed = if project.protected_branch?(ref)
can?(current_user, :push_code_to_protected_branches, project) can?(current_user, :push_code_to_protected_branches, project)
......
require_relative "base_context" require_relative "base_service"
module Files module Files
class DeleteContext < BaseContext class DeleteService < BaseService
def execute def execute
allowed = if project.protected_branch?(ref) allowed = if project.protected_branch?(ref)
can?(current_user, :push_code_to_protected_branches, project) can?(current_user, :push_code_to_protected_branches, project)
......
require_relative "base_context" require_relative "base_service"
module Files module Files
class UpdateContext < BaseContext class UpdateService < BaseService
def execute def execute
allowed = if project.protected_branch?(ref) allowed = if project.protected_branch?(ref)
can?(current_user, :push_code_to_protected_branches, project) can?(current_user, :push_code_to_protected_branches, project)
......
module Issues module Issues
class BulkUpdateContext < BaseContext class BulkUpdateService < BaseService
def execute def execute
update_data = params[:update] update_data = params[:update]
......
module Notes module Notes
class CreateContext < BaseContext class CreateService < BaseService
def execute def execute
note = project.notes.new(params[:note]) note = project.notes.new(params[:note])
note.author = current_user note.author = current_user
......
module Notes module Notes
class LoadContext < BaseContext class LoadService < BaseService
def execute def execute
target_type = params[:target_type] target_type = params[:target_type]
target_id = params[:target_id] target_id = params[:target_id]
......
module Projects module Projects
class CreateContext < BaseContext class CreateService < BaseService
def initialize(user, params) def initialize(user, params)
@current_user, @params = user, params.dup @current_user, @params = user, params.dup
end end
......
module Projects module Projects
class ForkContext < BaseContext class ForkService < BaseService
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
def initialize(project, user) def initialize(project, user)
......
module Projects module Projects
class TransferContext < BaseContext class TransferService < BaseService
def execute(role = :default) def execute(role = :default)
namespace_id = params[:project].delete(:namespace_id) namespace_id = params[:project].delete(:namespace_id)
allowed_transfer = can?(current_user, :change_namespace, project) || role == :admin allowed_transfer = can?(current_user, :change_namespace, project) || role == :admin
......
module Projects module Projects
class UpdateContext < BaseContext class UpdateService < BaseService
def execute(role = :default) def execute(role = :default)
params[:project].delete(:namespace_id) params[:project].delete(:namespace_id)
# check that user is allowed to set specified visibility_level # check that user is allowed to set specified visibility_level
......
module Search module Search
class GlobalContext class GlobalService
attr_accessor :current_user, :params attr_accessor :current_user, :params
def initialize(user, params) def initialize(user, params)
......
module Search module Search
class ProjectContext class ProjectService
attr_accessor :project, :current_user, :params attr_accessor :project, :current_user, :params
def initialize(project, user, params) def initialize(project, user, params)
......
...@@ -21,7 +21,7 @@ module API ...@@ -21,7 +21,7 @@ module API
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
branch_name = attrs.delete(:branch_name) branch_name = attrs.delete(:branch_name)
file_path = attrs.delete(:file_path) file_path = attrs.delete(:file_path)
result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute result = ::Files::CreateService.new(user_project, current_user, attrs, branch_name, file_path).execute
if result[:status] == :success if result[:status] == :success
status(201) status(201)
...@@ -51,7 +51,7 @@ module API ...@@ -51,7 +51,7 @@ module API
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding] attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
branch_name = attrs.delete(:branch_name) branch_name = attrs.delete(:branch_name)
file_path = attrs.delete(:file_path) file_path = attrs.delete(:file_path)
result = ::Files::UpdateContext.new(user_project, current_user, attrs, branch_name, file_path).execute result = ::Files::UpdateService.new(user_project, current_user, attrs, branch_name, file_path).execute
if result[:status] == :success if result[:status] == :success
status(200) status(200)
...@@ -81,7 +81,7 @@ module API ...@@ -81,7 +81,7 @@ module API
attrs = attributes_for_keys [:file_path, :branch_name, :commit_message] attrs = attributes_for_keys [:file_path, :branch_name, :commit_message]
branch_name = attrs.delete(:branch_name) branch_name = attrs.delete(:branch_name)
file_path = attrs.delete(:file_path) file_path = attrs.delete(:file_path)
result = ::Files::DeleteContext.new(user_project, current_user, attrs, branch_name, file_path).execute result = ::Files::DeleteService.new(user_project, current_user, attrs, branch_name, file_path).execute
if result[:status] == :success if result[:status] == :success
status(200) status(200)
......
...@@ -102,7 +102,7 @@ module API ...@@ -102,7 +102,7 @@ module API
:visibility_level, :visibility_level,
:import_url] :import_url]
attrs = map_public_to_visibility_level(attrs) attrs = map_public_to_visibility_level(attrs)
@project = ::Projects::CreateContext.new(current_user, attrs).execute @project = ::Projects::CreateService.new(current_user, attrs).execute
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
else else
...@@ -143,7 +143,7 @@ module API ...@@ -143,7 +143,7 @@ module API
:public, :public,
:visibility_level] :visibility_level]
attrs = map_public_to_visibility_level(attrs) attrs = map_public_to_visibility_level(attrs)
@project = ::Projects::CreateContext.new(user, attrs).execute @project = ::Projects::CreateService.new(user, attrs).execute
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
else else
......
...@@ -66,7 +66,7 @@ namespace :gitlab do ...@@ -66,7 +66,7 @@ namespace :gitlab do
project_params[:namespace_id] = group.id project_params[:namespace_id] = group.id
end end
project = Projects::CreateContext.new(user, project_params).execute project = Projects::CreateService.new(user, project_params).execute
if project.valid? if project.valid?
puts " * Created #{project.name} (#{repo_path})".green puts " * Created #{project.name} (#{repo_path})".green
......
...@@ -58,7 +58,7 @@ describe :forked_from_project do ...@@ -58,7 +58,7 @@ describe :forked_from_project do
end end
def fork_project(from_project, user) def fork_project(from_project, user)
context = Projects::ForkContext.new(from_project, user) context = Projects::ForkService.new(from_project, user)
shell = double("gitlab_shell") shell = double("gitlab_shell")
shell.stub(fork_repository: true) shell.stub(fork_repository: true)
context.stub(gitlab_shell: shell) context.stub(gitlab_shell: shell)
......
require 'spec_helper' require 'spec_helper'
describe Projects::ForkContext do describe Projects::ForkService do
describe :fork_by_user do describe :fork_by_user do
before do before do
@from_namespace = create(:namespace) @from_namespace = create(:namespace)
...@@ -47,7 +47,7 @@ describe Projects::ForkContext do ...@@ -47,7 +47,7 @@ describe Projects::ForkContext do
end end
def fork_project(from_project, user, fork_success = true) def fork_project(from_project, user, fork_success = true)
context = Projects::ForkContext.new(from_project, user) context = Projects::ForkService.new(from_project, user)
shell = double("gitlab_shell") shell = double("gitlab_shell")
shell.stub(fork_repository: fork_success) shell.stub(fork_repository: fork_success)
context.stub(gitlab_shell: shell) context.stub(gitlab_shell: shell)
......
require 'spec_helper' require 'spec_helper'
describe Issues::BulkUpdateContext do describe Issues::BulkUpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
...@@ -14,7 +14,7 @@ describe Issues::BulkUpdateContext do ...@@ -14,7 +14,7 @@ describe Issues::BulkUpdateContext do
name: "GitLab", name: "GitLab",
namespace: @user.namespace namespace: @user.namespace
} }
@project = Projects::CreateContext.new(@user, opts).execute @project = Projects::CreateService.new(@user, opts).execute
end end
describe :close_issue do describe :close_issue do
...@@ -32,7 +32,7 @@ describe Issues::BulkUpdateContext do ...@@ -32,7 +32,7 @@ describe Issues::BulkUpdateContext do
end end
it { it {
result = Issues::BulkUpdateContext.new(@project, @user, @params).execute result = Issues::BulkUpdateService.new(@project, @user, @params).execute
result[:success].should be_true result[:success].should be_true
result[:count].should == @issues.count result[:count].should == @issues.count
...@@ -57,7 +57,7 @@ describe Issues::BulkUpdateContext do ...@@ -57,7 +57,7 @@ describe Issues::BulkUpdateContext do
end end
it { it {
result = Issues::BulkUpdateContext.new(@project, @user, @params).execute result = Issues::BulkUpdateService.new(@project, @user, @params).execute
result[:success].should be_true result[:success].should be_true
result[:count].should == @issues.count result[:count].should == @issues.count
...@@ -80,7 +80,7 @@ describe Issues::BulkUpdateContext do ...@@ -80,7 +80,7 @@ describe Issues::BulkUpdateContext do
end end
it { it {
result = Issues::BulkUpdateContext.new(@project, @user, @params).execute result = Issues::BulkUpdateService.new(@project, @user, @params).execute
result[:success].should be_true result[:success].should be_true
result[:count].should == 1 result[:count].should == 1
...@@ -102,7 +102,7 @@ describe Issues::BulkUpdateContext do ...@@ -102,7 +102,7 @@ describe Issues::BulkUpdateContext do
end end
it { it {
result = Issues::BulkUpdateContext.new(@project, @user, @params).execute result = Issues::BulkUpdateService.new(@project, @user, @params).execute
result[:success].should be_true result[:success].should be_true
result[:count].should == 1 result[:count].should == 1
......
require 'spec_helper' require 'spec_helper'
describe Projects::CreateContext do describe Projects::CreateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
...@@ -136,7 +136,7 @@ describe Projects::CreateContext do ...@@ -136,7 +136,7 @@ describe Projects::CreateContext do
end end
def create_project(user, opts) def create_project(user, opts)
Projects::CreateContext.new(user, opts).execute Projects::CreateService.new(user, opts).execute
end end
end end
require 'spec_helper' require 'spec_helper'
describe Projects::UpdateContext do describe Projects::UpdateService do
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
...@@ -106,6 +106,6 @@ describe Projects::UpdateContext do ...@@ -106,6 +106,6 @@ describe Projects::UpdateContext do
end end
def update_project(project, user, opts) def update_project(project, user, opts)
Projects::UpdateContext.new(project, user, opts).execute Projects::UpdateService.new(project, user, opts).execute
end end
end end
\ No newline at end of file
require 'spec_helper' require 'spec_helper'
describe 'Search::GlobalContext' do describe 'Search::GlobalService' do
let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') }
let(:user) { create(:user, namespace: found_namespace) } let(:user) { create(:user, namespace: found_namespace) }
let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) }
...@@ -19,7 +19,7 @@ describe 'Search::GlobalContext' do ...@@ -19,7 +19,7 @@ describe 'Search::GlobalContext' do
describe '#execute' do describe '#execute' do
context 'unauthenticated' do context 'unauthenticated' do
it 'should return public projects only' do it 'should return public projects only' do
context = Search::GlobalContext.new(nil, search: "searchable") context = Search::GlobalService.new(nil, search: "searchable")
results = context.execute results = context.execute
results[:projects].should have(1).items results[:projects].should have(1).items
results[:projects].should include(public_project) results[:projects].should include(public_project)
...@@ -28,7 +28,7 @@ describe 'Search::GlobalContext' do ...@@ -28,7 +28,7 @@ describe 'Search::GlobalContext' do
context 'authenticated' do context 'authenticated' do
it 'should return public, internal and private projects' do it 'should return public, internal and private projects' do
context = Search::GlobalContext.new(user, search: "searchable") context = Search::GlobalService.new(user, search: "searchable")
results = context.execute results = context.execute
results[:projects].should have(3).items results[:projects].should have(3).items
results[:projects].should include(public_project) results[:projects].should include(public_project)
...@@ -37,7 +37,7 @@ describe 'Search::GlobalContext' do ...@@ -37,7 +37,7 @@ describe 'Search::GlobalContext' do
end end
it 'should return only public & internal projects' do it 'should return only public & internal projects' do
context = Search::GlobalContext.new(internal_user, search: "searchable") context = Search::GlobalService.new(internal_user, search: "searchable")
results = context.execute results = context.execute
results[:projects].should have(2).items results[:projects].should have(2).items
results[:projects].should include(internal_project) results[:projects].should include(internal_project)
...@@ -45,7 +45,7 @@ describe 'Search::GlobalContext' do ...@@ -45,7 +45,7 @@ describe 'Search::GlobalContext' do
end end
it 'namespace name should be searchable' do it 'namespace name should be searchable' do
context = Search::GlobalContext.new(user, search: "searchable namespace") context = Search::GlobalService.new(user, search: "searchable namespace")
results = context.execute results = context.execute
results[:projects].should == [found_project] results[:projects].should == [found_project]
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