@@ -1,85 +1,85 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Wiki < ActiveRecord::Base |
|
19 | 19 | belongs_to :project |
|
20 | 20 | has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title' |
|
21 | 21 | has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all |
|
22 | 22 | |
|
23 | 23 | acts_as_watchable |
|
24 | 24 | |
|
25 | 25 | validates_presence_of :start_page |
|
26 | 26 | validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/ |
|
27 | 27 | |
|
28 | 28 | def visible?(user=User.current) |
|
29 | 29 | !user.nil? && user.allowed_to?(:view_wiki_pages, project) |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | # Returns the wiki page that acts as the sidebar content |
|
33 | 33 | # or nil if no such page exists |
|
34 | 34 | def sidebar |
|
35 | 35 | @sidebar ||= find_page('Sidebar', :with_redirect => false) |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | # find the page with the given title |
|
39 | 39 | # if page doesn't exist, return a new page |
|
40 | 40 | def find_or_new_page(title) |
|
41 | 41 | title = start_page if title.blank? |
|
42 | 42 | find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | # find the page with the given title |
|
46 | 46 | def find_page(title, options = {}) |
|
47 | 47 | title = start_page if title.blank? |
|
48 |
title = Wiki.titleize(title) |
|
|
49 | page = pages.first(:conditions => ["LOWER(title) LIKE ?", title]) | |
|
48 | title = Wiki.titleize(title) | |
|
49 | page = pages.first(:conditions => ["LOWER(title) LIKE LOWER(?)", title]) | |
|
50 | 50 | if !page && !(options[:with_redirect] == false) |
|
51 | 51 | # search for a redirect |
|
52 | redirect = redirects.first(:conditions => ["LOWER(title) LIKE ?", title]) | |
|
52 | redirect = redirects.first(:conditions => ["LOWER(title) LIKE LOWER(?)", title]) | |
|
53 | 53 | page = find_page(redirect.redirects_to, :with_redirect => false) if redirect |
|
54 | 54 | end |
|
55 | 55 | page |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | # Finds a page by title |
|
59 | 59 | # The given string can be of one of the forms: "title" or "project:title" |
|
60 | 60 | # Examples: |
|
61 | 61 | # Wiki.find_page("bar", project => foo) |
|
62 | 62 | # Wiki.find_page("foo:bar") |
|
63 | 63 | def self.find_page(title, options = {}) |
|
64 | 64 | project = options[:project] |
|
65 | 65 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} |
|
66 | 66 | project_identifier, title = $1, $2 |
|
67 | 67 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) |
|
68 | 68 | end |
|
69 | 69 | if project && project.wiki |
|
70 | 70 | page = project.wiki.find_page(title) |
|
71 | 71 | if page && page.content |
|
72 | 72 | page |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | # turn a string into a valid page title |
|
78 | 78 | def self.titleize(title) |
|
79 | 79 | # replace spaces with _ and remove unwanted caracters |
|
80 | 80 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title |
|
81 | 81 | # upcase the first letter |
|
82 | 82 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title |
|
83 | 83 | title |
|
84 | 84 | end |
|
85 | 85 | end |
@@ -1,65 +1,71 | |||
|
1 | 1 | --- |
|
2 | 2 | wiki_pages_001: |
|
3 | 3 | created_on: 2007-03-07 00:08:07 +01:00 |
|
4 | 4 | title: CookBook_documentation |
|
5 | 5 | id: 1 |
|
6 | 6 | wiki_id: 1 |
|
7 | 7 | protected: true |
|
8 | 8 | parent_id: |
|
9 | 9 | wiki_pages_002: |
|
10 | 10 | created_on: 2007-03-08 00:18:07 +01:00 |
|
11 | 11 | title: Another_page |
|
12 | 12 | id: 2 |
|
13 | 13 | wiki_id: 1 |
|
14 | 14 | protected: false |
|
15 | 15 | parent_id: |
|
16 | 16 | wiki_pages_003: |
|
17 | 17 | created_on: 2007-03-08 00:18:07 +01:00 |
|
18 | 18 | title: Start_page |
|
19 | 19 | id: 3 |
|
20 | 20 | wiki_id: 2 |
|
21 | 21 | protected: false |
|
22 | 22 | parent_id: |
|
23 | 23 | wiki_pages_004: |
|
24 | 24 | created_on: 2007-03-08 00:18:07 +01:00 |
|
25 | 25 | title: Page_with_an_inline_image |
|
26 | 26 | id: 4 |
|
27 | 27 | wiki_id: 1 |
|
28 | 28 | protected: false |
|
29 | 29 | parent_id: 1 |
|
30 | 30 | wiki_pages_005: |
|
31 | 31 | created_on: 2007-03-08 00:18:07 +01:00 |
|
32 | 32 | title: Child_1 |
|
33 | 33 | id: 5 |
|
34 | 34 | wiki_id: 1 |
|
35 | 35 | protected: false |
|
36 | 36 | parent_id: 2 |
|
37 | 37 | wiki_pages_006: |
|
38 | 38 | created_on: 2007-03-08 00:18:07 +01:00 |
|
39 | 39 | title: Child_2 |
|
40 | 40 | id: 6 |
|
41 | 41 | wiki_id: 1 |
|
42 | 42 | protected: false |
|
43 | 43 | parent_id: 2 |
|
44 | 44 | wiki_pages_007: |
|
45 | 45 | created_on: 2007-03-08 00:18:07 +01:00 |
|
46 | 46 | title: Child_page_1 |
|
47 | 47 | id: 7 |
|
48 | 48 | wiki_id: 2 |
|
49 | 49 | protected: false |
|
50 | 50 | parent_id: 8 |
|
51 | 51 | wiki_pages_008: |
|
52 | 52 | created_on: 2007-03-08 00:18:07 +01:00 |
|
53 | 53 | title: Parent_page |
|
54 | 54 | id: 8 |
|
55 | 55 | wiki_id: 2 |
|
56 | 56 | protected: false |
|
57 | 57 | parent_id: |
|
58 | 58 | wiki_pages_009: |
|
59 | 59 | created_on: 2007-03-08 00:18:07 +01:00 |
|
60 | 60 | title: Child_page_2 |
|
61 | 61 | id: 9 |
|
62 | 62 | wiki_id: 2 |
|
63 | 63 | protected: false |
|
64 | 64 | parent_id: 8 |
|
65 | No newline at end of file | |
|
65 | wiki_pages_010: | |
|
66 | created_on: 2007-03-08 00:18:07 +01:00 | |
|
67 | title: Этика_менеджмента | |
|
68 | id: 10 | |
|
69 | wiki_id: 1 | |
|
70 | protected: false | |
|
71 | parent_id: |
@@ -1,74 +1,77 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # redMine - project management software |
|
4 | 4 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
5 | 5 | # |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; either version 2 |
|
9 | 9 | # of the License, or (at your option) any later version. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 | 19 | |
|
20 | 20 | require File.expand_path('../../test_helper', __FILE__) |
|
21 | 21 | |
|
22 | 22 | class WikiTest < ActiveSupport::TestCase |
|
23 | 23 | fixtures :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions |
|
24 | 24 | |
|
25 | 25 | def test_create |
|
26 | 26 | wiki = Wiki.new(:project => Project.find(2)) |
|
27 | 27 | assert !wiki.save |
|
28 | 28 | assert_equal 1, wiki.errors.count |
|
29 | 29 | |
|
30 | 30 | wiki.start_page = "Start page" |
|
31 | 31 | assert wiki.save |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_update |
|
35 | 35 | @wiki = Wiki.find(1) |
|
36 | 36 | @wiki.start_page = "Another start page" |
|
37 | 37 | assert @wiki.save |
|
38 | 38 | @wiki.reload |
|
39 | 39 | assert_equal "Another start page", @wiki.start_page |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def test_find_page |
|
43 | 43 | wiki = Wiki.find(1) |
|
44 | 44 | page = WikiPage.find(2) |
|
45 | 45 | |
|
46 | 46 | assert_equal page, wiki.find_page('Another_page') |
|
47 | 47 | assert_equal page, wiki.find_page('Another page') |
|
48 | 48 | assert_equal page, wiki.find_page('ANOTHER page') |
|
49 | ||
|
50 | page = WikiPage.find(10) | |
|
51 | assert_equal page, wiki.find_page('Этика_менеджмента') | |
|
49 | 52 | end |
|
50 | 53 | |
|
51 | 54 | def test_titleize |
|
52 | 55 | assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES') |
|
53 | 56 | assert_equal 'テスト', Wiki.titleize('テスト') |
|
54 | 57 | end |
|
55 | 58 | |
|
56 | 59 | context "#sidebar" do |
|
57 | 60 | setup do |
|
58 | 61 | @wiki = Wiki.find(1) |
|
59 | 62 | end |
|
60 | 63 | |
|
61 | 64 | should "return nil if undefined" do |
|
62 | 65 | assert_nil @wiki.sidebar |
|
63 | 66 | end |
|
64 | 67 | |
|
65 | 68 | should "return a WikiPage if defined" do |
|
66 | 69 | page = @wiki.pages.new(:title => 'Sidebar') |
|
67 | 70 | page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar') |
|
68 | 71 | page.save! |
|
69 | 72 | |
|
70 | 73 | assert_kind_of WikiPage, @wiki.sidebar |
|
71 | 74 | assert_equal 'Sidebar', @wiki.sidebar.title |
|
72 | 75 | end |
|
73 | 76 | end |
|
74 | 77 | end |
General Comments 0
You need to be logged in to leave comments.
Login now