Commit 97a9c2293affaeeba6f448b8760bb5dffd170f2c

Authored by Sato Hiroyuki
1 parent edcdbd67

Refactor: remove dup code

app/controllers/refs_controller.rb
1 class RefsController < ProjectResourceController 1 class RefsController < ProjectResourceController
  2 + include ExtractsPath
2 3
3 # Authorize 4 # Authorize
4 before_filter :authorize_read_project! 5 before_filter :authorize_read_project!
5 before_filter :authorize_code_access! 6 before_filter :authorize_code_access!
6 before_filter :require_non_empty_project 7 before_filter :require_non_empty_project
7 8
8 - before_filter :ref  
9 - before_filter :define_tree_vars, only: [:blob, :logs_tree]  
10 -  
11 def switch 9 def switch
12 respond_to do |format| 10 respond_to do |format|
13 format.html do 11 format.html do
14 new_path = if params[:destination] == "tree" 12 new_path = if params[:destination] == "tree"
15 - project_tree_path(@project, (@ref + "/" + params[:path])) 13 + project_tree_path(@project, (@id))
16 elsif params[:destination] == "blob" 14 elsif params[:destination] == "blob"
17 - project_blob_path(@project, (@ref + "/" + params[:path])) 15 + project_blob_path(@project, (@id))
18 elsif params[:destination] == "graph" 16 elsif params[:destination] == "graph"
19 - project_graph_path(@project, @ref) 17 + project_graph_path(@project, @id)
20 else 18 else
21 - project_commits_path(@project, @ref) 19 + project_commits_path(@project, @id)
22 end 20 end
23 21
24 redirect_to new_path 22 redirect_to new_path
@@ -42,27 +40,4 @@ class RefsController &lt; ProjectResourceController @@ -42,27 +40,4 @@ class RefsController &lt; ProjectResourceController
42 } 40 }
43 end 41 end
44 end 42 end
45 -  
46 - protected  
47 -  
48 - def define_tree_vars  
49 - params[:path] = nil if params[:path].blank?  
50 -  
51 - @repo = project.repository  
52 - @commit = @repo.commit(@ref)  
53 - @tree = Tree.new(@repo, @commit.id, @ref, params[:path])  
54 - @hex_path = Digest::SHA1.hexdigest(params[:path] || "")  
55 -  
56 - if params[:path]  
57 - @logs_path = logs_file_project_ref_path(@project, @ref, params[:path])  
58 - else  
59 - @logs_path = logs_tree_project_ref_path(@project, @ref)  
60 - end  
61 - rescue  
62 - return render_404  
63 - end  
64 -  
65 - def ref  
66 - @ref = params[:id] || params[:ref]  
67 - end  
68 end 43 end
app/controllers/tree_controller.rb
@@ -8,9 +8,6 @@ class TreeController &lt; ProjectResourceController @@ -8,9 +8,6 @@ class TreeController &lt; ProjectResourceController
8 before_filter :require_non_empty_project 8 before_filter :require_non_empty_project
9 9
10 def show 10 def show
11 - @hex_path = Digest::SHA1.hexdigest(@path)  
12 - @logs_path = logs_file_project_ref_path(@project, @ref, @path)  
13 -  
14 respond_to do |format| 11 respond_to do |format|
15 format.html 12 format.html
16 # Disable cache so browser history works 13 # Disable cache so browser history works
app/views/graph/_head.html.haml
@@ -3,10 +3,10 @@ @@ -3,10 +3,10 @@
3 3
4 .clearfix 4 .clearfix
5 .pull-left 5 .pull-left
6 - = render partial: 'shared/ref_switcher', locals: {destination: 'graph', path: @path} 6 + = render partial: 'shared/ref_switcher', locals: {destination: 'graph'}
7 7
8 .search.pull-right 8 .search.pull-right
9 - = form_tag project_graph_path(@project, params[:id]), method: :get do |f| 9 + = form_tag project_graph_path(@project, @id), method: :get do |f|
10 .control-group 10 .control-group
11 = label_tag :search , "Looking for commit:", class: 'control-label light' 11 = label_tag :search , "Looking for commit:", class: 'control-label light'
12 .controls 12 .controls
lib/extracts_path.rb
@@ -94,16 +94,28 @@ module ExtractsPath @@ -94,16 +94,28 @@ module ExtractsPath
94 # Automatically renders `not_found!` if a valid tree path could not be 94 # Automatically renders `not_found!` if a valid tree path could not be
95 # resolved (e.g., when a user inserts an invalid path or ref). 95 # resolved (e.g., when a user inserts an invalid path or ref).
96 def assign_ref_vars 96 def assign_ref_vars
97 - @id = params[:id] 97 + @id = get_id
98 98
99 @ref, @path = extract_ref(@id) 99 @ref, @path = extract_ref(@id)
100 100
101 - @commit = @project.repository.commit(@ref) 101 + @repo = @project.repository
102 102
103 - @tree = Tree.new(@project.repository, @commit.id, @ref, @path) 103 + @commit = @repo.commit(@ref)
  104 +
  105 + @tree = Tree.new(@repo, @commit.id, @ref, @path)
  106 + @hex_path = Digest::SHA1.hexdigest(@path)
  107 + @logs_path = logs_file_project_ref_path(@project, @ref, @path)
104 108
105 raise InvalidPathError unless @tree.exists? 109 raise InvalidPathError unless @tree.exists?
106 rescue RuntimeError, NoMethodError, InvalidPathError 110 rescue RuntimeError, NoMethodError, InvalidPathError
107 not_found! 111 not_found!
108 end 112 end
  113 +
  114 + private
  115 +
  116 + def get_id
  117 + id = params[:id] || params[:ref]
  118 + id += "/" + params[:path] unless params[:path].blank?
  119 + id
  120 + end
109 end 121 end