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 | 1 | module Projects |
| 2 | 2 | class CreateContext < BaseContext |
| 3 | + def initialize(user, params) | |
| 4 | + @current_user, @params = user, params.dup | |
| 5 | + end | |
| 6 | + | |
| 3 | 7 | def execute |
| 4 | 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 | 13 | # Parametrize path for project |
| 10 | 14 | # |
| ... | ... | @@ -25,7 +29,7 @@ module Projects |
| 25 | 29 | end |
| 26 | 30 | else |
| 27 | 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 | 33 | end |
| 30 | 34 | |
| 31 | 35 | Project.transaction do | ... | ... |
app/controllers/projects_controller.rb
| ... | ... | @@ -19,7 +19,7 @@ class ProjectsController < ProjectResourceController |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | 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 | 24 | respond_to do |format| |
| 25 | 25 | flash[:notice] = 'Project was successfully created.' if @project.saved? | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -43,7 +43,7 @@ module Gitlab |
| 43 | 43 | :wall_enabled, |
| 44 | 44 | :merge_requests_enabled, |
| 45 | 45 | :wiki_enabled] |
| 46 | - @project = Projects::CreateContext.new(nil, attrs, current_user).execute | |
| 46 | + @project = ::Projects::CreateContext.new(current_user, attrs).execute | |
| 47 | 47 | if @project.saved? |
| 48 | 48 | present @project, with: Entities::Project |
| 49 | 49 | else | ... | ... |
| ... | ... | @@ -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 | 153 | end |
| 154 | 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 | 157 | describe :find_with_namespace do |
| 188 | 158 | context 'with namespace' do | ... | ... |