Commit 6f05ea4f6e26e76edc0a6ad5d2eb4e5db676aea0
1 parent
232d61d5
Exists in
master
and in
4 other branches
Improve CreateContext call. Fixed test
Showing
5 changed files
with
47 additions
and
35 deletions
Show diff stats
app/contexts/projects/create_context.rb
| 1 | module Projects | 1 | module Projects |
| 2 | class CreateContext < BaseContext | 2 | class CreateContext < BaseContext |
| 3 | + def initialize(user, params) | ||
| 4 | + @current_user, @params = user, params.dup | ||
| 5 | + end | ||
| 6 | + | ||
| 3 | def execute | 7 | def execute |
| 4 | # get namespace id | 8 | # get namespace id |
| 5 | - namespace_id = params[:project].delete(:namespace_id) | 9 | + namespace_id = params.delete(:namespace_id) |
| 6 | 10 | ||
| 7 | - @project = Project.new(params[:project]) | 11 | + @project = Project.new(params) |
| 8 | 12 | ||
| 9 | # Parametrize path for project | 13 | # Parametrize path for project |
| 10 | # | 14 | # |
| @@ -25,7 +29,7 @@ module Projects | @@ -25,7 +29,7 @@ module Projects | ||
| 25 | end | 29 | end |
| 26 | else | 30 | else |
| 27 | # Set current user namespace if namespace_id is nil | 31 | # Set current user namespace if namespace_id is nil |
| 28 | - @project.namespace_id = current_user.id | 32 | + @project.namespace_id = current_user.namespace_id |
| 29 | end | 33 | end |
| 30 | 34 | ||
| 31 | Project.transaction do | 35 | Project.transaction do |
app/controllers/projects_controller.rb
| @@ -19,7 +19,7 @@ class ProjectsController < ProjectResourceController | @@ -19,7 +19,7 @@ class ProjectsController < ProjectResourceController | ||
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def create | 21 | def create |
| 22 | - @project = Projects::CreateContext.new(nil, current_user, params).execute | 22 | + @project = Projects::CreateContext.new(current_user, params).execute |
| 23 | 23 | ||
| 24 | respond_to do |format| | 24 | respond_to do |format| |
| 25 | flash[:notice] = 'Project was successfully created.' if @project.saved? | 25 | flash[:notice] = 'Project was successfully created.' if @project.saved? |
lib/api/projects.rb
| @@ -43,7 +43,7 @@ module Gitlab | @@ -43,7 +43,7 @@ module Gitlab | ||
| 43 | :wall_enabled, | 43 | :wall_enabled, |
| 44 | :merge_requests_enabled, | 44 | :merge_requests_enabled, |
| 45 | :wiki_enabled] | 45 | :wiki_enabled] |
| 46 | - @project = Projects::CreateContext.new(nil, attrs, current_user).execute | 46 | + @project = ::Projects::CreateContext.new(current_user, attrs).execute |
| 47 | if @project.saved? | 47 | if @project.saved? |
| 48 | present @project, with: Entities::Project | 48 | present @project, with: Entities::Project |
| 49 | else | 49 | else |
| @@ -0,0 +1,38 @@ | @@ -0,0 +1,38 @@ | ||
| 1 | +require 'spec_helper' | ||
| 2 | + | ||
| 3 | +describe Projects::CreateContext do | ||
| 4 | + describe :create_by_user do | ||
| 5 | + before do | ||
| 6 | + @user = create :user | ||
| 7 | + @opts = { | ||
| 8 | + name: "GitLab" | ||
| 9 | + } | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + context 'user namespace' do | ||
| 13 | + before do | ||
| 14 | + @project = create_project(@user, @opts) | ||
| 15 | + end | ||
| 16 | + | ||
| 17 | + it { @project.should be_valid } | ||
| 18 | + it { @project.owner.should == @user } | ||
| 19 | + it { @project.namespace.should == @user.namespace } | ||
| 20 | + end | ||
| 21 | + | ||
| 22 | + context 'group namespace' do | ||
| 23 | + before do | ||
| 24 | + @group = create :group, owner: @user | ||
| 25 | + @opts.merge!(namespace_id: @group.id) | ||
| 26 | + @project = create_project(@user, @opts) | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + it { @project.should be_valid } | ||
| 30 | + it { @project.owner.should == @user } | ||
| 31 | + it { @project.namespace.should == @group } | ||
| 32 | + end | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + def create_project(user, opts) | ||
| 36 | + Projects::CreateContext.new(user, opts).execute | ||
| 37 | + end | ||
| 38 | +end |
spec/models/project_spec.rb
| @@ -153,36 +153,6 @@ describe Project do | @@ -153,36 +153,6 @@ describe Project do | ||
| 153 | end | 153 | end |
| 154 | end | 154 | end |
| 155 | 155 | ||
| 156 | - describe :create_by_user do | ||
| 157 | - before do | ||
| 158 | - @user = create :user | ||
| 159 | - @opts = { | ||
| 160 | - name: "GitLab" | ||
| 161 | - } | ||
| 162 | - end | ||
| 163 | - | ||
| 164 | - context 'user namespace' do | ||
| 165 | - before do | ||
| 166 | - @project = Project.create_by_user(@opts, @user) | ||
| 167 | - end | ||
| 168 | - | ||
| 169 | - it { @project.should be_valid } | ||
| 170 | - it { @project.owner.should == @user } | ||
| 171 | - it { @project.namespace.should == @user.namespace } | ||
| 172 | - end | ||
| 173 | - | ||
| 174 | - context 'user namespace' do | ||
| 175 | - before do | ||
| 176 | - @group = create :group, owner: @user | ||
| 177 | - @opts.merge!(namespace_id: @group.id) | ||
| 178 | - @project = Project.create_by_user(@opts, @user) | ||
| 179 | - end | ||
| 180 | - | ||
| 181 | - it { @project.should be_valid } | ||
| 182 | - it { @project.owner.should == @user } | ||
| 183 | - it { @project.namespace.should == @group } | ||
| 184 | - end | ||
| 185 | - end | ||
| 186 | 156 | ||
| 187 | describe :find_with_namespace do | 157 | describe :find_with_namespace do |
| 188 | context 'with namespace' do | 158 | context 'with namespace' do |