Commit 8c5003cf authored by Sato Hiroyuki's avatar Sato Hiroyuki

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.
parent e03a018d
...@@ -4,28 +4,19 @@ module Network ...@@ -4,28 +4,19 @@ module Network
class Commit class Commit
include ActionView::Helpers::TagHelper include ActionView::Helpers::TagHelper
attr_accessor :time, :spaces, :refs, :parent_spaces attr_reader :refs
attr_accessor :time, :spaces, :parent_spaces
def initialize(commit) def initialize(raw_commit, refs)
@_commit = commit @commit = ::Commit.new(raw_commit)
@time = -1 @time = -1
@spaces = [] @spaces = []
@parent_spaces = [] @parent_spaces = []
@refs = refs || []
end end
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
@_commit.send(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 ||= []
end end
def space def space
......
...@@ -2,7 +2,7 @@ require "grit" ...@@ -2,7 +2,7 @@ require "grit"
module Network module Network
class Graph class Graph
attr_accessor :days, :commits, :ref_cache, :repo attr_reader :days, :commits
def self.max_count def self.max_count
@max_count ||= 650 @max_count ||= 650
...@@ -13,7 +13,6 @@ module Network ...@@ -13,7 +13,6 @@ module Network
@ref = ref @ref = ref
@commit = commit @commit = commit
@repo = project.repo @repo = project.repo
@ref_cache = {}
@commits = collect_commits @commits = collect_commits
@days = index_commits @days = index_commits
...@@ -24,17 +23,11 @@ module Network ...@@ -24,17 +23,11 @@ module Network
# Get commits from repository # Get commits from repository
# #
def collect_commits def collect_commits
@commits = Grit::Commit.find_all(@repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
@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) }
# Decorate with app/model/network/commit.rb # Decorate with app/model/network/commit.rb
@commits.map! { |commit| Network::Commit.new(commit) } refs_cache = build_refs_cache
@commits.map! { |commit| Network::Commit.new(commit, refs_cache[commit.id]) }
# add refs to each commit
@commits.each { |commit| commit.add_refs(ref_cache, repo) }
@commits @commits
end end
...@@ -78,7 +71,7 @@ module Network ...@@ -78,7 +71,7 @@ module Network
# Skip count that the target commit is displayed in center. # Skip count that the target commit is displayed in center.
def to_commit 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| commit_index = commits.index do |c|
c.id == @commit.id c.id == @commit.id
end end
...@@ -280,5 +273,14 @@ module Network ...@@ -280,5 +273,14 @@ module Network
leaves.push(commit) leaves.push(commit)
end end
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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment