From 8c5003cf75bf48faf88d7148a241c9e89f623c97 Mon Sep 17 00:00:00 2001
From: Sato Hiroyuki <sathiroyuki@gmail.com>
Date: Thu, 7 Mar 2013 17:56:01 +0900
Subject: [PATCH] Refactor: clean up models.

* Network::Commit
** Removing unnecessary accessors.
** Removing add_refs methods.
* Network::Graph
** Removing unnecessary accessors.
** The 3 times loop of commits don't need.
---
 app/models/network/commit.rb | 21 ++++++---------------
 app/models/network/graph.rb  | 26 ++++++++++++++------------
 2 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/app/models/network/commit.rb b/app/models/network/commit.rb
index 9c0a99ccd41..27c8cc97284 100644
--- a/app/models/network/commit.rb
+++ b/app/models/network/commit.rb
@@ -4,28 +4,19 @@ module Network
   class Commit
     include ActionView::Helpers::TagHelper
 
-    attr_accessor :time, :spaces, :refs, :parent_spaces
+    attr_reader :refs
+    attr_accessor :time, :spaces, :parent_spaces
 
-    def initialize(commit)
-      @_commit = commit
+    def initialize(raw_commit, refs)
+      @commit = ::Commit.new(raw_commit)
       @time = -1
       @spaces = []
       @parent_spaces = []
+      @refs = refs || []
     end
 
     def method_missing(m, *args, &block)
-      @_commit.send(m, *args, &block)
-    end
-
-    def add_refs(ref_cache, repo)
-      if ref_cache.empty?
-        repo.refs.each do |ref|
-          ref_cache[ref.commit.id] ||= []
-          ref_cache[ref.commit.id] << ref
-        end
-      end
-      @refs = ref_cache[@_commit.id] if ref_cache.include?(@_commit.id)
-      @refs ||= []
+      @commit.send(m, *args, &block)
     end
 
     def space
diff --git a/app/models/network/graph.rb b/app/models/network/graph.rb
index c9ecbc910e8..f130bffca14 100644
--- a/app/models/network/graph.rb
+++ b/app/models/network/graph.rb
@@ -2,7 +2,7 @@ require "grit"
 
 module Network
   class Graph
-    attr_accessor :days, :commits, :ref_cache, :repo
+    attr_reader :days, :commits
 
     def self.max_count
       @max_count ||= 650
@@ -13,7 +13,6 @@ module Network
       @ref = ref
       @commit = commit
       @repo = project.repo
-      @ref_cache = {}
 
       @commits = collect_commits
       @days = index_commits
@@ -24,17 +23,11 @@ module Network
     # Get commits from repository
     #
     def collect_commits
-
-      @commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
-
-      # Decorate with app/models/commit.rb
-      @commits.map! { |commit| Commit.new(commit) }
+      @commits = Grit::Commit.find_all(@repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
 
       # Decorate with app/model/network/commit.rb
-      @commits.map! { |commit| Network::Commit.new(commit) }
-
-      # add refs to each commit
-      @commits.each { |commit| commit.add_refs(ref_cache, repo) }
+      refs_cache = build_refs_cache
+      @commits.map! { |commit| Network::Commit.new(commit, refs_cache[commit.id]) }
 
       @commits
     end
@@ -78,7 +71,7 @@ module Network
 
     # Skip count that the target commit is displayed in center.
     def to_commit
-      commits = Grit::Commit.find_all(repo, nil, {date_order: true})
+      commits = Grit::Commit.find_all(@repo, nil, {date_order: true})
       commit_index = commits.index do |c|
         c.id == @commit.id
       end
@@ -280,5 +273,14 @@ module Network
         leaves.push(commit)
       end
     end
+
+    def build_refs_cache
+      refs_cache = {}
+      @repo.refs.each do |ref|
+        refs_cache[ref.commit.id] = [] unless refs_cache.include?(ref.commit.id)
+        refs_cache[ref.commit.id] << ref
+      end
+      refs_cache
+    end
   end
 end
-- 
GitLab