Refactor: replace "render :json = graph.to_json" to view template(show.json.erb).
Because model shouldn't know about view logic.
This commit is contained in:
parent
9dc46eee8e
commit
2f7f46b256
5 changed files with 39 additions and 38 deletions
|
@ -8,24 +8,21 @@ class GraphController < ProjectResourceController
|
||||||
before_filter :require_non_empty_project
|
before_filter :require_non_empty_project
|
||||||
|
|
||||||
def show
|
def show
|
||||||
if params.has_key?(:q) && params[:q].blank?
|
|
||||||
redirect_to project_graph_path(@project, params[:id])
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if params.has_key?(:q)
|
if params.has_key?(:q)
|
||||||
|
if params[:q].blank?
|
||||||
|
redirect_to project_graph_path(@project, params[:id])
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
@q = params[:q]
|
@q = params[:q]
|
||||||
@commit = @project.repository.commit(@q) || @commit
|
@commit = @project.repository.commit(@q) || @commit
|
||||||
end
|
end
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
|
|
||||||
format.json do
|
format.json do
|
||||||
graph = Graph::JsonBuilder.new(project, @ref, @commit)
|
@graph = Graph::JsonBuilder.new(project, @ref, @commit)
|
||||||
graph.commits.each do |c|
|
|
||||||
c.icon = gravatar_icon(c.author.email)
|
|
||||||
end
|
|
||||||
render :json => graph.to_json
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
5
app/helpers/graph_helper.rb
Normal file
5
app/helpers/graph_helper.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module GraphHelper
|
||||||
|
def join_with_space(ary)
|
||||||
|
ary.collect{|r|r.name}.join(" ") unless ary.nil?
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,7 +4,7 @@ module Graph
|
||||||
class Commit
|
class Commit
|
||||||
include ActionView::Helpers::TagHelper
|
include ActionView::Helpers::TagHelper
|
||||||
|
|
||||||
attr_accessor :time, :spaces, :refs, :parent_spaces, :icon
|
attr_accessor :time, :spaces, :refs, :parent_spaces
|
||||||
|
|
||||||
def initialize(commit)
|
def initialize(commit)
|
||||||
@_commit = commit
|
@_commit = commit
|
||||||
|
@ -17,26 +17,6 @@ module Graph
|
||||||
@_commit.send(m, *args, &block)
|
@_commit.send(m, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_graph_hash
|
|
||||||
h = {}
|
|
||||||
h[:parents] = self.parents.collect do |p|
|
|
||||||
[p.id,0,0]
|
|
||||||
end
|
|
||||||
h[:author] = {
|
|
||||||
name: author.name,
|
|
||||||
email: author.email,
|
|
||||||
icon: icon
|
|
||||||
}
|
|
||||||
h[:time] = time
|
|
||||||
h[:space] = spaces.first
|
|
||||||
h[:parent_spaces] = parent_spaces
|
|
||||||
h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil?
|
|
||||||
h[:id] = sha
|
|
||||||
h[:date] = date
|
|
||||||
h[:message] = message
|
|
||||||
h
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_refs(ref_cache, repo)
|
def add_refs(ref_cache, repo)
|
||||||
if ref_cache.empty?
|
if ref_cache.empty?
|
||||||
repo.refs.each do |ref|
|
repo.refs.each do |ref|
|
||||||
|
|
|
@ -19,13 +19,6 @@ module Graph
|
||||||
@days = index_commits
|
@days = index_commits
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json(*args)
|
|
||||||
{
|
|
||||||
days: @days.compact.map { |d| [d.day, d.strftime("%b")] },
|
|
||||||
commits: @commits.map(&:to_graph_hash)
|
|
||||||
}.to_json(*args)
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Get commits from repository
|
# Get commits from repository
|
||||||
|
|
26
app/views/graph/show.json.erb
Normal file
26
app/views/graph/show.json.erb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<% self.formats = ["html"] %>
|
||||||
|
|
||||||
|
<%= raw(
|
||||||
|
{
|
||||||
|
days: @graph.days.compact.map { |d| [d.day, d.strftime("%b")] },
|
||||||
|
commits: @graph.commits.map do |c|
|
||||||
|
{
|
||||||
|
parents: c.parents.collect do |p|
|
||||||
|
[p.id,0,0]
|
||||||
|
end,
|
||||||
|
author: {
|
||||||
|
name: c.author.name,
|
||||||
|
email: c.author.email,
|
||||||
|
icon: gravatar_icon(c.author.email, 20)
|
||||||
|
},
|
||||||
|
time: c.time,
|
||||||
|
space: c.spaces.first,
|
||||||
|
parent_spaces: c.parent_spaces,
|
||||||
|
refs: join_with_space(c.refs),
|
||||||
|
id: c.sha,
|
||||||
|
date: c.date,
|
||||||
|
message: c.message,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
}.to_json
|
||||||
|
) %>
|
Loading…
Reference in a new issue