From c514dd6885af45ebb38d00ba4a8c61b1ea206d2c Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 28 Oct 2010 21:25:38 +0000 Subject: [PATCH] Refactor: convert WikiController to a REST resource git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4303 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/wiki_controller.rb | 4 +-- app/helpers/application_helper.rb | 3 +- app/views/projects/settings/_versions.rhtml | 2 +- app/views/wiki/date_index.html.erb | 2 +- app/views/wiki/edit.rhtml | 4 +-- app/views/wiki/history.rhtml | 2 +- app/views/wiki/show.rhtml | 10 +++--- config/routes.rb | 37 ++++++++------------ test/functional/wiki_controller_test.rb | 4 +-- test/integration/routing_test.rb | 3 +- test/unit/helpers/application_helper_test.rb | 4 +-- 11 files changed, 35 insertions(+), 40 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 39786341b..ae6f9d166 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -98,7 +98,7 @@ class WikiController < ApplicationController flash[:error] = l(:notice_locking_conflict) end - verify :method => :post, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } + verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } # Creates a new page or updates an existing one def update @page = @wiki.find_or_new_page(params[:id]) @@ -234,7 +234,7 @@ class WikiController < ApplicationController return render_403 unless editable? attachments = Attachment.attach_files(@page, params[:attachments]) render_attachment_warning_if_needed(@page) - redirect_to :action => 'show', :id => @page.title + redirect_to :action => 'show', :id => @page.title, :project_id => @project end private diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 12f4b6e5e..bf4e2d5ba 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -541,7 +541,8 @@ module ApplicationHelper when :local; "#{title}.html" when :anchor; "##{title}" # used for single-file wiki export else - url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => Wiki.titleize(page), :anchor => anchor) + wiki_page_id = page.present? ? Wiki.titleize(page) : nil + url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor) end link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) else diff --git a/app/views/projects/settings/_versions.rhtml b/app/views/projects/settings/_versions.rhtml index eb04d6d43..c8a5db1cf 100644 --- a/app/views/projects/settings/_versions.rhtml +++ b/app/views/projects/settings/_versions.rhtml @@ -17,7 +17,7 @@ <%=h version.description %> <%= l("version_status_#{version.status}") %> <%=h format_version_sharing(version.sharing) %> - <%= link_to(h(version.wiki_page_title), :controller => 'wiki', :id => Wiki.titleize(version.wiki_page_title)) unless version.wiki_page_title.blank? || @project.wiki.nil? %> + <%= link_to(h(version.wiki_page_title), :controller => 'wiki', :action => 'show', :id => Wiki.titleize(version.wiki_page_title), :project_id => @project) unless version.wiki_page_title.blank? || @project.wiki.nil? %> <% if version.project == @project %> <%= link_to_if_authorized l(:button_edit), {:controller => 'versions', :action => 'edit', :id => version }, :class => 'icon icon-edit' %> diff --git a/app/views/wiki/date_index.html.erb b/app/views/wiki/date_index.html.erb index 01385bc49..65af6084e 100644 --- a/app/views/wiki/date_index.html.erb +++ b/app/views/wiki/date_index.html.erb @@ -12,7 +12,7 @@

<%= format_date(date) %>

<% end %> diff --git a/app/views/wiki/edit.rhtml b/app/views/wiki/edit.rhtml index a140e2088..2615075e2 100644 --- a/app/views/wiki/edit.rhtml +++ b/app/views/wiki/edit.rhtml @@ -1,6 +1,6 @@

<%=h @page.pretty_title %>

-<% form_for :content, @content, :url => {:action => 'update', :id => @page.title}, :html => {:multipart => true, :id => 'wiki_form'} do |f| %> +<% form_for :content, @content, :url => {:action => 'update', :id => @page.title}, :html => {:method => :put, :multipart => true, :id => 'wiki_form'} do |f| %> <%= f.hidden_field :version %> <%= error_messages_for 'content' %> @@ -11,7 +11,7 @@

<%= submit_tag l(:button_save) %> <%= link_to_remote l(:label_preview), { :url => { :controller => 'wiki', :action => 'preview', :project_id => @project, :id => @page.title }, - :method => 'post', + :method => :post, :update => 'preview', :with => "Form.serialize('wiki_form')", :complete => "Element.scrollTo('preview')" diff --git a/app/views/wiki/history.rhtml b/app/views/wiki/history.rhtml index 5d44598f9..eac24705b 100644 --- a/app/views/wiki/history.rhtml +++ b/app/views/wiki/history.rhtml @@ -19,7 +19,7 @@ <% line_num = 1 %> <% @versions.each do |ver| %> "> - <%= link_to ver.version, :action => 'show', :id => @page.title, :version => ver.version %> + <%= link_to ver.version, :action => 'show', :id => @page.title, :project_id => @page.project, :version => ver.version %> <%= radio_button_tag('version', ver.version, (line_num==1), :id => "cb-#{line_num}", :onclick => "$('cbto-#{line_num+1}').checked=true;") if show_diff && (line_num < @versions.size) %> <%= radio_button_tag('version_from', ver.version, (line_num==2), :id => "cbto-#{line_num}") if show_diff && (line_num > 1) %> <%= format_time(ver.updated_on) %> diff --git a/app/views/wiki/show.rhtml b/app/views/wiki/show.rhtml index 173ac9ee2..9b2f2de28 100644 --- a/app/views/wiki/show.rhtml +++ b/app/views/wiki/show.rhtml @@ -11,15 +11,15 @@ <%= link_to_if_authorized(l(:label_history), {:action => 'history', :id => @page.title}, :class => 'icon icon-history') %> -<%= breadcrumb(@page.ancestors.reverse.collect {|parent| link_to h(parent.pretty_title), {:id => parent.title}}) %> +<%= breadcrumb(@page.ancestors.reverse.collect {|parent| link_to h(parent.pretty_title), {:id => parent.title, :project_id => parent.project}}) %> <% if @content.version != @page.content.version %>

- <%= link_to(('« ' + l(:label_previous)), :action => 'show', :id => @page.title, :version => (@content.version - 1)) + " - " if @content.version > 1 %> + <%= link_to(('« ' + l(:label_previous)), :action => 'show', :id => @page.title, :project_id => @page.project, :version => (@content.version - 1)) + " - " if @content.version > 1 %> <%= "#{l(:label_version)} #{@content.version}/#{@page.content.version}" %> - <%= '(' + link_to('diff', :controller => 'wiki', :action => 'diff', :id => @page.title, :version => @content.version) + ')' if @content.version > 1 %> - - <%= link_to((l(:label_next) + ' »'), :action => 'show', :id => @page.title, :version => (@content.version + 1)) + " - " if @content.version < @page.content.version %> - <%= link_to(l(:label_current_version), :action => 'show', :id => @page.title) %> + <%= '(' + link_to('diff', :controller => 'wiki', :action => 'diff', :id => @page.title, :project_id => @page.project, :version => @content.version) + ')' if @content.version > 1 %> - + <%= link_to((l(:label_next) + ' »'), :action => 'show', :id => @page.title, :project_id => @page.project, :version => (@content.version + 1)) + " - " if @content.version < @page.content.version %> + <%= link_to(l(:label_current_version), :action => 'show', :id => @page.title, :project_id => @page.project) %>
<%= @content.author ? @content.author.name : "anonyme" %>, <%= format_time(@content.updated_on) %>
<%=h @content.comments %> diff --git a/config/routes.rb b/config/routes.rb index 0fa76f235..b062aea94 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,27 +27,6 @@ ActionController::Routing::Routes.draw do |map| map.connect 'projects/:id/wiki', :controller => 'wikis', :action => 'edit', :conditions => {:method => :post} map.connect 'projects/:id/wiki/destroy', :controller => 'wikis', :action => 'destroy', :conditions => {:method => :get} map.connect 'projects/:id/wiki/destroy', :controller => 'wikis', :action => 'destroy', :conditions => {:method => :post} - map.with_options :controller => 'wiki' do |wiki_routes| - wiki_routes.with_options :conditions => {:method => :get} do |wiki_views| - wiki_views.connect 'projects/:project_id/wiki/export', :action => 'export' - wiki_views.connect 'projects/:project_id/wiki/index', :action => 'index' - wiki_views.connect 'projects/:project_id/wiki/date_index', :action => 'date_index' - wiki_views.connect 'projects/:project_id/wiki/:id', :action => 'show', :id => nil - wiki_views.connect 'projects/:project_id/wiki/:id/edit', :action => 'edit' - wiki_views.connect 'projects/:project_id/wiki/:id/rename', :action => 'rename' - wiki_views.connect 'projects/:project_id/wiki/:id/history', :action => 'history' - wiki_views.connect 'projects/:project_id/wiki/:id/diff/:version/vs/:version_from', :action => 'diff' - wiki_views.connect 'projects/:project_id/wiki/:id/annotate/:version', :action => 'annotate' - end - - wiki_routes.connect 'projects/:project_id/wiki/:id/:action', - :action => /rename|preview|protect|add_attachment/, - :conditions => {:method => :post} - - wiki_routes.connect 'projects/:project_id/wiki/:id/edit', :action => 'update', :conditions => {:method => :post} - - wiki_routes.connect 'projects/:project_id/wiki/:id', :action => 'destroy', :conditions => {:method => :delete} - end map.with_options :controller => 'messages' do |messages_routes| messages_routes.with_options :conditions => {:method => :get} do |messages_views| @@ -168,7 +147,21 @@ ActionController::Routing::Routes.draw do |map| project.resources :news, :shallow => true project.resources :time_entries, :controller => 'timelog', :path_prefix => 'projects/:project_id' - + project.wiki_start_page 'wiki', :controller => 'wiki', :action => 'show', :conditions => {:method => :get} + project.wiki_index 'wiki/index', :controller => 'wiki', :action => 'index', :conditions => {:method => :get} + project.wiki_diff 'wiki/:id/diff/:version/vs/:version_from', :controller => 'wiki', :action => 'diff' + project.wiki_annotate 'wiki/:id/annotate/:version', :controller => 'wiki', :action => 'annotate' + project.resources :wiki, :except => [:new, :create], :member => { + :rename => [:get, :post], + :history => :get, + :preview => :any, + :protect => :post, + :add_attachment => :post + }, :collection => { + :export => :get, + :date_index => :get + } + end # Destroy uses a get request to prompt the user before the actual DELETE request diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 5bbeba9dc..4273dddd0 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -80,7 +80,7 @@ class WikiControllerTest < ActionController::TestCase def test_create_page @request.session[:user_id] = 2 - post :update, :project_id => 1, + put :update, :project_id => 1, :id => 'New page', :content => {:comments => 'Created the page', :text => "h1. New page\n\nThis is a new page", @@ -96,7 +96,7 @@ class WikiControllerTest < ActionController::TestCase @request.session[:user_id] = 2 assert_difference 'WikiPage.count' do assert_difference 'Attachment.count' do - post :update, :project_id => 1, + put :update, :project_id => 1, :id => 'New page', :content => {:comments => 'Created the page', :text => "h1. New page\n\nThis is a new page", diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 011193ea0..4624e7867 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -322,12 +322,13 @@ class RoutingTest < ActionController::IntegrationTest should_route :get, "/projects/567/wiki/date_index", :controller => 'wiki', :action => 'date_index', :project_id => '567' should_route :get, "/projects/567/wiki/export", :controller => 'wiki', :action => 'export', :project_id => '567' - should_route :post, "/projects/567/wiki/my_page/edit", :controller => 'wiki', :action => 'update', :project_id => '567', :id => 'my_page' should_route :post, "/projects/567/wiki/CookBook_documentation/preview", :controller => 'wiki', :action => 'preview', :project_id => '567', :id => 'CookBook_documentation' should_route :post, "/projects/22/wiki/ladida/rename", :controller => 'wiki', :action => 'rename', :project_id => '22', :id => 'ladida' should_route :post, "/projects/22/wiki/ladida/protect", :controller => 'wiki', :action => 'protect', :project_id => '22', :id => 'ladida' should_route :post, "/projects/22/wiki/ladida/add_attachment", :controller => 'wiki', :action => 'add_attachment', :project_id => '22', :id => 'ladida' + should_route :put, "/projects/567/wiki/my_page", :controller => 'wiki', :action => 'update', :project_id => '567', :id => 'my_page' + should_route :delete, "/projects/22/wiki/ladida", :controller => 'wiki', :action => 'destroy', :project_id => '22', :id => 'ladida' end diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 83a508594..282a4cced 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -253,8 +253,8 @@ RAW '[[Unknown page]]' => 'Unknown page', '[[Unknown page|404]]' => '404', # link to another project wiki - '[[onlinestore:]]' => 'onlinestore', - '[[onlinestore:|Wiki]]' => 'Wiki', + '[[onlinestore:]]' => 'onlinestore', + '[[onlinestore:|Wiki]]' => 'Wiki', '[[onlinestore:Start page]]' => 'Start page', '[[onlinestore:Start page|Text]]' => 'Text', '[[onlinestore:Unknown page]]' => 'Unknown page', -- 2.39.5