Commit 97a9c2293affaeeba6f448b8760bb5dffd170f2c

Authored by Sato Hiroyuki
1 parent edcdbd67

Refactor: remove dup code

app/controllers/refs_controller.rb
1 1 class RefsController < ProjectResourceController
  2 + include ExtractsPath
2 3  
3 4 # Authorize
4 5 before_filter :authorize_read_project!
5 6 before_filter :authorize_code_access!
6 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 9 def switch
12 10 respond_to do |format|
13 11 format.html do
14 12 new_path = if params[:destination] == "tree"
15   - project_tree_path(@project, (@ref + "/" + params[:path]))
  13 + project_tree_path(@project, (@id))
16 14 elsif params[:destination] == "blob"
17   - project_blob_path(@project, (@ref + "/" + params[:path]))
  15 + project_blob_path(@project, (@id))
18 16 elsif params[:destination] == "graph"
19   - project_graph_path(@project, @ref)
  17 + project_graph_path(@project, @id)
20 18 else
21   - project_commits_path(@project, @ref)
  19 + project_commits_path(@project, @id)
22 20 end
23 21  
24 22 redirect_to new_path
... ... @@ -42,27 +40,4 @@ class RefsController &lt; ProjectResourceController
42 40 }
43 41 end
44 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 43 end
... ...
app/controllers/tree_controller.rb
... ... @@ -8,9 +8,6 @@ class TreeController &lt; ProjectResourceController
8 8 before_filter :require_non_empty_project
9 9  
10 10 def show
11   - @hex_path = Digest::SHA1.hexdigest(@path)
12   - @logs_path = logs_file_project_ref_path(@project, @ref, @path)
13   -
14 11 respond_to do |format|
15 12 format.html
16 13 # Disable cache so browser history works
... ...
app/views/graph/_head.html.haml
... ... @@ -3,10 +3,10 @@
3 3  
4 4 .clearfix
5 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 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 10 .control-group
11 11 = label_tag :search , "Looking for commit:", class: 'control-label light'
12 12 .controls
... ...
lib/extracts_path.rb
... ... @@ -94,16 +94,28 @@ module ExtractsPath
94 94 # Automatically renders `not_found!` if a valid tree path could not be
95 95 # resolved (e.g., when a user inserts an invalid path or ref).
96 96 def assign_ref_vars
97   - @id = params[:id]
  97 + @id = get_id
98 98  
99 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 109 raise InvalidPathError unless @tree.exists?
106 110 rescue RuntimeError, NoMethodError, InvalidPathError
107 111 not_found!
108 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 121 end
... ...