Commit b7e5f4556ba845cf228eb8cb70f437e2f6792c69

Authored by Sato Hiroyuki
1 parent 79cd1ca3

Refactor: Removing the duplicated code.

app/models/network/commit.rb
... ... @@ -26,5 +26,14 @@ module Network
26 26 0
27 27 end
28 28 end
  29 +
  30 + def parents(map)
  31 + @commit.parents.map do |p|
  32 + if map.include?(p.id)
  33 + map[p.id]
  34 + end
  35 + end
  36 + .compact
  37 + end
29 38 end
30 39 end
... ...
app/models/network/graph.rb
... ... @@ -2,7 +2,7 @@ require "grit"
2 2  
3 3 module Network
4 4 class Graph
5   - attr_reader :days, :commits
  5 + attr_reader :days, :commits, :map
6 6  
7 7 def self.max_count
8 8 @max_count ||= 650
... ... @@ -61,9 +61,7 @@ module Network
61 61 end
62 62  
63 63 commits_sort_by_ref.each do |commit|
64   - if @map.include? commit.id then
65   - place_chain(commit)
66   - end
  64 + place_chain(commit)
67 65 end
68 66  
69 67 # find parent spaces for not overlap lines
... ... @@ -115,25 +113,21 @@ module Network
115 113 def find_free_parent_spaces(commit)
116 114 spaces = []
117 115  
118   - commit.parents.each do |p|
119   - if @map.include?(p.id) then
120   - parent = @map[p.id]
121   -
122   - range = if commit.time < parent.time then
123   - commit.time..parent.time
124   - else
125   - parent.time..commit.time
126   - end
127   -
128   - space = if commit.space >= parent.space then
129   - find_free_parent_space(range, parent.space, -1, commit.space)
130   - else
131   - find_free_parent_space(range, commit.space, -1, parent.space)
132   - end
133   -
134   - mark_reserved(range, space)
135   - spaces << space
136   - end
  116 + commit.parents(@map).each do |parent|
  117 + range = if commit.time < parent.time then
  118 + commit.time..parent.time
  119 + else
  120 + parent.time..commit.time
  121 + end
  122 +
  123 + space = if commit.space >= parent.space then
  124 + find_free_parent_space(range, parent.space, -1, commit.space)
  125 + else
  126 + find_free_parent_space(range, commit.space, -1, parent.space)
  127 + end
  128 +
  129 + mark_reserved(range, space)
  130 + spaces << space
137 131 end
138 132  
139 133 spaces
... ... @@ -175,25 +169,18 @@ module Network
175 169 leaves.each do |l|
176 170 l.spaces << space
177 171 # Also add space to parent
178   - l.parents.each do |p|
179   - if @map.include?(p.id)
180   - parent = @map[p.id]
181   - if parent.space > 0
182   - parent.spaces << space
183   - end
  172 + l.parents(@map).each do |parent|
  173 + if parent.space > 0
  174 + parent.spaces << space
184 175 end
185 176 end
186 177 end
187 178  
188 179 # and mark it as reserved
189 180 min_time = leaves.last.time
190   - parents = leaves.last.parents.collect
191   - parents.each do |p|
192   - if @map.include? p.id
193   - parent = @map[p.id]
194   - if parent.time < min_time
195   - min_time = parent.time
196   - end
  181 + leaves.last.parents(@map).each do |parent|
  182 + if parent.time < min_time
  183 + min_time = parent.time
197 184 end
198 185 end
199 186  
... ... @@ -206,7 +193,7 @@ module Network
206 193  
207 194 # Visit branching chains
208 195 leaves.each do |l|
209   - parents = l.parents.collect.select{|p| @map.include? p.id and @map[p.id].space.zero?}
  196 + parents = l.parents(@map).select{|p| p.space.zero?}
210 197 for p in parents
211 198 place_chain(p, l.time)
212 199 end
... ... @@ -215,13 +202,10 @@ module Network
215 202  
216 203 def get_space_base(leaves)
217 204 space_base = 1
218   - if leaves.last.parents.size > 0
219   - first_parent = leaves.last.parents.first
220   - if @map.include?(first_parent.id)
221   - first_p = @map[first_parent.id]
222   - if first_p.space > 0
223   - space_base = first_p.space
224   - end
  205 + parents = leaves.last.parents(@map)
  206 + if parents.size > 0
  207 + if parents.first.space > 0
  208 + space_base = parents.first.space
225 209 end
226 210 end
227 211 space_base
... ... @@ -266,10 +250,9 @@ module Network
266 250 leaves.push(commit) if commit.space.zero?
267 251  
268 252 while true
269   - return leaves if commit.parents.count.zero?
270   - return leaves unless @map.include? commit.parents.first.id
  253 + return leaves if commit.parents(@map).count.zero?
271 254  
272   - commit = @map[commit.parents.first.id]
  255 + commit = commit.parents(@map).first
273 256  
274 257 return leaves unless commit.space.zero?
275 258  
... ...
app/views/graph/show.json.erb
... ... @@ -5,7 +5,7 @@
5 5 days: @graph.days.compact.map { |d| [d.day, d.strftime("%b")] },
6 6 commits: @graph.commits.map do |c|
7 7 {
8   - parents: parents_zip_spaces(c.parents, c.parent_spaces),
  8 + parents: parents_zip_spaces(c.parents(@graph.map), c.parent_spaces),
9 9 author: {
10 10 name: c.author.name,
11 11 email: c.author.email,
... ...