diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 60b59ff5f00b25ed19c47f7d68ae92318b882185..55194e0487dd5ef4538e6df14dd1eeba94c50b33 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -33,7 +33,7 @@ class WikiController < ApplicationController page_title = params[:page] @page = @wiki.find_or_new_page(page_title) if @page.new_record? - if User.current.allowed_to?(:edit_wiki_pages, @project) + if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? edit render :action => 'edit' else diff --git a/app/models/wiki.rb b/app/models/wiki.rb index b9a76fb32440fc4a007b8afac49a7752b16b7ecf..9bd24595599981ff7ef172636e786664ef48ff83 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -29,6 +29,12 @@ class Wiki < ActiveRecord::Base !user.nil? && user.allowed_to?(:view_wiki_pages, project) end + # Returns the wiki page that acts as the sidebar content + # or nil if no such page exists + def sidebar + @sidebar ||= find_page('Sidebar', :with_redirect => false) + end + # find the page with the given title # if page doesn't exist, return a new page def find_or_new_page(title) diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb index f81aa9e7836e52c8ecd19c452a911539130fc99e..1f0eb9a62f8cce300e516287822f8ef52942fdac 100644 --- a/app/models/wiki_content.rb +++ b/app/models/wiki_content.rb @@ -34,6 +34,10 @@ class WikiContent < ActiveRecord::Base page.project end + def attachments + page.nil? ? [] : page.attachments + end + # Returns the mail adresses of users that should be notified def recipients notified = project.notified_users diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index ad2d8135a76a83610d037dd20067e7ea1460d29b..010a8532c30f4eada0b87ff5e0f2407dacc953a3 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -41,6 +41,15 @@ class WikiPage < ActiveRecord::Base validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false validates_associated :content + # Wiki pages that are protected by default + DEFAULT_PROTECTED_PAGES = %w(sidebar) + + def after_initialize + if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase) + self.protected = true + end + end + def visible?(user=User.current) !user.nil? && user.allowed_to?(:view_wiki_pages, project) end diff --git a/app/views/wiki/_sidebar.rhtml b/app/views/wiki/_sidebar.rhtml index 20c08712372436e4aedda73a3c6d0912133d8403..ad4b1d2505d51493c33c9c8a2180d03cbda25e02 100644 --- a/app/views/wiki/_sidebar.rhtml +++ b/app/views/wiki/_sidebar.rhtml @@ -1,3 +1,7 @@ +<% if @wiki && @wiki.sidebar -%> + <%= textilizable @wiki.sidebar.content, :text %> +<% end -%> + <h3><%= l(:label_wiki) %></h3> <%= link_to l(:field_start_page), {:action => 'index', :page => nil} %><br /> diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 3f9ac7eacee630de4f35d3543207681d222f16ec..298cfee9ca3f10fd6ace02d3f2644a3554efc91a 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -70,6 +70,17 @@ class WikiControllerTest < ActionController::TestCase :alt => 'This is a logo' } end + def test_show_with_sidebar + page = Project.find(1).wiki.pages.new(:title => 'Sidebar') + page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar') + page.save! + + get :index, :id => 1, :page => 'Another_page' + assert_response :success + assert_tag :tag => 'div', :attributes => {:id => 'sidebar'}, + :content => /Side bar content for test_show_with_sidebar/ + end + def test_show_unexistent_page_without_edit_right get :index, :id => 1, :page => 'Unexistent page' assert_response 404 diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 5c0def179d9ab5bb8a5fdac4e6c4986b9342e664..c8fd1b29824dfdb02bdc1c1ec1e4e9a5005cb388 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -33,11 +33,18 @@ class WikiPageTest < ActiveSupport::TestCase page.title = "Page" assert page.save page.reload + assert !page.protected? @wiki.reload assert @wiki.pages.include?(page) end + def test_sidebar_should_be_protected_by_default + page = @wiki.find_or_new_page('sidebar') + assert page.new_record? + assert page.protected? + end + def test_find_or_new_page page = @wiki.find_or_new_page("CookBook documentation") assert_kind_of WikiPage, page diff --git a/test/unit/wiki_test.rb b/test/unit/wiki_test.rb index 6595e6dceab7443bf411ed61fb61a4318d2c2886..9303ce7845c47bfd028ee54ff10d71ce81205f65 100644 --- a/test/unit/wiki_test.rb +++ b/test/unit/wiki_test.rb @@ -43,4 +43,23 @@ class WikiTest < ActiveSupport::TestCase assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES') assert_equal 'テスト', Wiki.titleize('テスト') end + + context "#sidebar" do + setup do + @wiki = Wiki.find(1) + end + + should "return nil if undefined" do + assert_nil @wiki.sidebar + end + + should "return a WikiPage if defined" do + page = @wiki.pages.new(:title => 'Sidebar') + page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar') + page.save! + + assert_kind_of WikiPage, @wiki.sidebar + assert_equal 'Sidebar', @wiki.sidebar.title + end + end end