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.
This commit is contained in:
Sato Hiroyuki 2013-03-07 17:56:01 +09:00
parent e03a018d28
commit 8c5003cf75
2 changed files with 20 additions and 27 deletions

View file

@ -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

View file

@ -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