Commit 2f7f46b25655aa6f2c2a7756663c97ddb4491100

Authored by Sato Hiroyuki
1 parent 9dc46eee

Refactor: replace "render :json = graph.to_json" to view template(show.json.erb).

Because model shouldn't know about view logic.
app/controllers/graph_controller.rb
@@ -8,24 +8,21 @@ class GraphController < ProjectResourceController @@ -8,24 +8,21 @@ class GraphController < ProjectResourceController
8 before_filter :require_non_empty_project 8 before_filter :require_non_empty_project
9 9
10 def show 10 def show
11 - if params.has_key?(:q) && params[:q].blank?  
12 - redirect_to project_graph_path(@project, params[:id])  
13 - return  
14 - end  
15 -  
16 if params.has_key?(:q) 11 if params.has_key?(:q)
  12 + if params[:q].blank?
  13 + redirect_to project_graph_path(@project, params[:id])
  14 + return
  15 + end
  16 +
17 @q = params[:q] 17 @q = params[:q]
18 @commit = @project.repository.commit(@q) || @commit 18 @commit = @project.repository.commit(@q) || @commit
19 end 19 end
20 20
21 respond_to do |format| 21 respond_to do |format|
22 format.html 22 format.html
  23 +
23 format.json do 24 format.json do
24 - graph = Graph::JsonBuilder.new(project, @ref, @commit)  
25 - graph.commits.each do |c|  
26 - c.icon = gravatar_icon(c.author.email)  
27 - end  
28 - render :json => graph.to_json 25 + @graph = Graph::JsonBuilder.new(project, @ref, @commit)
29 end 26 end
30 end 27 end
31 end 28 end
app/helpers/graph_helper.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +module GraphHelper
  2 + def join_with_space(ary)
  3 + ary.collect{|r|r.name}.join(" ") unless ary.nil?
  4 + end
  5 +end
app/models/graph/commit.rb
@@ -4,7 +4,7 @@ module Graph @@ -4,7 +4,7 @@ module Graph
4 class Commit 4 class Commit
5 include ActionView::Helpers::TagHelper 5 include ActionView::Helpers::TagHelper
6 6
7 - attr_accessor :time, :spaces, :refs, :parent_spaces, :icon 7 + attr_accessor :time, :spaces, :refs, :parent_spaces
8 8
9 def initialize(commit) 9 def initialize(commit)
10 @_commit = commit 10 @_commit = commit
@@ -17,26 +17,6 @@ module Graph @@ -17,26 +17,6 @@ module Graph
17 @_commit.send(m, *args, &block) 17 @_commit.send(m, *args, &block)
18 end 18 end
19 19
20 - def to_graph_hash  
21 - h = {}  
22 - h[:parents] = self.parents.collect do |p|  
23 - [p.id,0,0]  
24 - end  
25 - h[:author] = {  
26 - name: author.name,  
27 - email: author.email,  
28 - icon: icon  
29 - }  
30 - h[:time] = time  
31 - h[:space] = spaces.first  
32 - h[:parent_spaces] = parent_spaces  
33 - h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil?  
34 - h[:id] = sha  
35 - h[:date] = date  
36 - h[:message] = message  
37 - h  
38 - end  
39 -  
40 def add_refs(ref_cache, repo) 20 def add_refs(ref_cache, repo)
41 if ref_cache.empty? 21 if ref_cache.empty?
42 repo.refs.each do |ref| 22 repo.refs.each do |ref|
app/models/graph/json_builder.rb
@@ -19,13 +19,6 @@ module Graph @@ -19,13 +19,6 @@ module Graph
19 @days = index_commits 19 @days = index_commits
20 end 20 end
21 21
22 - def to_json(*args)  
23 - {  
24 - days: @days.compact.map { |d| [d.day, d.strftime("%b")] },  
25 - commits: @commits.map(&:to_graph_hash)  
26 - }.to_json(*args)  
27 - end  
28 -  
29 protected 22 protected
30 23
31 # Get commits from repository 24 # Get commits from repository
app/views/graph/show.json.erb 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +<% self.formats = ["html"] %>
  2 +
  3 +<%= raw(
  4 + {
  5 + days: @graph.days.compact.map { |d| [d.day, d.strftime("%b")] },
  6 + commits: @graph.commits.map do |c|
  7 + {
  8 + parents: c.parents.collect do |p|
  9 + [p.id,0,0]
  10 + end,
  11 + author: {
  12 + name: c.author.name,
  13 + email: c.author.email,
  14 + icon: gravatar_icon(c.author.email, 20)
  15 + },
  16 + time: c.time,
  17 + space: c.spaces.first,
  18 + parent_spaces: c.parent_spaces,
  19 + refs: join_with_space(c.refs),
  20 + id: c.sha,
  21 + date: c.date,
  22 + message: c.message,
  23 + }
  24 + end
  25 + }.to_json
  26 +) %>