]> source.dussan.org Git - redmine.git/commitdiff
Move some RepositoriesController logic to Repository#stats_by_author (#13487).
authorJean-Baptiste Barth <jeanbaptiste.barth@gmail.com>
Tue, 26 Aug 2014 13:58:58 +0000 (13:58 +0000)
committerJean-Baptiste Barth <jeanbaptiste.barth@gmail.com>
Tue, 26 Aug 2014 13:58:58 +0000 (13:58 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@13351 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/repositories_controller.rb
app/models/repository.rb
test/unit/repository_test.rb

index bdd56f0aad045a62b0bb9305c9cb8838ef663a9c..59afc5529adf508d3786be758feb1c1d69ced95d 100644 (file)
@@ -397,16 +397,16 @@ class RepositoriesController < ApplicationController
   end
 
   def graph_commits_per_author(repository)
-    commits_by_author = Changeset.where("repository_id = ?", repository.id).group(:committer).count
-    commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
-
-    changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", repository.id).group(:committer).count
-    h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
-
-    fields = commits_by_author.collect {|r| r.first}
-    commits_data = commits_by_author.collect {|r| r.last}
-    changes_data = commits_by_author.collect {|r| h[r.first] || 0}
+    #data
+    stats = repository.stats_by_author
+    fields, commits_data, changes_data = [], [], []
+    stats.each do |name, hsh|
+      fields << name
+      commits_data << hsh[:commits_count]
+      changes_data << hsh[:changes_count]
+    end
 
+    #expand to 10 values if needed
     fields = fields + [""]*(10 - fields.length) if fields.length<10
     commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
     changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
@@ -414,6 +414,7 @@ class RepositoriesController < ApplicationController
     # Remove email address in usernames
     fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
 
+    #prepare graph
     graph = SVG::Graph::BarHorizontal.new(
       :height => 30 * commits_data.length,
       :width => 800,
index 1b469c0ccd380e09e60857df45b88a623790b17f..3e5174d2af117511ae01193d496bdc7061e0bb83 100644 (file)
@@ -405,6 +405,29 @@ class Repository < ActiveRecord::Base
     new_record? && project && Repository.where(:project_id => project.id).empty?
   end
 
+  # Returns a hash with statistics by author in the following form:
+  # {
+  #   "John Smith" => { :commits => 45, :changes => 324 },
+  #   "Bob" => { ... }
+  # }
+  #
+  # Notes:
+  # - this hash honnors the users mapping defined for the repository
+  def stats_by_author
+    commits_by_author = Changeset.where("repository_id = ?", id).group(:committer).count
+    commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
+
+    changes_by_author = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", id).group(:committer).count
+    h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
+
+    commits_by_author.inject({}) do |hash, (name, commits_count)|
+      hash[name] = {}
+      hash[name][:commits_count] = commits_count
+      hash[name][:changes_count] = h[name] || 0
+      hash
+    end
+  end
+
   protected
 
   def check_default
index 8d2e7866acf4702b4c0eb04136b6c98d56c06dc6..173483c2d76bb06c605ba37c8638c5a9b38cc5af 100644 (file)
@@ -395,4 +395,23 @@ class RepositoryTest < ActiveSupport::TestCase
       [r1, r2].sort
     end
   end
+
+  def test_stats_by_author_reflect_changesets_and_changes
+    repository = Repository.find(10)
+
+    expected = {"dlopper"=>{:commits_count=>10, :changes_count=>3}}
+    assert_equal expected, repository.stats_by_author
+
+    set = Changeset.create!(
+      :repository => repository,
+      :committer => 'dlopper',
+      :committed_on => Time.now,
+      :revision => 101,
+      :comments => 'Another commit by foo.'
+    )
+    Change.create!(:changeset => set, :action => 'create', :path => '/path/to/file1')
+    Change.create!(:changeset => set, :action => 'create', :path => '/path/to/file2')
+    expected = {"dlopper"=>{:commits_count=>11, :changes_count=>5}}
+    assert_equal expected, repository.stats_by_author
+  end
 end