comparison app/models/wiki_page.rb @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents 0c939c159af4
children cbb26bc654de
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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.
30 :datetime => :created_on, 30 :datetime => :created_on,
31 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}} 31 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
32 32
33 acts_as_searchable :columns => ['title', 'text'], 33 acts_as_searchable :columns => ['title', 'text'],
34 :include => [{:wiki => :project}, :content], 34 :include => [{:wiki => :project}, :content],
35 :permission => :view_wiki_pages,
35 :project_key => "#{Wiki.table_name}.project_id" 36 :project_key => "#{Wiki.table_name}.project_id"
36 37
37 attr_accessor :redirect_existing_links 38 attr_accessor :redirect_existing_links
38 39
39 validates_presence_of :title 40 validates_presence_of :title
40 validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/ 41 validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
41 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false 42 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
42 validates_associated :content 43 validates_associated :content
44
45 # eager load information about last updates, without loading text
46 named_scope :with_updated_on, {
47 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
48 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
49 }
43 50
44 # Wiki pages that are protected by default 51 # Wiki pages that are protected by default
45 DEFAULT_PROTECTED_PAGES = %w(sidebar) 52 DEFAULT_PROTECTED_PAGES = %w(sidebar)
46 53
47 def after_initialize 54 def after_initialize
119 126
120 def text 127 def text
121 content.text if content 128 content.text if content
122 end 129 end
123 130
131 def updated_on
132 unless @updated_on
133 if time = read_attribute(:updated_on)
134 # content updated_on was eager loaded with the page
135 @updated_on = Time.parse(time) rescue nil
136 else
137 @updated_on = content && content.updated_on
138 end
139 end
140 @updated_on
141 end
142
124 # Returns true if usr is allowed to edit the page, otherwise false 143 # Returns true if usr is allowed to edit the page, otherwise false
125 def editable_by?(usr) 144 def editable_by?(usr)
126 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project) 145 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
127 end 146 end
128 147
147 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self)) 166 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
148 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id) 167 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
149 end 168 end
150 end 169 end
151 170
152 class WikiDiff 171 class WikiDiff < Redmine::Helpers::Diff
153 attr_reader :diff, :words, :content_to, :content_from 172 attr_reader :content_to, :content_from
154 173
155 def initialize(content_to, content_from) 174 def initialize(content_to, content_from)
156 @content_to = content_to 175 @content_to = content_to
157 @content_from = content_from 176 @content_from = content_from
158 @words = content_to.text.split(/(\s+)/) 177 super(content_to.text, content_from.text)
159 @words = @words.select {|word| word != ' '}
160 words_from = content_from.text.split(/(\s+)/)
161 words_from = words_from.select {|word| word != ' '}
162 @diff = words_from.diff @words
163 end 178 end
164 end 179 end
165 180
166 class WikiAnnotate 181 class WikiAnnotate
167 attr_reader :lines, :content 182 attr_reader :lines, :content
195 positions.compact! 210 positions.compact!
196 # Stop if every line is annotated 211 # Stop if every line is annotated
197 break unless @lines.detect { |line| line[0].nil? } 212 break unless @lines.detect { |line| line[0].nil? }
198 current = current.previous 213 current = current.previous
199 end 214 end
200 @lines.each { |line| line[0] ||= current.version } 215 @lines.each { |line|
216 line[0] ||= current.version
217 # if the last known version is > 1 (eg. history was cleared), we don't know the author
218 line[1] ||= current.author if current.version == 1
219 }
201 end 220 end
202 end 221 end