@@ -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 | 48 | title = Wiki.titleize(title) |
|
49 |
page = pages.first(:conditions => ["LOWER(title) |
|
|
49 | page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title]) | |
|
50 | 50 | if !page && !(options[:with_redirect] == false) |
|
51 | 51 | # search for a redirect |
|
52 |
redirect = redirects.first(:conditions => ["LOWER(title) |
|
|
52 | redirect = redirects.first(:conditions => ["LOWER(title) = 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,77 +1,80 | |||
|
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 | fixtures :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions | |
|
23 | fixtures :projects, :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 | 49 | |
|
50 | 50 | page = WikiPage.find(10) |
|
51 | 51 | assert_equal page, wiki.find_page('Этика_менеджмента') |
|
52 | ||
|
53 | page = WikiPage.generate!(:wiki => wiki, :title => '2009\\02\\09') | |
|
54 | assert_equal page, wiki.find_page('2009\\02\\09') | |
|
52 | 55 | end |
|
53 | 56 | |
|
54 | 57 | def test_titleize |
|
55 | 58 | assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES') |
|
56 | 59 | assert_equal 'テスト', Wiki.titleize('テスト') |
|
57 | 60 | end |
|
58 | 61 | |
|
59 | 62 | context "#sidebar" do |
|
60 | 63 | setup do |
|
61 | 64 | @wiki = Wiki.find(1) |
|
62 | 65 | end |
|
63 | 66 | |
|
64 | 67 | should "return nil if undefined" do |
|
65 | 68 | assert_nil @wiki.sidebar |
|
66 | 69 | end |
|
67 | 70 | |
|
68 | 71 | should "return a WikiPage if defined" do |
|
69 | 72 | page = @wiki.pages.new(:title => 'Sidebar') |
|
70 | 73 | page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar') |
|
71 | 74 | page.save! |
|
72 | 75 | |
|
73 | 76 | assert_kind_of WikiPage, @wiki.sidebar |
|
74 | 77 | assert_equal 'Sidebar', @wiki.sidebar.title |
|
75 | 78 | end |
|
76 | 79 | end |
|
77 | 80 | end |
General Comments 0
You need to be logged in to leave comments.
Login now