Commit d730e3ef8b1c9bf73f9eeb492968b455fbec319d

Authored by Dmitriy Zaporozhets
1 parent dab072c1

refactoring project, commits controllers

app/controllers/commits_controller.rb
... ... @@ -8,18 +8,18 @@ class CommitsController < ApplicationController
8 8 before_filter :add_project_abilities
9 9 before_filter :authorize_read_project!
10 10 before_filter :require_non_empty_project
  11 + before_filter :load_refs, :only => :index # load @branch, @tag & @ref
11 12  
12   - def index
13   - load_refs # load @branch, @tag & @ref
14 13  
  14 + def index
15 15 @repo = project.repo
16 16 limit, offset = (params[:limit] || 20), (params[:offset] || 0)
17 17  
18   - if params[:path]
19   - @commits = @repo.log(@ref, params[:path], :max_count => limit, :skip => offset)
20   - else
21   - @commits = @repo.commits(@ref, limit, offset)
22   - end
  18 + @commits = if params[:path]
  19 + @repo.log(@ref, params[:path], :max_count => limit, :skip => offset)
  20 + else
  21 + @repo.commits(@ref, limit, offset)
  22 + end
23 23  
24 24 respond_to do |format|
25 25 format.html # index.html.erb
... ... @@ -29,8 +29,8 @@ class CommitsController < ApplicationController
29 29  
30 30 def show
31 31 @commit = project.repo.commits(params[:id]).first
32   - @notes = project.notes.where(:noteable_id => @commit.id, :noteable_type => "Commit").order("created_at DESC").limit(20)
33   - @note = @project.notes.new(:noteable_id => @commit.id, :noteable_type => "Commit")
  32 + @notes = project.commit_notes(@commit).fresh.limit(20)
  33 + @note = @project.build_commit_note(@commit)
34 34  
35 35 respond_to do |format|
36 36 format.html
... ...
app/controllers/projects_controller.rb
... ... @@ -6,8 +6,8 @@ class ProjectsController < ApplicationController
6 6 before_filter :add_project_abilities
7 7 before_filter :authorize_read_project!, :except => [:index, :new, :create]
8 8 before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy]
9   -
10 9 before_filter :require_non_empty_project, :only => [:blob, :tree]
  10 + before_filter :load_refs, :only => :tree # load @branch, @tag & @ref
11 11  
12 12 def index
13 13 source = current_user.projects
... ... @@ -101,15 +101,13 @@ class ProjectsController < ApplicationController
101 101 #
102 102  
103 103 def tree
104   - load_refs # load @branch, @tag & @ref
105   -
106 104 @repo = project.repo
107 105  
108   - if params[:commit_id]
109   - @commit = @repo.commits(params[:commit_id]).first
110   - else
111   - @commit = @repo.commits(@ref).first
112   - end
  106 + @commit = if params[:commit_id]
  107 + @repo.commits(params[:commit_id]).first
  108 + else
  109 + @repo.commits(@ref).first
  110 + end
113 111  
114 112 @tree = @commit.tree
115 113 @tree = @tree / params[:path] if params[:path]
... ...
app/models/project.rb
... ... @@ -51,6 +51,10 @@ class Project < ActiveRecord::Base
51 51 end
52 52  
53 53 delegate :repo,
  54 + :url_to_repo,
  55 + :path_to_repo,
  56 + :update_gitosis_project,
  57 + :destroy_gitosis_project,
54 58 :tags,
55 59 :repo_exists?,
56 60 :commit,
... ... @@ -74,16 +78,12 @@ class Project < ActiveRecord::Base
74 78 notes.where(:noteable_type => ["", nil])
75 79 end
76 80  
77   - def update_gitosis_project
78   - Gitosis.new.configure do |c|
79   - c.update_project(path, gitosis_writers)
80   - end
  81 + def build_commit_note(commit)
  82 + notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
81 83 end
82 84  
83   - def destroy_gitosis_project
84   - Gitosis.new.configure do |c|
85   - c.destroy_project(self)
86   - end
  85 + def commit_notes(commit)
  86 + notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
87 87 end
88 88  
89 89 def add_access(user, *access)
... ... @@ -121,14 +121,6 @@ class Project < ActiveRecord::Base
121 121 private_flag
122 122 end
123 123  
124   - def url_to_repo
125   - "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"
126   - end
127   -
128   - def path_to_repo
129   - GITOSIS["base_path"] + path + ".git"
130   - end
131   -
132 124 def last_activity
133 125 updates(1).first
134 126 rescue
... ...
app/models/repository.rb
... ... @@ -9,24 +9,48 @@ class Repository
9 9 @project = project
10 10 end
11 11  
  12 + def path
  13 + @path ||= project.path
  14 + end
  15 +
  16 + def project_id
  17 + project.id
  18 + end
  19 +
12 20 def repo
13 21 @repo ||= Grit::Repo.new(project.path_to_repo)
14 22 end
15 23  
16   - def tags
17   - repo.tags.map(&:name).sort.reverse
  24 + def url_to_repo
  25 + "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"
  26 + end
  27 +
  28 + def path_to_repo
  29 + GITOSIS["base_path"] + path + ".git"
  30 + end
  31 +
  32 + def update_gitosis_project
  33 + Gitosis.new.configure do |c|
  34 + c.update_project(path, project.gitosis_writers)
  35 + end
  36 + end
  37 +
  38 + def destroy_gitosis_project
  39 + Gitosis.new.configure do |c|
  40 + c.destroy_project(@project)
  41 + end
18 42 end
19 43  
20 44 def repo_exists?
21 45 repo rescue false
22 46 end
23 47  
24   - def commit(commit_id = nil)
25   - if commit_id
26   - repo.commits(commit_id).first
27   - else
28   - repo.commits.first
29   - end
  48 + def tags
  49 + repo.tags.map(&:name).sort.reverse
  50 + end
  51 +
  52 + def heads
  53 + @heads ||= repo.heads
30 54 end
31 55  
32 56 def tree(fcommit, path = nil)
... ... @@ -35,6 +59,14 @@ class Repository
35 59 path ? (tree / path) : tree
36 60 end
37 61  
  62 + def commit(commit_id = nil)
  63 + if commit_id
  64 + repo.commits(commit_id).first
  65 + else
  66 + repo.commits.first
  67 + end
  68 + end
  69 +
38 70 def fresh_commits(n = 10)
39 71 commits = heads.map do |h|
40 72 repo.commits(h.name, n)
... ... @@ -47,10 +79,6 @@ class Repository
47 79 commits[0...n]
48 80 end
49 81  
50   - def heads
51   - @heads ||= repo.heads
52   - end
53   -
54 82 def commits_since(date)
55 83 commits = heads.map do |h|
56 84 repo.log(h.name, nil, :since => date)
... ...