Commit cd06d6edacac73ea798d05754fb47c110d9ae0de
1 parent
9988282e
Exists in
master
and in
4 other branches
Project model refactored
Showing
4 changed files
with
82 additions
and
68 deletions
Show diff stats
app/models/commit.rb
| @@ -20,6 +20,66 @@ class Commit | @@ -20,6 +20,66 @@ class Commit | ||
| 20 | :id, | 20 | :id, |
| 21 | :to => :commit | 21 | :to => :commit |
| 22 | 22 | ||
| 23 | + | ||
| 24 | + class << self | ||
| 25 | + def find_or_first(repo, commit_id = nil) | ||
| 26 | + commit = if commit_id | ||
| 27 | + repo.commit(commit_id) | ||
| 28 | + else | ||
| 29 | + repo.commits.first | ||
| 30 | + end | ||
| 31 | + Commit.new(commit) if commit | ||
| 32 | + end | ||
| 33 | + | ||
| 34 | + def fresh_commits(repo, n = 10) | ||
| 35 | + commits = repo.heads.map do |h| | ||
| 36 | + repo.commits(h.name, n).map { |c| Commit.new(c, h) } | ||
| 37 | + end.flatten.uniq { |c| c.id } | ||
| 38 | + | ||
| 39 | + commits.sort! do |x, y| | ||
| 40 | + y.committed_date <=> x.committed_date | ||
| 41 | + end | ||
| 42 | + | ||
| 43 | + commits[0...n] | ||
| 44 | + end | ||
| 45 | + | ||
| 46 | + def commits_with_refs(repo, n = 20) | ||
| 47 | + commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) } | ||
| 48 | + | ||
| 49 | + commits.sort! do |x, y| | ||
| 50 | + y.committed_date <=> x.committed_date | ||
| 51 | + end | ||
| 52 | + | ||
| 53 | + commits[0..n] | ||
| 54 | + end | ||
| 55 | + | ||
| 56 | + def commits_since(repo, date) | ||
| 57 | + commits = repo.heads.map do |h| | ||
| 58 | + repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) } | ||
| 59 | + end.flatten.uniq { |c| c.id } | ||
| 60 | + | ||
| 61 | + commits.sort! do |x, y| | ||
| 62 | + y.committed_date <=> x.committed_date | ||
| 63 | + end | ||
| 64 | + | ||
| 65 | + commits | ||
| 66 | + end | ||
| 67 | + | ||
| 68 | + def commits(repo, ref, path = nil, limit = nil, offset = nil) | ||
| 69 | + if path | ||
| 70 | + repo.log(ref, path, :max_count => limit, :skip => offset) | ||
| 71 | + elsif limit && offset | ||
| 72 | + repo.commits(ref, limit, offset) | ||
| 73 | + else | ||
| 74 | + repo.commits(ref) | ||
| 75 | + end.map{ |c| Commit.new(c) } | ||
| 76 | + end | ||
| 77 | + | ||
| 78 | + def commits_between(repo, from, to) | ||
| 79 | + repo.commits_between(from, to).map { |c| Commit.new(c) } | ||
| 80 | + end | ||
| 81 | + end | ||
| 82 | + | ||
| 23 | def persisted? | 83 | def persisted? |
| 24 | false | 84 | false |
| 25 | end | 85 | end |
app/models/project.rb
| @@ -69,7 +69,7 @@ class Project < ActiveRecord::Base | @@ -69,7 +69,7 @@ class Project < ActiveRecord::Base | ||
| 69 | :project => self, | 69 | :project => self, |
| 70 | :action => Event::Pushed, | 70 | :action => Event::Pushed, |
| 71 | :data => data, | 71 | :data => data, |
| 72 | - :author_id => Key.find_by_identifier(author_key_id).user.id | 72 | + :author_id => data[:user_id] |
| 73 | ) | 73 | ) |
| 74 | end | 74 | end |
| 75 | 75 | ||
| @@ -259,60 +259,27 @@ class Project < ActiveRecord::Base | @@ -259,60 +259,27 @@ class Project < ActiveRecord::Base | ||
| 259 | end | 259 | end |
| 260 | 260 | ||
| 261 | def commit(commit_id = nil) | 261 | def commit(commit_id = nil) |
| 262 | - commit = if commit_id | ||
| 263 | - repo.commit(commit_id) | ||
| 264 | - else | ||
| 265 | - repo.commits.first | ||
| 266 | - end | ||
| 267 | - Commit.new(commit) if commit | 262 | + Commit.find_or_first(repo, commit_id) |
| 268 | end | 263 | end |
| 269 | 264 | ||
| 270 | def fresh_commits(n = 10) | 265 | def fresh_commits(n = 10) |
| 271 | - commits = heads.map do |h| | ||
| 272 | - repo.commits(h.name, n).map { |c| Commit.new(c, h) } | ||
| 273 | - end.flatten.uniq { |c| c.id } | ||
| 274 | - | ||
| 275 | - commits.sort! do |x, y| | ||
| 276 | - y.committed_date <=> x.committed_date | ||
| 277 | - end | ||
| 278 | - | ||
| 279 | - commits[0...n] | 266 | + Commit.fresh_commits(repo, n) |
| 280 | end | 267 | end |
| 281 | 268 | ||
| 282 | def commits_with_refs(n = 20) | 269 | def commits_with_refs(n = 20) |
| 283 | - commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) } | ||
| 284 | - | ||
| 285 | - commits.sort! do |x, y| | ||
| 286 | - y.committed_date <=> x.committed_date | ||
| 287 | - end | ||
| 288 | - | ||
| 289 | - commits[0..n] | 270 | + Commit.commits_with_refs(repo, n) |
| 290 | end | 271 | end |
| 291 | 272 | ||
| 292 | def commits_since(date) | 273 | def commits_since(date) |
| 293 | - commits = heads.map do |h| | ||
| 294 | - repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) } | ||
| 295 | - end.flatten.uniq { |c| c.id } | ||
| 296 | - | ||
| 297 | - commits.sort! do |x, y| | ||
| 298 | - y.committed_date <=> x.committed_date | ||
| 299 | - end | ||
| 300 | - | ||
| 301 | - commits | 274 | + Commit.commits_since(repo, date) |
| 302 | end | 275 | end |
| 303 | 276 | ||
| 304 | def commits(ref, path = nil, limit = nil, offset = nil) | 277 | def commits(ref, path = nil, limit = nil, offset = nil) |
| 305 | - if path | ||
| 306 | - repo.log(ref, path, :max_count => limit, :skip => offset) | ||
| 307 | - elsif limit && offset | ||
| 308 | - repo.commits(ref, limit, offset) | ||
| 309 | - else | ||
| 310 | - repo.commits(ref) | ||
| 311 | - end.map{ |c| Commit.new(c) } | 278 | + Commit.commits(repo, ref, path, limit, offset) |
| 312 | end | 279 | end |
| 313 | 280 | ||
| 314 | def commits_between(from, to) | 281 | def commits_between(from, to) |
| 315 | - repo.commits_between(from, to).map { |c| Commit.new(c) } | 282 | + Commit.commits_between(repo, from, to) |
| 316 | end | 283 | end |
| 317 | 284 | ||
| 318 | def project_id | 285 | def project_id |
app/models/repository.rb
db/fixtures/development/004_teams.rb
| 1 | UsersProject.seed(:id, [ | 1 | UsersProject.seed(:id, [ |
| 2 | - { :id => 1, :project_id => 1, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, | ||
| 3 | - { :id => 2, :project_id => 1, :user_id => 2, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N }, | ||
| 4 | - { :id => 3, :project_id => 1, :user_id => 3, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N }, | ||
| 5 | - { :id => 4, :project_id => 1, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | ||
| 6 | - { :id => 5, :project_id => 1, :user_id => 5, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | 2 | + { :id => 1, :project_id => 1, :user_id => 1, :project_access => UsersProject::MASTER }, |
| 3 | + { :id => 2, :project_id => 1, :user_id => 2, :project_access => UsersProject::REPORTERW}, | ||
| 4 | + { :id => 3, :project_id => 1, :user_id => 3, :project_access => UsersProject::REPORTERW}, | ||
| 5 | + { :id => 4, :project_id => 1, :user_id => 4, :project_access => UsersProject::REPORTER}, | ||
| 6 | + { :id => 5, :project_id => 1, :user_id => 5, :project_access => UsersProject::REPORTER}, | ||
| 7 | 7 | ||
| 8 | - { :id => 6, :project_id => 2, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, | ||
| 9 | - { :id => 7, :project_id => 2, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | ||
| 10 | - { :id => 8, :project_id => 2, :user_id => 3, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | ||
| 11 | - { :id => 9, :project_id => 2, :user_id => 4, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, | ||
| 12 | - { :id => 11, :project_id => 2, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, | 8 | + { :id => 6, :project_id => 2, :user_id => 1, :project_access => UsersProject::MASTER }, |
| 9 | + { :id => 7, :project_id => 2, :user_id => 2, :project_access => UsersProject::REPORTER}, | ||
| 10 | + { :id => 8, :project_id => 2, :user_id => 3, :project_access => UsersProject::REPORTER}, | ||
| 11 | + { :id => 9, :project_id => 2, :user_id => 4, :project_access => UsersProject::MASTER}, | ||
| 12 | + { :id => 11, :project_id => 2, :user_id => 5, :project_access => UsersProject::MASTER}, | ||
| 13 | 13 | ||
| 14 | - { :id => 12, :project_id => 3, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW }, | ||
| 15 | - { :id => 13, :project_id => 3, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | ||
| 16 | - { :id => 14, :project_id => 3, :user_id => 3, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }, | ||
| 17 | - { :id => 15, :project_id => 3, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N }, | ||
| 18 | - { :id => 16, :project_id => 3, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N } | 14 | + { :id => 12, :project_id => 3, :user_id => 1, :project_access => UsersProject::MASTER }, |
| 15 | + { :id => 13, :project_id => 3, :user_id => 2, :project_access => UsersProject::REPORTER}, | ||
| 16 | + { :id => 14, :project_id => 3, :user_id => 3, :project_access => UsersProject::MASTER}, | ||
| 17 | + { :id => 15, :project_id => 3, :user_id => 4, :project_access => UsersProject::REPORTER}, | ||
| 18 | + { :id => 16, :project_id => 3, :user_id => 5, :project_access => UsersProject::MASTER} | ||
| 19 | ]) | 19 | ]) |
| 20 | 20 | ||
| 21 | 21 |