Commit 5926bbac12d5831e1ad90964272b96e152a72e34

Authored by Dmitriy Zaporozhets
1 parent 69e41250

Backend Refactoring

app/contexts/notes/create_context.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +module Notes
  2 + class CreateContext < BaseContext
  3 + def execute
  4 + note = project.notes.new(params[:note])
  5 + note.author = current_user
  6 + note.notify = true if params[:notify] == '1'
  7 + note.notify_author = true if params[:notify_author] == '1'
  8 + note.save
  9 + note
  10 + end
  11 + end
  12 +end
app/contexts/notes/load_context.rb 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +module Notes
  2 + class LoadContext < BaseContext
  3 + def execute
  4 + target_type = params[:target_type]
  5 + target_id = params[:target_id]
  6 + first_id = params[:first_id]
  7 + last_id = params[:last_id]
  8 +
  9 +
  10 + @notes = case target_type
  11 + when "commit"
  12 + then project.commit_notes(project.commit(target_id)).fresh.limit(20)
  13 + when "snippet"
  14 + then project.snippets.find(target_id).notes
  15 + when "wall"
  16 + then project.common_notes.order("created_at DESC").fresh.limit(50)
  17 + when "issue"
  18 + then project.issues.find(target_id).notes.inc_author.order("created_at DESC").limit(20)
  19 + when "merge_request"
  20 + then project.merge_requests.find(target_id).notes.inc_author.order("created_at DESC").limit(20)
  21 + when "wiki"
  22 + then project.wikis.reverse.map {|w| w.notes.fresh }.flatten[0..20]
  23 + end
  24 +
  25 + @notes = if last_id
  26 + @notes.where("id > ?", last_id)
  27 + elsif first_id
  28 + @notes.where("id < ?", first_id)
  29 + else
  30 + @notes
  31 + end
  32 + end
  33 + end
  34 +end
app/contexts/notes_load.rb
@@ -1,32 +0,0 @@ @@ -1,32 +0,0 @@
1 -class NotesLoad < BaseContext  
2 - def execute  
3 - target_type = params[:target_type]  
4 - target_id = params[:target_id]  
5 - first_id = params[:first_id]  
6 - last_id = params[:last_id]  
7 -  
8 -  
9 - @notes = case target_type  
10 - when "commit"  
11 - then project.commit_notes(project.commit(target_id)).fresh.limit(20)  
12 - when "snippet"  
13 - then project.snippets.find(target_id).notes  
14 - when "wall"  
15 - then project.common_notes.order("created_at DESC").fresh.limit(50)  
16 - when "issue"  
17 - then project.issues.find(target_id).notes.inc_author.order("created_at DESC").limit(20)  
18 - when "merge_request"  
19 - then project.merge_requests.find(target_id).notes.inc_author.order("created_at DESC").limit(20)  
20 - when "wiki"  
21 - then project.wikis.reverse.map {|w| w.notes.fresh }.flatten[0..20]  
22 - end  
23 -  
24 - @notes = if last_id  
25 - @notes.where("id > ?", last_id)  
26 - elsif first_id  
27 - @notes.where("id < ?", first_id)  
28 - else  
29 - @notes  
30 - end  
31 - end  
32 -end  
app/contexts/test_hook_context.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +class TestHookContext < BaseContext
  2 + def execute
  3 + hook = project.hooks.find(params[:id])
  4 + commits = project.commits(project.default_branch, nil, 3)
  5 + data = project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", current_user)
  6 + hook.execute(data)
  7 + end
  8 +end
app/controllers/admin/projects_controller.rb
@@ -2,6 +2,7 @@ class Admin::ProjectsController &lt; ApplicationController @@ -2,6 +2,7 @@ class Admin::ProjectsController &lt; ApplicationController
2 layout "admin" 2 layout "admin"
3 before_filter :authenticate_user! 3 before_filter :authenticate_user!
4 before_filter :authenticate_admin! 4 before_filter :authenticate_admin!
  5 + before_filter :admin_project, :only => [:edit, :show, :update, :destroy, :team_update]
5 6
6 def index 7 def index
7 @admin_projects = Project.scoped 8 @admin_projects = Project.scoped
@@ -10,13 +11,9 @@ class Admin::ProjectsController &lt; ApplicationController @@ -10,13 +11,9 @@ class Admin::ProjectsController &lt; ApplicationController
10 end 11 end
11 12
12 def show 13 def show
13 - @admin_project = Project.find_by_code(params[:id])  
14 -  
15 - @users = if @admin_project.users.empty?  
16 - User  
17 - else  
18 - User.not_in_project(@admin_project)  
19 - end.all 14 + @users = User.scoped
  15 + @users = @users.not_in_project(@admin_project) if @admin_project.users.present?
  16 + @users = @users.all
20 end 17 end
21 18
22 def new 19 def new
@@ -24,19 +21,10 @@ class Admin::ProjectsController &lt; ApplicationController @@ -24,19 +21,10 @@ class Admin::ProjectsController &lt; ApplicationController
24 end 21 end
25 22
26 def edit 23 def edit
27 - @admin_project = Project.find_by_code(params[:id])  
28 end 24 end
29 25
30 def team_update 26 def team_update
31 - @admin_project = Project.find_by_code(params[:id])  
32 -  
33 - UsersProject.bulk_import(  
34 - @admin_project,  
35 - params[:user_ids],  
36 - params[:project_access]  
37 - )  
38 -  
39 - @admin_project.update_repository 27 + @admin_project.add_users_ids_to_team(params[:user_ids], params[:project_access])
40 28
41 redirect_to [:admin, @admin_project], notice: 'Project was successfully updated.' 29 redirect_to [:admin, @admin_project], notice: 'Project was successfully updated.'
42 end 30 end
@@ -53,8 +41,6 @@ class Admin::ProjectsController &lt; ApplicationController @@ -53,8 +41,6 @@ class Admin::ProjectsController &lt; ApplicationController
53 end 41 end
54 42
55 def update 43 def update
56 - @admin_project = Project.find_by_code(params[:id])  
57 -  
58 owner_id = params[:project].delete(:owner_id) 44 owner_id = params[:project].delete(:owner_id)
59 45
60 if owner_id 46 if owner_id
@@ -69,9 +55,14 @@ class Admin::ProjectsController &lt; ApplicationController @@ -69,9 +55,14 @@ class Admin::ProjectsController &lt; ApplicationController
69 end 55 end
70 56
71 def destroy 57 def destroy
72 - @admin_project = Project.find_by_code(params[:id])  
73 @admin_project.destroy 58 @admin_project.destroy
74 59
75 redirect_to admin_projects_url, notice: 'Project was successfully deleted.' 60 redirect_to admin_projects_url, notice: 'Project was successfully deleted.'
76 end 61 end
  62 +
  63 + private
  64 +
  65 + def admin_project
  66 + @admin_project = Project.find_by_code(params[:id])
  67 + end
77 end 68 end
app/controllers/hooks_controller.rb
@@ -28,10 +28,7 @@ class HooksController &lt; ApplicationController @@ -28,10 +28,7 @@ class HooksController &lt; ApplicationController
28 end 28 end
29 29
30 def test 30 def test
31 - @hook = @project.hooks.find(params[:id])  
32 - commits = @project.commits(@project.default_branch, nil, 3)  
33 - data = @project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{@project.default_branch}", current_user)  
34 - @hook.execute(data) 31 + TestHookContext.new(project, current_user, params).execute
35 32
36 redirect_to :back 33 redirect_to :back
37 end 34 end
app/controllers/notes_controller.rb
@@ -15,11 +15,7 @@ class NotesController &lt; ApplicationController @@ -15,11 +15,7 @@ class NotesController &lt; ApplicationController
15 end 15 end
16 16
17 def create 17 def create
18 - @note = @project.notes.new(params[:note])  
19 - @note.author = current_user  
20 - @note.notify = true if params[:notify] == '1'  
21 - @note.notify_author = true if params[:notify_author] == '1'  
22 - @note.save 18 + @note = Notes::CreateContext.new(project, current_user, params).execute
23 19
24 respond_to do |format| 20 respond_to do |format|
25 format.html {redirect_to :back} 21 format.html {redirect_to :back}
@@ -40,6 +36,6 @@ class NotesController &lt; ApplicationController @@ -40,6 +36,6 @@ class NotesController &lt; ApplicationController
40 protected 36 protected
41 37
42 def notes 38 def notes
43 - @notes = NotesLoad.new(project, current_user, params).execute 39 + @notes = Notes::LoadContext.new(project, current_user, params).execute
44 end 40 end
45 end 41 end
app/controllers/profile_controller.rb
1 class ProfileController < ApplicationController 1 class ProfileController < ApplicationController
2 layout "profile" 2 layout "profile"
  3 + before_filter :user
  4 +
3 def show 5 def show
4 - @user = current_user  
5 end 6 end
6 7
7 def design 8 def design
8 - @user = current_user  
9 end 9 end
10 10
11 def update 11 def update
12 - @user = current_user  
13 @user.update_attributes(params[:user]) 12 @user.update_attributes(params[:user])
14 redirect_to :back 13 redirect_to :back
15 end 14 end
16 15
17 def token 16 def token
18 - @user = current_user  
19 end 17 end
20 18
21 def password 19 def password
22 - @user = current_user  
23 end 20 end
24 21
25 def password_update 22 def password_update
26 params[:user].reject!{ |k, v| k != "password" && k != "password_confirmation"} 23 params[:user].reject!{ |k, v| k != "password" && k != "password_confirmation"}
27 - @user = current_user  
28 24
29 if @user.update_attributes(params[:user]) 25 if @user.update_attributes(params[:user])
30 flash[:notice] = "Password was successfully updated. Please login with it" 26 flash[:notice] = "Password was successfully updated. Please login with it"
@@ -38,4 +34,10 @@ class ProfileController &lt; ApplicationController @@ -38,4 +34,10 @@ class ProfileController &lt; ApplicationController
38 current_user.reset_authentication_token! 34 current_user.reset_authentication_token!
39 redirect_to profile_token_path 35 redirect_to profile_token_path
40 end 36 end
  37 +
  38 + private
  39 +
  40 + def user
  41 + @user = current_user
  42 + end
41 end 43 end
app/controllers/search_controller.rb
1 class SearchController < ApplicationController 1 class SearchController < ApplicationController
2 def show 2 def show
3 query = params[:search] 3 query = params[:search]
4 - if query.blank?  
5 - @projects = []  
6 - @merge_requests = []  
7 - @issues = []  
8 - else 4 +
  5 + @projects = []
  6 + @merge_requests = []
  7 + @issues = []
  8 +
  9 + if query.present?
9 @projects = current_user.projects.search(query).limit(10) 10 @projects = current_user.projects.search(query).limit(10)
10 @merge_requests = MergeRequest.where(:project_id => current_user.project_ids).search(query).limit(10) 11 @merge_requests = MergeRequest.where(:project_id => current_user.project_ids).search(query).limit(10)
11 @issues = Issue.where(:project_id => current_user.project_ids).search(query).limit(10) 12 @issues = Issue.where(:project_id => current_user.project_ids).search(query).limit(10)
app/roles/team.rb
@@ -4,7 +4,36 @@ module Team @@ -4,7 +4,36 @@ module Team
4 users_projects.find_by_user_id(user.id) if user 4 users_projects.find_by_user_id(user.id) if user
5 end 5 end
6 6
  7 + # Get Team Member record by user id
7 def team_member_by_id(user_id) 8 def team_member_by_id(user_id)
8 users_projects.find_by_user_id(user_id) 9 users_projects.find_by_user_id(user_id)
9 end 10 end
  11 +
  12 + # Add user to project
  13 + # with passed access role
  14 + def add_user_to_team(user, access_role)
  15 + add_user_id_to_team(user.id, access_role)
  16 + end
  17 +
  18 + # Add multiple users to project
  19 + # with same access role
  20 + def add_users_to_team(users, access_role)
  21 + add_users_ids_to_team(users.map(&:id), access_role)
  22 + end
  23 +
  24 + # Add user to project
  25 + # with passed access role by user id
  26 + def add_user_id_to_team(user_id, access_role)
  27 + users_projects.create(
  28 + :user_id => user_id,
  29 + :project_access => access_role
  30 + )
  31 + end
  32 +
  33 + # Add multiple users to project
  34 + # with same access role by user ids
  35 + def add_users_ids_to_team(users_ids, access_role)
  36 + UsersProject.bulk_import(self, users_ids, access_role)
  37 + self.update_repository
  38 + end
10 end 39 end
spec/models/project_spec.rb
@@ -22,21 +22,56 @@ describe Project do @@ -22,21 +22,56 @@ describe Project do
22 end 22 end
23 23
24 describe "Respond to" do 24 describe "Respond to" do
25 - it { should respond_to(:repository_writers) }  
26 - it { should respond_to(:add_access) }  
27 - it { should respond_to(:reset_access) }  
28 - it { should respond_to(:update_repository) }  
29 - it { should respond_to(:destroy_repository) }  
30 it { should respond_to(:public?) } 25 it { should respond_to(:public?) }
31 it { should respond_to(:private?) } 26 it { should respond_to(:private?) }
32 it { should respond_to(:url_to_repo) } 27 it { should respond_to(:url_to_repo) }
33 it { should respond_to(:path_to_repo) } 28 it { should respond_to(:path_to_repo) }
34 it { should respond_to(:valid_repo?) } 29 it { should respond_to(:valid_repo?) }
35 it { should respond_to(:repo_exists?) } 30 it { should respond_to(:repo_exists?) }
  31 +
  32 + # Repository Role
  33 + it { should respond_to(:tree) }
  34 + it { should respond_to(:root_ref) }
36 it { should respond_to(:repo) } 35 it { should respond_to(:repo) }
37 it { should respond_to(:tags) } 36 it { should respond_to(:tags) }
38 it { should respond_to(:commit) } 37 it { should respond_to(:commit) }
  38 + it { should respond_to(:commits) }
  39 + it { should respond_to(:commits_between) }
  40 + it { should respond_to(:commits_with_refs) }
  41 + it { should respond_to(:commits_since) }
39 it { should respond_to(:commits_between) } 42 it { should respond_to(:commits_between) }
  43 + it { should respond_to(:write_hooks) }
  44 + it { should respond_to(:satellite) }
  45 + it { should respond_to(:update_repository) }
  46 + it { should respond_to(:destroy_repository) }
  47 + it { should respond_to(:archive_repo) }
  48 +
  49 + # Authority Role
  50 + it { should respond_to(:add_access) }
  51 + it { should respond_to(:reset_access) }
  52 + it { should respond_to(:repository_writers) }
  53 + it { should respond_to(:repository_masters) }
  54 + it { should respond_to(:repository_readers) }
  55 + it { should respond_to(:allow_read_for?) }
  56 + it { should respond_to(:guest_access_for?) }
  57 + it { should respond_to(:report_access_for?) }
  58 + it { should respond_to(:dev_access_for?) }
  59 + it { should respond_to(:master_access_for?) }
  60 +
  61 + # Team Role
  62 + it { should respond_to(:team_member_by_name_or_email) }
  63 + it { should respond_to(:team_member_by_id) }
  64 + it { should respond_to(:add_user_to_team) }
  65 + it { should respond_to(:add_users_to_team) }
  66 + it { should respond_to(:add_user_id_to_team) }
  67 + it { should respond_to(:add_users_ids_to_team) }
  68 +
  69 + # Project Push Role
  70 + it { should respond_to(:observe_push) }
  71 + it { should respond_to(:update_merge_requests) }
  72 + it { should respond_to(:execute_hooks) }
  73 + it { should respond_to(:post_receive_data) }
  74 + it { should respond_to(:trigger_post_receive) }
40 end 75 end
41 76
42 it "should not allow 'gitolite-admin' as repo name" do 77 it "should not allow 'gitolite-admin' as repo name" do