From 001b255b081bec908851ac7e8d715f1bb6e61c03 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 5 Feb 2012 14:35:27 +0000 Subject: [PATCH] Ability to edit a wiki page's parent on the edit page (#6449). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8787 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/wiki_controller.rb | 13 +++--- app/models/wiki_page.rb | 5 +++ app/views/wiki/edit.html.erb | 9 +++- test/functional/wiki_controller_test.rb | 56 +++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 079c44ff3..28d0d44b1 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -123,6 +123,7 @@ class WikiController < ApplicationController def update return render_403 unless editable? @page.content = WikiContent.new(:page => @page) if @page.new_record? + @page.safe_attributes = params[:wiki_page] @content = @page.content_for_version(params[:version]) @content.text = initial_page_content(@page) if @content.text.blank? @@ -132,11 +133,12 @@ class WikiController < ApplicationController if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text] attachments = Attachment.attach_files(@page, params[:attachments]) render_attachment_warning_if_needed(@page) - # don't save if text wasn't changed + # don't save content if text wasn't changed + @page.save redirect_to :action => 'show', :project_id => @project, :id => @page.title return end - + @content.comments = params[:content][:comments] @text = params[:content][:text] if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? @@ -148,11 +150,8 @@ class WikiController < ApplicationController @content.text = @text end @content.author = User.current - if @page.new_record? && params[:page] - @page.parent_id = params[:page][:parent_id] - end - # if page is new @page.save will also save content, but not if page isn't a new record - if (@page.new_record? ? @page.save : @content.save) + @page.content = @content + if @page.save attachments = Attachment.attach_files(@page, params[:attachments]) render_attachment_warning_if_needed(@page) call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 5508bd218..6088e48ca 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -19,6 +19,8 @@ require 'diff' require 'enumerator' class WikiPage < ActiveRecord::Base + include Redmine::SafeAttributes + belongs_to :wiki has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy acts_as_attachable :delete_permission => :delete_wiki_pages_attachments @@ -55,6 +57,9 @@ class WikiPage < ActiveRecord::Base # Wiki pages that are protected by default DEFAULT_PROTECTED_PAGES = %w(sidebar) + safe_attributes 'parent_id', + :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)} + def initialize(attributes=nil, *args) super if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase) diff --git a/app/views/wiki/edit.html.erb b/app/views/wiki/edit.html.erb index 00d243a13..3bee3dcc8 100644 --- a/app/views/wiki/edit.html.erb +++ b/app/views/wiki/edit.html.erb @@ -13,8 +13,13 @@
<%= text_area_tag 'content[text]', @text, :cols => 100, :rows => 25, :class => 'wiki-edit', :accesskey => accesskey(:edit) %> -<% if @page.new_record? && @page.parent %> -

<%=h @page.parent.pretty_title %>

+<% if @page.safe_attribute_names.include?('parent_id') && @wiki.pages.any? %> + <% fields_for @page do |fp| %> +

+ + <%= fp.select :parent_id, "" + wiki_page_options_for_select(@wiki.pages.all(:include => :parent) - @page.self_and_descendants, @page.parent) %> +

+ <% end %> <% end %>

<%= f.text_field :comments, :size => 120 %>

diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 2b3cc37fb..733170ab6 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -124,15 +124,15 @@ class WikiControllerTest < ActionController::TestCase get :show, :project_id => 1, :id => 'Unexistent page' assert_response :success assert_template 'edit' - assert_no_tag 'input', :attributes => {:name => 'page[parent_id]'} end - def test_show_unexistent_page_with_parent + def test_show_unexistent_page_with_parent_should_preselect_parent @request.session[:user_id] = 2 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page' assert_response :success assert_template 'edit' - assert_tag 'input', :attributes => {:name => 'page[parent_id]', :value => '2'} + assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'}, + :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}} end def test_show_should_not_show_history_without_permission @@ -183,7 +183,7 @@ class WikiControllerTest < ActionController::TestCase assert_difference 'WikiPage.count' do put :update, :project_id => 1, :id => 'New page', :content => {:text => "h1. New page\n\nThis is a new page", :version => 0}, - :page => {:parent_id => 2} + :wiki_page => {:parent_id => 2} end page = Project.find(1).wiki.find_page('New page') assert_equal WikiPage.find(2), page.parent @@ -250,6 +250,31 @@ class WikiControllerTest < ActionController::TestCase assert_equal "my comments", page.content.comments end + def test_update_page_with_parent + @request.session[:user_id] = 2 + assert_no_difference 'WikiPage.count' do + assert_no_difference 'WikiContent.count' do + assert_difference 'WikiContent::Version.count' do + put :update, :project_id => 1, + :id => 'Another_page', + :content => { + :comments => "my comments", + :text => "edited", + :version => 1 + }, + :wiki_page => {:parent_id => '1'} + end + end + end + assert_redirected_to '/projects/ecookbook/wiki/Another_page' + + page = Wiki.find(1).pages.find_by_title('Another_page') + assert_equal "edited", page.content.text + assert_equal 2, page.content.version + assert_equal "my comments", page.content.comments + assert_equal WikiPage.find(1), page.parent + end + def test_update_page_with_failure @request.session[:user_id] = 2 assert_no_difference 'WikiPage.count' do @@ -273,6 +298,27 @@ class WikiControllerTest < ActionController::TestCase assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'} end + def test_update_page_with_parent_change_only_should_not_create_content_version + @request.session[:user_id] = 2 + assert_no_difference 'WikiPage.count' do + assert_no_difference 'WikiContent.count' do + assert_no_difference 'WikiContent::Version.count' do + put :update, :project_id => 1, + :id => 'Another_page', + :content => { + :comments => '', + :text => Wiki.find(1).find_page('Another_page').content.text, + :version => 1 + }, + :wiki_page => {:parent_id => '1'} + end + end + end + page = Wiki.find(1).pages.find_by_title('Another_page') + assert_equal 1, page.content.version + assert_equal WikiPage.find(1), page.parent + end + def test_update_page_with_attachments_only_should_not_create_content_version @request.session[:user_id] = 2 assert_no_difference 'WikiPage.count' do @@ -291,6 +337,8 @@ class WikiControllerTest < ActionController::TestCase end end end + page = Wiki.find(1).pages.find_by_title('Another_page') + assert_equal 1, page.content.version end def test_update_stale_page_should_not_raise_an_error