From 97a9c2293affaeeba6f448b8760bb5dffd170f2c Mon Sep 17 00:00:00 2001 From: Sato Hiroyuki Date: Sun, 14 Apr 2013 12:08:16 +0000 Subject: [PATCH] Refactor: remove dup code --- app/controllers/refs_controller.rb | 35 +++++------------------------- app/controllers/tree_controller.rb | 3 --- app/views/graph/_head.html.haml | 4 ++-- lib/extracts_path.rb | 18 ++++++++++++--- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/controllers/refs_controller.rb b/app/controllers/refs_controller.rb index 188feb73ec1..69756bb8ecd 100644 --- a/app/controllers/refs_controller.rb +++ b/app/controllers/refs_controller.rb @@ -1,24 +1,22 @@ class RefsController < ProjectResourceController + include ExtractsPath # Authorize before_filter :authorize_read_project! before_filter :authorize_code_access! before_filter :require_non_empty_project - before_filter :ref - before_filter :define_tree_vars, only: [:blob, :logs_tree] - def switch respond_to do |format| format.html do new_path = if params[:destination] == "tree" - project_tree_path(@project, (@ref + "/" + params[:path])) + project_tree_path(@project, (@id)) elsif params[:destination] == "blob" - project_blob_path(@project, (@ref + "/" + params[:path])) + project_blob_path(@project, (@id)) elsif params[:destination] == "graph" - project_graph_path(@project, @ref) + project_graph_path(@project, @id) else - project_commits_path(@project, @ref) + project_commits_path(@project, @id) end redirect_to new_path @@ -42,27 +40,4 @@ class RefsController < ProjectResourceController } end end - - protected - - def define_tree_vars - params[:path] = nil if params[:path].blank? - - @repo = project.repository - @commit = @repo.commit(@ref) - @tree = Tree.new(@repo, @commit.id, @ref, params[:path]) - @hex_path = Digest::SHA1.hexdigest(params[:path] || "") - - if params[:path] - @logs_path = logs_file_project_ref_path(@project, @ref, params[:path]) - else - @logs_path = logs_tree_project_ref_path(@project, @ref) - end - rescue - return render_404 - end - - def ref - @ref = params[:id] || params[:ref] - end end diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb index a03ea3ff30d..24e1329f926 100644 --- a/app/controllers/tree_controller.rb +++ b/app/controllers/tree_controller.rb @@ -8,9 +8,6 @@ class TreeController < ProjectResourceController before_filter :require_non_empty_project def show - @hex_path = Digest::SHA1.hexdigest(@path) - @logs_path = logs_file_project_ref_path(@project, @ref, @path) - respond_to do |format| format.html # Disable cache so browser history works diff --git a/app/views/graph/_head.html.haml b/app/views/graph/_head.html.haml index fba9a958a19..04a66892584 100644 --- a/app/views/graph/_head.html.haml +++ b/app/views/graph/_head.html.haml @@ -3,10 +3,10 @@ .clearfix .pull-left - = render partial: 'shared/ref_switcher', locals: {destination: 'graph', path: @path} + = render partial: 'shared/ref_switcher', locals: {destination: 'graph'} .search.pull-right - = form_tag project_graph_path(@project, params[:id]), method: :get do |f| + = form_tag project_graph_path(@project, @id), method: :get do |f| .control-group = label_tag :search , "Looking for commit:", class: 'control-label light' .controls diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb index 1b7c698d0a8..6025bb2a9f6 100644 --- a/lib/extracts_path.rb +++ b/lib/extracts_path.rb @@ -94,16 +94,28 @@ module ExtractsPath # Automatically renders `not_found!` if a valid tree path could not be # resolved (e.g., when a user inserts an invalid path or ref). def assign_ref_vars - @id = params[:id] + @id = get_id @ref, @path = extract_ref(@id) - @commit = @project.repository.commit(@ref) + @repo = @project.repository - @tree = Tree.new(@project.repository, @commit.id, @ref, @path) + @commit = @repo.commit(@ref) + + @tree = Tree.new(@repo, @commit.id, @ref, @path) + @hex_path = Digest::SHA1.hexdigest(@path) + @logs_path = logs_file_project_ref_path(@project, @ref, @path) raise InvalidPathError unless @tree.exists? rescue RuntimeError, NoMethodError, InvalidPathError not_found! end + + private + + def get_id + id = params[:id] || params[:ref] + id += "/" + params[:path] unless params[:path].blank? + id + end end