##// END OF EJS Templates
Fixed: can't preview/save the very first wiki page....
Jean-Philippe Lang -
r715:8929bfca6312
parent child
Show More
@@ -1,53 +1,54
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Wiki < ActiveRecord::Base
18 class Wiki < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
22
22
23 validates_presence_of :start_page
23 validates_presence_of :start_page
24 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
24 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
25
25
26 # find the page with the given title
26 # find the page with the given title
27 # if page doesn't exist, return a new page
27 # if page doesn't exist, return a new page
28 def find_or_new_page(title)
28 def find_or_new_page(title)
29 title = start_page if title.blank?
29 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
30 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
30 end
31 end
31
32
32 # find the page with the given title
33 # find the page with the given title
33 def find_page(title, options = {})
34 def find_page(title, options = {})
34 title = start_page if title.blank?
35 title = start_page if title.blank?
35 title = Wiki.titleize(title)
36 title = Wiki.titleize(title)
36 page = pages.find_by_title(title)
37 page = pages.find_by_title(title)
37 if !page && !(options[:with_redirect] == false)
38 if !page && !(options[:with_redirect] == false)
38 # search for a redirect
39 # search for a redirect
39 redirect = redirects.find_by_title(title)
40 redirect = redirects.find_by_title(title)
40 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
41 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
41 end
42 end
42 page
43 page
43 end
44 end
44
45
45 # turn a string into a valid page title
46 # turn a string into a valid page title
46 def self.titleize(title)
47 def self.titleize(title)
47 # replace spaces with _ and remove unwanted caracters
48 # replace spaces with _ and remove unwanted caracters
48 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
49 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
49 # upcase the first letter
50 # upcase the first letter
50 title = (title.length > 1 ? title.first.upcase + title[1..-1] : title.upcase) if title
51 title = (title.length > 1 ? title.first.upcase + title[1..-1] : title.upcase) if title
51 title
52 title
52 end
53 end
53 end
54 end
General Comments 0
You need to be logged in to leave comments. Login now