From 29b0ac489b7dea0bab97b25cf34a3b2d44fcd069 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 24 Mar 2013 11:57:44 +0200 Subject: [PATCH] move edit to separate controller. This fixes #3265 --- app/controllers/edit_tree_controller.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/controllers/tree_controller.rb | 38 -------------------------------------- app/views/edit_tree/show.html.haml | 44 ++++++++++++++++++++++++++++++++++++++++++++ app/views/tree/_blob_actions.html.haml | 2 +- app/views/tree/edit.html.haml | 44 -------------------------------------------- config/routes.rb | 3 ++- 6 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 app/controllers/edit_tree_controller.rb create mode 100644 app/views/edit_tree/show.html.haml delete mode 100644 app/views/tree/edit.html.haml diff --git a/app/controllers/edit_tree_controller.rb b/app/controllers/edit_tree_controller.rb new file mode 100644 index 0000000..aae983a --- /dev/null +++ b/app/controllers/edit_tree_controller.rb @@ -0,0 +1,47 @@ +# Controller for edit a repository's file +class EditTreeController < ProjectResourceController + include ExtractsPath + + # Authorize + before_filter :authorize_read_project! + before_filter :authorize_code_access! + before_filter :require_non_empty_project + + before_filter :edit_requirements, only: [:edit, :update] + + def show + @last_commit = @project.repository.last_commit_for(@ref, @path).sha + end + + def update + edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) + updated_successfully = edit_file_action.commit!( + params[:content], + params[:commit_message], + params[:last_commit] + ) + + if updated_successfully + redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited" + else + flash[:notice] = "Your changes could not be commited, because the file has been changed" + render :edit + end + end + + private + + def edit_requirements + unless @tree.is_blob? && @tree.text? + redirect_to project_tree_path(@project, @id), notice: "You can only edit text files" + end + + allowed = if project.protected_branch? @ref + can?(current_user, :push_code_to_protected_branches, project) + else + can?(current_user, :push_code, project) + end + + return access_denied! unless allowed + end +end diff --git a/app/controllers/tree_controller.rb b/app/controllers/tree_controller.rb index 093fd5e..a03ea3f 100644 --- a/app/controllers/tree_controller.rb +++ b/app/controllers/tree_controller.rb @@ -7,8 +7,6 @@ class TreeController < ProjectResourceController before_filter :authorize_code_access! before_filter :require_non_empty_project - before_filter :edit_requirements, only: [:edit, :update] - def show @hex_path = Digest::SHA1.hexdigest(@path) @logs_path = logs_file_project_ref_path(@project, @ref, @path) @@ -19,40 +17,4 @@ class TreeController < ProjectResourceController format.js { no_cache_headers } end end - - def edit - @last_commit = @project.repository.last_commit_for(@ref, @path).sha - end - - def update - edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) - updated_successfully = edit_file_action.commit!( - params[:content], - params[:commit_message], - params[:last_commit] - ) - - if updated_successfully - redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited" - else - flash[:notice] = "Your changes could not be commited, because the file has been changed" - render :edit - end - end - - private - - def edit_requirements - unless @tree.is_blob? && @tree.text? - redirect_to project_tree_path(@project, @id), notice: "You can only edit text files" - end - - allowed = if project.protected_branch? @ref - can?(current_user, :push_code_to_protected_branches, project) - else - can?(current_user, :push_code, project) - end - - return access_denied! unless allowed - end end diff --git a/app/views/edit_tree/show.html.haml b/app/views/edit_tree/show.html.haml new file mode 100644 index 0000000..211396b --- /dev/null +++ b/app/views/edit_tree/show.html.haml @@ -0,0 +1,44 @@ +.file-editor + = form_tag(project_edit_tree_path(@project, @id), method: :put, class: "form-horizontal") do + .file_holder + .file_title + %i.icon-file + %span.file_name + = @tree.path + %small + on + %strong= @ref + %span.options + .btn-group.tree-btn-group + = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-tiny btn-cancel", confirm: "Are you sure?" + .file_content.code + %pre#editor= @tree.data + + .control-group.commit_message-group + = label_tag 'commit_message', class: "control-label" do + Commit message + .controls + = text_area_tag 'commit_message', '', placeholder: "Update #{@tree.name}", required: true, rows: 3 + .form-actions + = hidden_field_tag 'last_commit', @last_commit + = hidden_field_tag 'content', '', id: :file_content + .commit-button-annotation + = button_tag "Commit", class: 'btn commit-btn js-commit-button' + .message + to branch + %strong= @ref + = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-cancel", confirm: "Are you sure?" + +:javascript + var ace_mode = "#{@tree.language.try(:ace_mode)}"; + var editor = ace.edit("editor"); + if (ace_mode) { + editor.getSession().setMode('ace/mode/' + ace_mode); + } + + disableButtonIfEmptyField("#commit_message", ".js-commit-button"); + + $(".js-commit-button").click(function(){ + $("#file_content").val(editor.getValue()); + $(".file-editor form").submit(); + }); diff --git a/app/views/tree/_blob_actions.html.haml b/app/views/tree/_blob_actions.html.haml index 0bde968..1d55a4f 100644 --- a/app/views/tree/_blob_actions.html.haml +++ b/app/views/tree/_blob_actions.html.haml @@ -1,7 +1,7 @@ .btn-group.tree-btn-group -# only show edit link for text files - if @tree.text? - = link_to "edit", edit_project_tree_path(@project, @id), class: "btn btn-tiny", disabled: !allowed_tree_edit? + = link_to "edit", project_edit_tree_path(@project, @id), class: "btn btn-tiny", disabled: !allowed_tree_edit? = link_to "raw", project_blob_path(@project, @id), class: "btn btn-tiny", target: "_blank" -# only show normal/blame view links for text files - if @tree.text? diff --git a/app/views/tree/edit.html.haml b/app/views/tree/edit.html.haml deleted file mode 100644 index 81918e5..0000000 --- a/app/views/tree/edit.html.haml +++ /dev/null @@ -1,44 +0,0 @@ -.file-editor - = form_tag(project_tree_path(@project, @id), method: :put, class: "form-horizontal") do - .file_holder - .file_title - %i.icon-file - %span.file_name - = @tree.path - %small - on - %strong= @ref - %span.options - .btn-group.tree-btn-group - = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-tiny btn-cancel", confirm: "Are you sure?" - .file_content.code - %pre#editor= @tree.data - - .control-group.commit_message-group - = label_tag 'commit_message', class: "control-label" do - Commit message - .controls - = text_area_tag 'commit_message', '', placeholder: "Update #{@tree.name}", required: true, rows: 3 - .form-actions - = hidden_field_tag 'last_commit', @last_commit - = hidden_field_tag 'content', '', id: :file_content - .commit-button-annotation - = button_tag "Commit", class: 'btn commit-btn js-commit-button' - .message - to branch - %strong= @ref - = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-cancel", confirm: "Are you sure?" - -:javascript - var ace_mode = "#{@tree.language.try(:ace_mode)}"; - var editor = ace.edit("editor"); - if (ace_mode) { - editor.getSession().setMode('ace/mode/' + ace_mode); - } - - disableButtonIfEmptyField("#commit_message", ".js-commit-button"); - - $(".js-commit-button").click(function(){ - $("#file_content").val(editor.getValue()); - $(".file-editor form").submit(); - }); diff --git a/config/routes.rb b/config/routes.rb index 0028baf..fafaef8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -168,7 +168,8 @@ Gitlab::Application.routes.draw do # resources :projects, constraints: { id: /(?:[a-zA-Z.0-9_\-]+\/)?[a-zA-Z.0-9_\-]+/ }, except: [:new, :create, :index], path: "/" do resources :blob, only: [:show], constraints: {id: /.+/} - resources :tree, only: [:show, :edit, :update], constraints: {id: /.+/} + resources :tree, only: [:show], constraints: {id: /.+/, format: /(html|js)/ } + resources :edit_tree, only: [:show, :update], constraints: {id: /.+/}, path: 'edit' resources :commit, only: [:show], constraints: {id: /[[:alnum:]]{6,40}/} resources :commits, only: [:show], constraints: {id: /(?:[^.]|\.(?!atom$))+/, format: /atom/} resources :compare, only: [:index, :create] -- libgit2 0.21.2