Commit 398ba6f1bb60f176444dedc7b26188e08b920f54
1 parent
576cec6c
Exists in
master
and in
4 other branches
DRY up Blame, Blob and Tree controllers
Showing
6 changed files
with
38 additions
and
73 deletions
Show diff stats
app/controllers/blame_controller.rb
| 1 | # Controller for viewing a file's blame | 1 | # Controller for viewing a file's blame |
| 2 | class BlameController < ApplicationController | 2 | class BlameController < ApplicationController |
| 3 | - # Thrown when given an invalid path | ||
| 4 | - class InvalidPathError < StandardError; end | ||
| 5 | 3 | ||
| 6 | include RefExtractor | 4 | include RefExtractor |
| 7 | 5 | ||
| @@ -15,31 +13,10 @@ class BlameController < ApplicationController | @@ -15,31 +13,10 @@ class BlameController < ApplicationController | ||
| 15 | before_filter :authorize_code_access! | 13 | before_filter :authorize_code_access! |
| 16 | before_filter :require_non_empty_project | 14 | before_filter :require_non_empty_project |
| 17 | 15 | ||
| 18 | - before_filter :define_tree_vars | 16 | + before_filter :assign_ref_vars |
| 19 | 17 | ||
| 20 | def show | 18 | def show |
| 19 | + @repo = @project.repo | ||
| 21 | @blame = Grit::Blob.blame(@repo, @commit.id, @path) | 20 | @blame = Grit::Blob.blame(@repo, @commit.id, @path) |
| 22 | end | 21 | end |
| 23 | - | ||
| 24 | - private | ||
| 25 | - | ||
| 26 | - def define_tree_vars | ||
| 27 | - @ref, @path = extract_ref(params[:id]) | ||
| 28 | - | ||
| 29 | - @id = File.join(@ref, @path) | ||
| 30 | - @repo = @project.repo | ||
| 31 | - @commit = CommitDecorator.decorate(@project.commit(@ref)) | ||
| 32 | - | ||
| 33 | - @tree = Tree.new(@commit.tree, @project, @ref, @path) | ||
| 34 | - @tree = TreeDecorator.new(@tree) | ||
| 35 | - | ||
| 36 | - raise InvalidPathError if @tree.invalid? | ||
| 37 | - | ||
| 38 | - @hex_path = Digest::SHA1.hexdigest(@path) | ||
| 39 | - | ||
| 40 | - @history_path = project_tree_path(@project, @id) | ||
| 41 | - @logs_path = logs_file_project_ref_path(@project, @ref, @path) | ||
| 42 | - rescue NoMethodError, InvalidPathError | ||
| 43 | - not_found! | ||
| 44 | - end | ||
| 45 | end | 22 | end |
app/controllers/blob_controller.rb
| @@ -16,7 +16,7 @@ class BlobController < ApplicationController | @@ -16,7 +16,7 @@ class BlobController < ApplicationController | ||
| 16 | before_filter :authorize_code_access! | 16 | before_filter :authorize_code_access! |
| 17 | before_filter :require_non_empty_project | 17 | before_filter :require_non_empty_project |
| 18 | 18 | ||
| 19 | - before_filter :define_tree_vars | 19 | + before_filter :assign_ref_vars |
| 20 | 20 | ||
| 21 | def show | 21 | def show |
| 22 | if @tree.is_blob? | 22 | if @tree.is_blob? |
| @@ -37,26 +37,4 @@ class BlobController < ApplicationController | @@ -37,26 +37,4 @@ class BlobController < ApplicationController | ||
| 37 | not_found! | 37 | not_found! |
| 38 | end | 38 | end |
| 39 | end | 39 | end |
| 40 | - | ||
| 41 | - private | ||
| 42 | - | ||
| 43 | - def define_tree_vars | ||
| 44 | - @ref, @path = extract_ref(params[:id]) | ||
| 45 | - | ||
| 46 | - @id = File.join(@ref, @path) | ||
| 47 | - @repo = @project.repo | ||
| 48 | - @commit = CommitDecorator.decorate(@project.commit(@ref)) | ||
| 49 | - | ||
| 50 | - @tree = Tree.new(@commit.tree, @project, @ref, @path) | ||
| 51 | - @tree = TreeDecorator.new(@tree) | ||
| 52 | - | ||
| 53 | - raise InvalidPathError if @tree.invalid? | ||
| 54 | - | ||
| 55 | - @hex_path = Digest::SHA1.hexdigest(@path) | ||
| 56 | - | ||
| 57 | - @history_path = project_tree_path(@project, @id) | ||
| 58 | - @logs_path = logs_file_project_ref_path(@project, @ref, @path) | ||
| 59 | - rescue NoMethodError, InvalidPathError | ||
| 60 | - not_found! | ||
| 61 | - end | ||
| 62 | end | 40 | end |
app/controllers/tree_controller.rb
| @@ -15,35 +15,18 @@ class TreeController < ApplicationController | @@ -15,35 +15,18 @@ class TreeController < ApplicationController | ||
| 15 | before_filter :authorize_code_access! | 15 | before_filter :authorize_code_access! |
| 16 | before_filter :require_non_empty_project | 16 | before_filter :require_non_empty_project |
| 17 | 17 | ||
| 18 | - before_filter :define_tree_vars | 18 | + before_filter :assign_ref_vars |
| 19 | 19 | ||
| 20 | def show | 20 | def show |
| 21 | + @hex_path = Digest::SHA1.hexdigest(@path) | ||
| 22 | + | ||
| 23 | + @history_path = project_tree_path(@project, @id) | ||
| 24 | + @logs_path = logs_file_project_ref_path(@project, @ref, @path) | ||
| 25 | + | ||
| 21 | respond_to do |format| | 26 | respond_to do |format| |
| 22 | format.html | 27 | format.html |
| 23 | # Disable cache so browser history works | 28 | # Disable cache so browser history works |
| 24 | format.js { no_cache_headers } | 29 | format.js { no_cache_headers } |
| 25 | end | 30 | end |
| 26 | end | 31 | end |
| 27 | - | ||
| 28 | - private | ||
| 29 | - | ||
| 30 | - def define_tree_vars | ||
| 31 | - @ref, @path = extract_ref(params[:id]) | ||
| 32 | - | ||
| 33 | - @id = File.join(@ref, @path) | ||
| 34 | - @repo = @project.repo | ||
| 35 | - @commit = CommitDecorator.decorate(@project.commit(@ref)) | ||
| 36 | - | ||
| 37 | - @tree = Tree.new(@commit.tree, @project, @ref, @path.gsub(/^\//, '')) | ||
| 38 | - @tree = TreeDecorator.new(@tree) | ||
| 39 | - | ||
| 40 | - raise InvalidPathError if @tree.invalid? | ||
| 41 | - | ||
| 42 | - @hex_path = Digest::SHA1.hexdigest(@path) | ||
| 43 | - | ||
| 44 | - @history_path = project_tree_path(@project, @id) | ||
| 45 | - @logs_path = logs_file_project_ref_path(@project, @ref, @path) | ||
| 46 | - rescue NoMethodError, InvalidPathError | ||
| 47 | - not_found! | ||
| 48 | - end | ||
| 49 | end | 32 | end |
app/views/tree/show.html.haml
app/views/tree/show.js.haml
| 1 | :plain | 1 | :plain |
| 2 | // Load Files list | 2 | // Load Files list |
| 3 | - $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {repo: @repo, commit: @commit, tree: @tree}))}"); | 3 | + $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {commit: @commit, tree: @tree}))}"); |
| 4 | $("#tree-content-holder").show("slide", { direction: "right" }, 150); | 4 | $("#tree-content-holder").show("slide", { direction: "right" }, 150); |
| 5 | $('.project-refs-form #path').val("#{@path}"); | 5 | $('.project-refs-form #path').val("#{@path}"); |
| 6 | 6 |
lib/ref_extractor.rb
| @@ -67,4 +67,31 @@ module RefExtractor | @@ -67,4 +67,31 @@ module RefExtractor | ||
| 67 | 67 | ||
| 68 | pair | 68 | pair |
| 69 | end | 69 | end |
| 70 | + | ||
| 71 | + # Assigns common instance variables for views working with Git tree-ish objects | ||
| 72 | + # | ||
| 73 | + # Assignments are: | ||
| 74 | + # | ||
| 75 | + # - @id - A string representing the joined ref and path | ||
| 76 | + # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) | ||
| 77 | + # - @path - A string representing the filesystem path | ||
| 78 | + # - @commit - A CommitDecorator representing the commit from the given ref | ||
| 79 | + # - @tree - A TreeDecorator representing the tree at the given ref/path | ||
| 80 | + # | ||
| 81 | + # Automatically renders `not_found!` if a valid tree could not be resolved | ||
| 82 | + # (e.g., when a user inserts an invalid path or ref). | ||
| 83 | + def assign_ref_vars | ||
| 84 | + @ref, @path = extract_ref(params[:id]) | ||
| 85 | + | ||
| 86 | + @id = File.join(@ref, @path) | ||
| 87 | + | ||
| 88 | + @commit = CommitDecorator.decorate(@project.commit(@ref)) | ||
| 89 | + | ||
| 90 | + @tree = Tree.new(@commit.tree, @project, @ref, @path) | ||
| 91 | + @tree = TreeDecorator.new(@tree) | ||
| 92 | + | ||
| 93 | + raise InvalidPathError if @tree.invalid? | ||
| 94 | + rescue NoMethodError, InvalidPathError | ||
| 95 | + not_found! | ||
| 96 | + end | ||
| 70 | end | 97 | end |