Commit 29b0ac489b7dea0bab97b25cf34a3b2d44fcd069

Authored by Dmitriy Zaporozhets
1 parent b6641d69

move edit to separate controller. This fixes #3265

app/controllers/edit_tree_controller.rb 0 → 100644
@@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
  1 +# Controller for edit a repository's file
  2 +class EditTreeController < ProjectResourceController
  3 + include ExtractsPath
  4 +
  5 + # Authorize
  6 + before_filter :authorize_read_project!
  7 + before_filter :authorize_code_access!
  8 + before_filter :require_non_empty_project
  9 +
  10 + before_filter :edit_requirements, only: [:edit, :update]
  11 +
  12 + def show
  13 + @last_commit = @project.repository.last_commit_for(@ref, @path).sha
  14 + end
  15 +
  16 + def update
  17 + edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path)
  18 + updated_successfully = edit_file_action.commit!(
  19 + params[:content],
  20 + params[:commit_message],
  21 + params[:last_commit]
  22 + )
  23 +
  24 + if updated_successfully
  25 + redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited"
  26 + else
  27 + flash[:notice] = "Your changes could not be commited, because the file has been changed"
  28 + render :edit
  29 + end
  30 + end
  31 +
  32 + private
  33 +
  34 + def edit_requirements
  35 + unless @tree.is_blob? && @tree.text?
  36 + redirect_to project_tree_path(@project, @id), notice: "You can only edit text files"
  37 + end
  38 +
  39 + allowed = if project.protected_branch? @ref
  40 + can?(current_user, :push_code_to_protected_branches, project)
  41 + else
  42 + can?(current_user, :push_code, project)
  43 + end
  44 +
  45 + return access_denied! unless allowed
  46 + end
  47 +end
app/controllers/tree_controller.rb
@@ -7,8 +7,6 @@ class TreeController &lt; ProjectResourceController @@ -7,8 +7,6 @@ class TreeController &lt; ProjectResourceController
7 before_filter :authorize_code_access! 7 before_filter :authorize_code_access!
8 before_filter :require_non_empty_project 8 before_filter :require_non_empty_project
9 9
10 - before_filter :edit_requirements, only: [:edit, :update]  
11 -  
12 def show 10 def show
13 @hex_path = Digest::SHA1.hexdigest(@path) 11 @hex_path = Digest::SHA1.hexdigest(@path)
14 @logs_path = logs_file_project_ref_path(@project, @ref, @path) 12 @logs_path = logs_file_project_ref_path(@project, @ref, @path)
@@ -19,40 +17,4 @@ class TreeController &lt; ProjectResourceController @@ -19,40 +17,4 @@ class TreeController &lt; ProjectResourceController
19 format.js { no_cache_headers } 17 format.js { no_cache_headers }
20 end 18 end
21 end 19 end
22 -  
23 - def edit  
24 - @last_commit = @project.repository.last_commit_for(@ref, @path).sha  
25 - end  
26 -  
27 - def update  
28 - edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path)  
29 - updated_successfully = edit_file_action.commit!(  
30 - params[:content],  
31 - params[:commit_message],  
32 - params[:last_commit]  
33 - )  
34 -  
35 - if updated_successfully  
36 - redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited"  
37 - else  
38 - flash[:notice] = "Your changes could not be commited, because the file has been changed"  
39 - render :edit  
40 - end  
41 - end  
42 -  
43 - private  
44 -  
45 - def edit_requirements  
46 - unless @tree.is_blob? && @tree.text?  
47 - redirect_to project_tree_path(@project, @id), notice: "You can only edit text files"  
48 - end  
49 -  
50 - allowed = if project.protected_branch? @ref  
51 - can?(current_user, :push_code_to_protected_branches, project)  
52 - else  
53 - can?(current_user, :push_code, project)  
54 - end  
55 -  
56 - return access_denied! unless allowed  
57 - end  
58 end 20 end
app/views/edit_tree/show.html.haml 0 → 100644
@@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
  1 +.file-editor
  2 + = form_tag(project_edit_tree_path(@project, @id), method: :put, class: "form-horizontal") do
  3 + .file_holder
  4 + .file_title
  5 + %i.icon-file
  6 + %span.file_name
  7 + = @tree.path
  8 + %small
  9 + on
  10 + %strong= @ref
  11 + %span.options
  12 + .btn-group.tree-btn-group
  13 + = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-tiny btn-cancel", confirm: "Are you sure?"
  14 + .file_content.code
  15 + %pre#editor= @tree.data
  16 +
  17 + .control-group.commit_message-group
  18 + = label_tag 'commit_message', class: "control-label" do
  19 + Commit message
  20 + .controls
  21 + = text_area_tag 'commit_message', '', placeholder: "Update #{@tree.name}", required: true, rows: 3
  22 + .form-actions
  23 + = hidden_field_tag 'last_commit', @last_commit
  24 + = hidden_field_tag 'content', '', id: :file_content
  25 + .commit-button-annotation
  26 + = button_tag "Commit", class: 'btn commit-btn js-commit-button'
  27 + .message
  28 + to branch
  29 + %strong= @ref
  30 + = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-cancel", confirm: "Are you sure?"
  31 +
  32 +:javascript
  33 + var ace_mode = "#{@tree.language.try(:ace_mode)}";
  34 + var editor = ace.edit("editor");
  35 + if (ace_mode) {
  36 + editor.getSession().setMode('ace/mode/' + ace_mode);
  37 + }
  38 +
  39 + disableButtonIfEmptyField("#commit_message", ".js-commit-button");
  40 +
  41 + $(".js-commit-button").click(function(){
  42 + $("#file_content").val(editor.getValue());
  43 + $(".file-editor form").submit();
  44 + });
app/views/tree/_blob_actions.html.haml
1 .btn-group.tree-btn-group 1 .btn-group.tree-btn-group
2 -# only show edit link for text files 2 -# only show edit link for text files
3 - if @tree.text? 3 - if @tree.text?
4 - = link_to "edit", edit_project_tree_path(@project, @id), class: "btn btn-tiny", disabled: !allowed_tree_edit? 4 + = link_to "edit", project_edit_tree_path(@project, @id), class: "btn btn-tiny", disabled: !allowed_tree_edit?
5 = link_to "raw", project_blob_path(@project, @id), class: "btn btn-tiny", target: "_blank" 5 = link_to "raw", project_blob_path(@project, @id), class: "btn btn-tiny", target: "_blank"
6 -# only show normal/blame view links for text files 6 -# only show normal/blame view links for text files
7 - if @tree.text? 7 - if @tree.text?
app/views/tree/edit.html.haml
@@ -1,44 +0,0 @@ @@ -1,44 +0,0 @@
1 -.file-editor  
2 - = form_tag(project_tree_path(@project, @id), method: :put, class: "form-horizontal") do  
3 - .file_holder  
4 - .file_title  
5 - %i.icon-file  
6 - %span.file_name  
7 - = @tree.path  
8 - %small  
9 - on  
10 - %strong= @ref  
11 - %span.options  
12 - .btn-group.tree-btn-group  
13 - = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-tiny btn-cancel", confirm: "Are you sure?"  
14 - .file_content.code  
15 - %pre#editor= @tree.data  
16 -  
17 - .control-group.commit_message-group  
18 - = label_tag 'commit_message', class: "control-label" do  
19 - Commit message  
20 - .controls  
21 - = text_area_tag 'commit_message', '', placeholder: "Update #{@tree.name}", required: true, rows: 3  
22 - .form-actions  
23 - = hidden_field_tag 'last_commit', @last_commit  
24 - = hidden_field_tag 'content', '', id: :file_content  
25 - .commit-button-annotation  
26 - = button_tag "Commit", class: 'btn commit-btn js-commit-button'  
27 - .message  
28 - to branch  
29 - %strong= @ref  
30 - = link_to "Cancel", project_tree_path(@project, @id), class: "btn btn-cancel", confirm: "Are you sure?"  
31 -  
32 -:javascript  
33 - var ace_mode = "#{@tree.language.try(:ace_mode)}";  
34 - var editor = ace.edit("editor");  
35 - if (ace_mode) {  
36 - editor.getSession().setMode('ace/mode/' + ace_mode);  
37 - }  
38 -  
39 - disableButtonIfEmptyField("#commit_message", ".js-commit-button");  
40 -  
41 - $(".js-commit-button").click(function(){  
42 - $("#file_content").val(editor.getValue());  
43 - $(".file-editor form").submit();  
44 - });  
config/routes.rb
@@ -168,7 +168,8 @@ Gitlab::Application.routes.draw do @@ -168,7 +168,8 @@ Gitlab::Application.routes.draw do
168 # 168 #
169 resources :projects, constraints: { id: /(?:[a-zA-Z.0-9_\-]+\/)?[a-zA-Z.0-9_\-]+/ }, except: [:new, :create, :index], path: "/" do 169 resources :projects, constraints: { id: /(?:[a-zA-Z.0-9_\-]+\/)?[a-zA-Z.0-9_\-]+/ }, except: [:new, :create, :index], path: "/" do
170 resources :blob, only: [:show], constraints: {id: /.+/} 170 resources :blob, only: [:show], constraints: {id: /.+/}
171 - resources :tree, only: [:show, :edit, :update], constraints: {id: /.+/} 171 + resources :tree, only: [:show], constraints: {id: /.+/, format: /(html|js)/ }
  172 + resources :edit_tree, only: [:show, :update], constraints: {id: /.+/}, path: 'edit'
172 resources :commit, only: [:show], constraints: {id: /[[:alnum:]]{6,40}/} 173 resources :commit, only: [:show], constraints: {id: /[[:alnum:]]{6,40}/}
173 resources :commits, only: [:show], constraints: {id: /(?:[^.]|\.(?!atom$))+/, format: /atom/} 174 resources :commits, only: [:show], constraints: {id: /(?:[^.]|\.(?!atom$))+/, format: /atom/}
174 resources :compare, only: [:index, :create] 175 resources :compare, only: [:index, :create]