Commit dab072c1ab609917c3472ae663f1db49af75e56e

Authored by Dmitriy Zaporozhets
1 parent 99bd0015

refactor code: repository.rb

app/controllers/application_controller.rb
@@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base @@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base
64 else 64 else
65 @branch = params[:branch].blank? ? nil : params[:branch] 65 @branch = params[:branch].blank? ? nil : params[:branch]
66 @tag = params[:tag].blank? ? nil : params[:tag] 66 @tag = params[:tag].blank? ? nil : params[:tag]
67 - @ref = @branch || @tag || "master" 67 + @ref = @branch || @tag || Repository.default_ref
68 end 68 end
69 end 69 end
70 70
app/controllers/projects_controller.rb
@@ -108,7 +108,7 @@ class ProjectsController < ApplicationController @@ -108,7 +108,7 @@ class ProjectsController < ApplicationController
108 if params[:commit_id] 108 if params[:commit_id]
109 @commit = @repo.commits(params[:commit_id]).first 109 @commit = @repo.commits(params[:commit_id]).first
110 else 110 else
111 - @commit = @repo.commits(@ref || "master").first 111 + @commit = @repo.commits(@ref).first
112 end 112 end
113 113
114 @tree = @commit.tree 114 @tree = @commit.tree
app/models/project.rb
@@ -46,6 +46,21 @@ class Project < ActiveRecord::Base @@ -46,6 +46,21 @@ class Project < ActiveRecord::Base
46 46
47 scope :public_only, where(:private_flag => false) 47 scope :public_only, where(:private_flag => false)
48 48
  49 + def repository
  50 + @repository ||= Repository.new(self)
  51 + end
  52 +
  53 + delegate :repo,
  54 + :tags,
  55 + :repo_exists?,
  56 + :commit,
  57 + :commits,
  58 + :tree,
  59 + :heads,
  60 + :commits_since,
  61 + :fresh_commits,
  62 + :to => :repository, :prefix => nil
  63 +
49 def to_param 64 def to_param
50 code 65 code
51 end 66 end
@@ -114,18 +129,6 @@ class Project < ActiveRecord::Base @@ -114,18 +129,6 @@ class Project < ActiveRecord::Base
114 GITOSIS["base_path"] + path + ".git" 129 GITOSIS["base_path"] + path + ".git"
115 end 130 end
116 131
117 - def repo  
118 - @repo ||= Grit::Repo.new(path_to_repo)  
119 - end  
120 -  
121 - def tags  
122 - repo.tags.map(&:name).sort.reverse  
123 - end  
124 -  
125 - def repo_exists?  
126 - repo rescue false  
127 - end  
128 -  
129 def last_activity 132 def last_activity
130 updates(1).first 133 updates(1).first
131 rescue 134 rescue
@@ -146,48 +149,6 @@ class Project < ActiveRecord::Base @@ -146,48 +149,6 @@ class Project < ActiveRecord::Base
146 end[0...n] 149 end[0...n]
147 end 150 end
148 151
149 - def commit(commit_id = nil)  
150 - if commit_id  
151 - repo.commits(commit_id).first  
152 - else  
153 - repo.commits.first  
154 - end  
155 - end  
156 -  
157 - def heads  
158 - @heads ||= repo.heads  
159 - end  
160 -  
161 - def fresh_commits(n = 10)  
162 - commits = heads.map do |h|  
163 - repo.commits(h.name, n)  
164 - end.flatten.uniq { |c| c.id }  
165 -  
166 - commits.sort! do |x, y|  
167 - y.committed_date <=> x.committed_date  
168 - end  
169 -  
170 - commits[0...n]  
171 - end  
172 -  
173 - def commits_since(date)  
174 - commits = heads.map do |h|  
175 - repo.log(h.name, nil, :since => date)  
176 - end.flatten.uniq { |c| c.id }  
177 -  
178 - commits.sort! do |x, y|  
179 - y.committed_date <=> x.committed_date  
180 - end  
181 -  
182 - commits  
183 - end  
184 -  
185 - def tree(fcommit, path = nil)  
186 - fcommit = commit if fcommit == :head  
187 - tree = fcommit.tree  
188 - path ? (tree / path) : tree  
189 - end  
190 -  
191 def check_limit 152 def check_limit
192 unless owner.can_create_project? 153 unless owner.can_create_project?
193 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") 154 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
app/models/repository.rb 0 → 100644
@@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
  1 +class Repository
  2 + attr_accessor :project
  3 +
  4 + def self.default_ref
  5 + "master"
  6 + end
  7 +
  8 + def initialize(project)
  9 + @project = project
  10 + end
  11 +
  12 + def repo
  13 + @repo ||= Grit::Repo.new(project.path_to_repo)
  14 + end
  15 +
  16 + def tags
  17 + repo.tags.map(&:name).sort.reverse
  18 + end
  19 +
  20 + def repo_exists?
  21 + repo rescue false
  22 + end
  23 +
  24 + def commit(commit_id = nil)
  25 + if commit_id
  26 + repo.commits(commit_id).first
  27 + else
  28 + repo.commits.first
  29 + end
  30 + end
  31 +
  32 + def tree(fcommit, path = nil)
  33 + fcommit = commit if fcommit == :head
  34 + tree = fcommit.tree
  35 + path ? (tree / path) : tree
  36 + end
  37 +
  38 + def fresh_commits(n = 10)
  39 + commits = heads.map do |h|
  40 + repo.commits(h.name, n)
  41 + end.flatten.uniq { |c| c.id }
  42 +
  43 + commits.sort! do |x, y|
  44 + y.committed_date <=> x.committed_date
  45 + end
  46 +
  47 + commits[0...n]
  48 + end
  49 +
  50 + def heads
  51 + @heads ||= repo.heads
  52 + end
  53 +
  54 + def commits_since(date)
  55 + commits = heads.map do |h|
  56 + repo.log(h.name, nil, :since => date)
  57 + end.flatten.uniq { |c| c.id }
  58 +
  59 + commits.sort! do |x, y|
  60 + y.committed_date <=> x.committed_date
  61 + end
  62 +
  63 + commits
  64 + end
  65 +end