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 4 class Commit
5 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 12 @time = -1
12 13 @spaces = []
13 14 @parent_spaces = []
  15 + @refs = refs || []
14 16 end
15 17  
16 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 20 end
30 21  
31 22 def space
... ...
app/models/network/graph.rb
... ... @@ -2,7 +2,7 @@ require &quot;grit&quot;
2 2  
3 3 module Network
4 4 class Graph
5   - attr_accessor :days, :commits, :ref_cache, :repo
  5 + attr_reader :days, :commits
6 6  
7 7 def self.max_count
8 8 @max_count ||= 650
... ... @@ -13,7 +13,6 @@ module Network
13 13 @ref = ref
14 14 @commit = commit
15 15 @repo = project.repo
16   - @ref_cache = {}
17 16  
18 17 @commits = collect_commits
19 18 @days = index_commits
... ... @@ -24,17 +23,11 @@ module Network
24 23 # Get commits from repository
25 24 #
26 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 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 32 @commits
40 33 end
... ... @@ -78,7 +71,7 @@ module Network
78 71  
79 72 # Skip count that the target commit is displayed in center.
80 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 75 commit_index = commits.index do |c|
83 76 c.id == @commit.id
84 77 end
... ... @@ -280,5 +273,14 @@ module Network
280 273 leaves.push(commit)
281 274 end
282 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 285 end
284 286 end
... ...