Refactor: extract method in WikiController#special to create a new #export method

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4251 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2010-10-13 17:13:50 +00:00
parent 718816c5d4
commit e8468b51cc
7 changed files with 47 additions and 14 deletions

View File

@ -181,16 +181,9 @@ class WikiController < ApplicationController
:order => 'title'
@pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
@pages_by_parent_id = @pages.group_by(&:parent_id)
# export wiki to a single html file
when 'export'
if User.current.allowed_to?(:export_wiki_pages, @project)
@pages = @wiki.pages.find :all, :order => 'title'
export = render_to_string :action => 'export_multiple', :layout => false
send_data(export, :type => 'text/html', :filename => "wiki.html")
else
redirect_to :action => 'index', :id => @project, :page => nil
end
return
redirect_to :action => 'export', :id => @project # Compatibility stub while refactoring
return
else
# requested special page doesn't exist, redirect to default page
redirect_to :action => 'index', :id => @project, :page => nil
@ -198,6 +191,17 @@ class WikiController < ApplicationController
end
render :action => "special_#{page_title}"
end
# Export wiki to a single html file
def export
if User.current.allowed_to?(:export_wiki_pages, @project)
@pages = @wiki.pages.find :all, :order => 'title'
export = render_to_string :action => 'export_multiple', :layout => false
send_data(export, :type => 'text/html', :filename => "wiki.html")
else
redirect_to :action => 'index', :id => @project, :page => nil
end
end
def preview
page = @wiki.find_page(params[:page])

View File

@ -24,7 +24,7 @@
<% unless @pages.empty? %>
<% other_formats_links do |f| %>
<%= f.link_to 'Atom', :url => {:controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :key => User.current.rss_key} %>
<%= f.link_to('HTML', :url => {:action => 'special', :page => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %>
<%= f.link_to('HTML', :url => {:action => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %>
<% end %>
<% end %>

View File

@ -17,7 +17,7 @@
<% unless @pages.empty? %>
<% other_formats_links do |f| %>
<%= f.link_to 'Atom', :url => {:controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :key => User.current.rss_key} %>
<%= f.link_to('HTML', :url => {:action => 'special', :page => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %>
<%= f.link_to('HTML', :url => {:action => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %>
<% end %>
<% end %>

View File

@ -29,7 +29,8 @@ ActionController::Routing::Routes.draw do |map|
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/:id/wiki/:page', :action => 'special', :page => /page_index|date_index|export/i
wiki_views.connect 'projects/:id/wiki/export', :action => 'export'
wiki_views.connect 'projects/:id/wiki/:page', :action => 'special', :page => /page_index|date_index/i
wiki_views.connect 'projects/:id/wiki/:page', :action => 'index', :page => nil
wiki_views.connect 'projects/:id/wiki/:page/edit', :action => 'edit'
wiki_views.connect 'projects/:id/wiki/:page/rename', :action => 'rename'

View File

@ -112,7 +112,7 @@ Redmine::AccessControl.map do |map|
map.permission :rename_wiki_pages, {:wiki => :rename}, :require => :member
map.permission :delete_wiki_pages, {:wiki => :destroy}, :require => :member
map.permission :view_wiki_pages, :wiki => [:index, :special]
map.permission :export_wiki_pages, {}
map.permission :export_wiki_pages, :wiki => [:export]
map.permission :view_wiki_edits, :wiki => [:history, :diff, :annotate]
map.permission :edit_wiki_pages, :wiki => [:edit, :preview, :add_attachment]
map.permission :delete_wiki_pages_attachments, {}

View File

@ -256,6 +256,34 @@ class WikiControllerTest < ActionController::TestCase
:child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
:content => 'Another page' } }
end
context "GET :export" do
context "with an authorized user to export the wiki" do
setup do
@request.session[:user_id] = 2
get :export, :id => 'ecookbook'
end
should_respond_with :success
should_assign_to :pages
should_respond_with_content_type "text/html"
should "export all of the wiki pages to a single html file" do
assert_select "a[name=?]", "CookBook_documentation"
assert_select "a[name=?]", "Another_page"
assert_select "a[name=?]", "Page_with_an_inline_image"
end
end
context "with an unauthorized user" do
setup do
get :export, :id => 'ecookbook'
should_respond_with :redirect
should_redirect_to('wiki index') { {:action => 'index', :id => @project, :page => nil} }
end
end
end
def test_not_found
get :index, :id => 999

View File

@ -321,7 +321,7 @@ class RoutingTest < ActionController::IntegrationTest
should_route :get, "/projects/567/wiki/page_index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'page_index'
should_route :get, "/projects/567/wiki/Page_Index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'Page_Index'
should_route :get, "/projects/567/wiki/date_index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'date_index'
should_route :get, "/projects/567/wiki/export", :controller => 'wiki', :action => 'special', :id => '567', :page => 'export'
should_route :get, "/projects/567/wiki/export", :controller => 'wiki', :action => 'export', :id => '567'
should_route :post, "/projects/567/wiki/my_page/edit", :controller => 'wiki', :action => 'edit', :id => '567', :page => 'my_page'
should_route :post, "/projects/567/wiki/CookBook_documentation/preview", :controller => 'wiki', :action => 'preview', :id => '567', :page => 'CookBook_documentation'