OSDN Git Service

refactor code: repository.rb
authorDmitriy Zaporozhets <dzaporozhets@sphereconsultinginc.com>
Thu, 10 Nov 2011 22:51:19 +0000 (00:51 +0200)
committerDmitriy Zaporozhets <dzaporozhets@sphereconsultinginc.com>
Thu, 10 Nov 2011 22:51:33 +0000 (00:51 +0200)
app/controllers/application_controller.rb
app/controllers/projects_controller.rb
app/models/project.rb
app/models/repository.rb [new file with mode: 0644]

index 782612c..f16f1a3 100644 (file)
@@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base
     else
       @branch = params[:branch].blank? ? nil : params[:branch]
       @tag = params[:tag].blank? ? nil : params[:tag]
-      @ref = @branch || @tag || "master"
+      @ref = @branch || @tag || Repository.default_ref
     end
   end
 
index 2df5e9c..5e00fe4 100644 (file)
@@ -108,7 +108,7 @@ class ProjectsController < ApplicationController
     if params[:commit_id]
       @commit = @repo.commits(params[:commit_id]).first
     else
-      @commit = @repo.commits(@ref || "master").first
+      @commit = @repo.commits(@ref).first
     end
 
     @tree = @commit.tree
index a5b6224..9767352 100644 (file)
@@ -46,6 +46,21 @@ class Project < ActiveRecord::Base
 
   scope :public_only, where(:private_flag => false)
 
+  def repository
+    @repository ||= Repository.new(self)
+  end
+
+  delegate :repo,
+    :tags,
+    :repo_exists?,
+    :commit,
+    :commits,
+    :tree,
+    :heads,
+    :commits_since,
+    :fresh_commits,
+    :to => :repository, :prefix => nil
+
   def to_param
     code
   end
@@ -114,18 +129,6 @@ class Project < ActiveRecord::Base
     GITOSIS["base_path"] + path + ".git"
   end
 
-  def repo
-    @repo ||= Grit::Repo.new(path_to_repo)
-  end
-
-  def tags
-    repo.tags.map(&:name).sort.reverse
-  end
-
-  def repo_exists?
-    repo rescue false
-  end
-
   def last_activity 
     updates(1).first
   rescue 
@@ -146,48 +149,6 @@ class Project < ActiveRecord::Base
     end[0...n]
   end
 
-  def commit(commit_id = nil)
-    if commit_id
-      repo.commits(commit_id).first
-    else
-      repo.commits.first
-    end
-  end
-
-  def heads
-    @heads ||= repo.heads
-  end
-
-  def fresh_commits(n = 10)
-    commits = heads.map do |h|
-      repo.commits(h.name, n)
-    end.flatten.uniq { |c| c.id }
-
-    commits.sort! do |x, y|
-      y.committed_date <=> x.committed_date
-    end
-
-    commits[0...n]
-  end
-
-  def commits_since(date)
-    commits = heads.map do |h|
-      repo.log(h.name, nil, :since => date)
-    end.flatten.uniq { |c| c.id }
-
-    commits.sort! do |x, y|
-      y.committed_date <=> x.committed_date
-    end
-
-    commits
-  end
-
-  def tree(fcommit, path = nil)
-    fcommit = commit if fcommit == :head
-    tree = fcommit.tree
-    path ? (tree / path) : tree
-  end
-
   def check_limit
     unless owner.can_create_project?
       errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
diff --git a/app/models/repository.rb b/app/models/repository.rb
new file mode 100644 (file)
index 0000000..da679d8
--- /dev/null
@@ -0,0 +1,65 @@
+class Repository
+  attr_accessor :project
+
+  def self.default_ref
+    "master"
+  end
+
+  def initialize(project)
+    @project = project
+  end
+
+  def repo
+    @repo ||= Grit::Repo.new(project.path_to_repo)
+  end
+
+  def tags
+    repo.tags.map(&:name).sort.reverse
+  end
+
+  def repo_exists?
+    repo rescue false
+  end
+
+  def commit(commit_id = nil)
+    if commit_id
+      repo.commits(commit_id).first
+    else
+      repo.commits.first
+    end
+  end
+
+  def tree(fcommit, path = nil)
+    fcommit = commit if fcommit == :head
+    tree = fcommit.tree
+    path ? (tree / path) : tree
+  end
+
+  def fresh_commits(n = 10)
+    commits = heads.map do |h|
+      repo.commits(h.name, n)
+    end.flatten.uniq { |c| c.id }
+
+    commits.sort! do |x, y|
+      y.committed_date <=> x.committed_date
+    end
+
+    commits[0...n]
+  end
+
+  def heads
+    @heads ||= repo.heads
+  end
+
+  def commits_since(date)
+    commits = heads.map do |h|
+      repo.log(h.name, nil, :since => date)
+    end.flatten.uniq { |c| c.id }
+
+    commits.sort! do |x, y|
+      y.committed_date <=> x.committed_date
+    end
+
+    commits
+  end
+end