Project model refactored

This commit is contained in:
Dmitriy Zaporozhets 2012-03-09 21:43:46 +02:00
parent 9988282e84
commit cd06d6edac
4 changed files with 82 additions and 68 deletions

View file

@ -20,6 +20,66 @@ class Commit
:id, :id,
:to => :commit :to => :commit
class << self
def find_or_first(repo, commit_id = nil)
commit = if commit_id
repo.commit(commit_id)
else
repo.commits.first
end
Commit.new(commit) if commit
end
def fresh_commits(repo, n = 10)
commits = repo.heads.map do |h|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0...n]
end
def commits_with_refs(repo, n = 20)
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0..n]
end
def commits_since(repo, date)
commits = repo.heads.map do |h|
repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits
end
def commits(repo, ref, path = nil, limit = nil, offset = nil)
if path
repo.log(ref, path, :max_count => limit, :skip => offset)
elsif limit && offset
repo.commits(ref, limit, offset)
else
repo.commits(ref)
end.map{ |c| Commit.new(c) }
end
def commits_between(repo, from, to)
repo.commits_between(from, to).map { |c| Commit.new(c) }
end
end
def persisted? def persisted?
false false
end end

View file

@ -69,7 +69,7 @@ class Project < ActiveRecord::Base
:project => self, :project => self,
:action => Event::Pushed, :action => Event::Pushed,
:data => data, :data => data,
:author_id => Key.find_by_identifier(author_key_id).user.id :author_id => data[:user_id]
) )
end end
@ -259,60 +259,27 @@ class Project < ActiveRecord::Base
end end
def commit(commit_id = nil) def commit(commit_id = nil)
commit = if commit_id Commit.find_or_first(repo, commit_id)
repo.commit(commit_id)
else
repo.commits.first
end
Commit.new(commit) if commit
end end
def fresh_commits(n = 10) def fresh_commits(n = 10)
commits = heads.map do |h| Commit.fresh_commits(repo, n)
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0...n]
end end
def commits_with_refs(n = 20) def commits_with_refs(n = 20)
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) } Commit.commits_with_refs(repo, n)
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits[0..n]
end end
def commits_since(date) def commits_since(date)
commits = heads.map do |h| Commit.commits_since(repo, date)
repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
end.flatten.uniq { |c| c.id }
commits.sort! do |x, y|
y.committed_date <=> x.committed_date
end
commits
end end
def commits(ref, path = nil, limit = nil, offset = nil) def commits(ref, path = nil, limit = nil, offset = nil)
if path Commit.commits(repo, ref, path, limit, offset)
repo.log(ref, path, :max_count => limit, :skip => offset)
elsif limit && offset
repo.commits(ref, limit, offset)
else
repo.commits(ref)
end.map{ |c| Commit.new(c) }
end end
def commits_between(from, to) def commits_between(from, to)
repo.commits_between(from, to).map { |c| Commit.new(c) } Commit.commits_between(repo, from, to)
end end
def project_id def project_id

View file

@ -1,13 +0,0 @@
require File.join(Rails.root, "lib", "gitlabhq", "git_host")
class Repository
attr_accessor :project
def self.default_ref
"master"
end
def self.access_options
{}
end
end

View file

@ -1,21 +1,21 @@
UsersProject.seed(:id, [ UsersProject.seed(:id, [
{ :id => 1, :project_id => 1, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, { :id => 1, :project_id => 1, :user_id => 1, :project_access => UsersProject::MASTER },
{ :id => 2, :project_id => 1, :user_id => 2, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N }, { :id => 2, :project_id => 1, :user_id => 2, :project_access => UsersProject::REPORTERW},
{ :id => 3, :project_id => 1, :user_id => 3, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N }, { :id => 3, :project_id => 1, :user_id => 3, :project_access => UsersProject::REPORTERW},
{ :id => 4, :project_id => 1, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 4, :project_id => 1, :user_id => 4, :project_access => UsersProject::REPORTER},
{ :id => 5, :project_id => 1, :user_id => 5, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 5, :project_id => 1, :user_id => 5, :project_access => UsersProject::REPORTER},
{ :id => 6, :project_id => 2, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, { :id => 6, :project_id => 2, :user_id => 1, :project_access => UsersProject::MASTER },
{ :id => 7, :project_id => 2, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 7, :project_id => 2, :user_id => 2, :project_access => UsersProject::REPORTER},
{ :id => 8, :project_id => 2, :user_id => 3, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 8, :project_id => 2, :user_id => 3, :project_access => UsersProject::REPORTER},
{ :id => 9, :project_id => 2, :user_id => 4, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, { :id => 9, :project_id => 2, :user_id => 4, :project_access => UsersProject::MASTER},
{ :id => 11, :project_id => 2, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, { :id => 11, :project_id => 2, :user_id => 5, :project_access => UsersProject::MASTER},
{ :id => 12, :project_id => 3, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, { :id => 12, :project_id => 3, :user_id => 1, :project_access => UsersProject::MASTER },
{ :id => 13, :project_id => 3, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 13, :project_id => 3, :user_id => 2, :project_access => UsersProject::REPORTER},
{ :id => 14, :project_id => 3, :user_id => 3, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, { :id => 14, :project_id => 3, :user_id => 3, :project_access => UsersProject::MASTER},
{ :id => 15, :project_id => 3, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, { :id => 15, :project_id => 3, :user_id => 4, :project_access => UsersProject::REPORTER},
{ :id => 16, :project_id => 3, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N } { :id => 16, :project_id => 3, :user_id => 5, :project_access => UsersProject::MASTER}
]) ])