Commit 8c5003cf75bf48faf88d7148a241c9e89f623c97

Authored by Sato Hiroyuki
1 parent e03a018d

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
@@ -4,28 +4,19 @@ module Network @@ -4,28 +4,19 @@ module Network
4 class Commit 4 class Commit
5 include ActionView::Helpers::TagHelper 5 include ActionView::Helpers::TagHelper
6 6
7 - attr_accessor :time, :spaces, :refs, :parent_spaces 7 + attr_reader :refs
  8 + attr_accessor :time, :spaces, :parent_spaces
8 9
9 - def initialize(commit)  
10 - @_commit = commit 10 + def initialize(raw_commit, refs)
  11 + @commit = ::Commit.new(raw_commit)
11 @time = -1 12 @time = -1
12 @spaces = [] 13 @spaces = []
13 @parent_spaces = [] 14 @parent_spaces = []
  15 + @refs = refs || []
14 end 16 end
15 17
16 def method_missing(m, *args, &block) 18 def method_missing(m, *args, &block)
17 - @_commit.send(m, *args, &block)  
18 - end  
19 -  
20 - def add_refs(ref_cache, repo)  
21 - if ref_cache.empty?  
22 - repo.refs.each do |ref|  
23 - ref_cache[ref.commit.id] ||= []  
24 - ref_cache[ref.commit.id] << ref  
25 - end  
26 - end  
27 - @refs = ref_cache[@_commit.id] if ref_cache.include?(@_commit.id)  
28 - @refs ||= [] 19 + @commit.send(m, *args, &block)
29 end 20 end
30 21
31 def space 22 def space
app/models/network/graph.rb
@@ -2,7 +2,7 @@ require &quot;grit&quot; @@ -2,7 +2,7 @@ require &quot;grit&quot;
2 2
3 module Network 3 module Network
4 class Graph 4 class Graph
5 - attr_accessor :days, :commits, :ref_cache, :repo 5 + attr_reader :days, :commits
6 6
7 def self.max_count 7 def self.max_count
8 @max_count ||= 650 8 @max_count ||= 650
@@ -13,7 +13,6 @@ module Network @@ -13,7 +13,6 @@ module Network
13 @ref = ref 13 @ref = ref
14 @commit = commit 14 @commit = commit
15 @repo = project.repo 15 @repo = project.repo
16 - @ref_cache = {}  
17 16
18 @commits = collect_commits 17 @commits = collect_commits
19 @days = index_commits 18 @days = index_commits
@@ -24,17 +23,11 @@ module Network @@ -24,17 +23,11 @@ module Network
24 # Get commits from repository 23 # Get commits from repository
25 # 24 #
26 def collect_commits 25 def collect_commits
27 -  
28 - @commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup  
29 -  
30 - # Decorate with app/models/commit.rb  
31 - @commits.map! { |commit| Commit.new(commit) } 26 + @commits = Grit::Commit.find_all(@repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
32 27
33 # Decorate with app/model/network/commit.rb 28 # Decorate with app/model/network/commit.rb
34 - @commits.map! { |commit| Network::Commit.new(commit) }  
35 -  
36 - # add refs to each commit  
37 - @commits.each { |commit| commit.add_refs(ref_cache, repo) } 29 + refs_cache = build_refs_cache
  30 + @commits.map! { |commit| Network::Commit.new(commit, refs_cache[commit.id]) }
38 31
39 @commits 32 @commits
40 end 33 end
@@ -78,7 +71,7 @@ module Network @@ -78,7 +71,7 @@ module Network
78 71
79 # Skip count that the target commit is displayed in center. 72 # Skip count that the target commit is displayed in center.
80 def to_commit 73 def to_commit
81 - commits = Grit::Commit.find_all(repo, nil, {date_order: true}) 74 + commits = Grit::Commit.find_all(@repo, nil, {date_order: true})
82 commit_index = commits.index do |c| 75 commit_index = commits.index do |c|
83 c.id == @commit.id 76 c.id == @commit.id
84 end 77 end
@@ -280,5 +273,14 @@ module Network @@ -280,5 +273,14 @@ module Network
280 leaves.push(commit) 273 leaves.push(commit)
281 end 274 end
282 end 275 end
  276 +
  277 + def build_refs_cache
  278 + refs_cache = {}
  279 + @repo.refs.each do |ref|
  280 + refs_cache[ref.commit.id] = [] unless refs_cache.include?(ref.commit.id)
  281 + refs_cache[ref.commit.id] << ref
  282 + end
  283 + refs_cache
  284 + end
283 end 285 end
284 end 286 end