Class: WikiPage
Overview
Constant Summary
collapse
- DEFAULT_PROTECTED_PAGES =
Wiki pages that are protected by default
%w(sidebar)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#delete_unsafe_attributes, #safe_attribute?, #safe_attribute_names
Constructor Details
#initialize(attributes = nil, *args) ⇒ WikiPage
Returns a new instance of WikiPage
71
72
73
74
75
76
|
# File 'app/models/wiki_page.rb', line 71
def initialize(attributes=nil, *args)
super
if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
self.protected = true
end
end
|
Instance Attribute Details
#deleted_attachment_ids ⇒ Object
Returns the value of attribute deleted_attachment_ids
43
44
45
|
# File 'app/models/wiki_page.rb', line 43
def deleted_attachment_ids
@deleted_attachment_ids
end
|
#redirect_existing_links ⇒ Object
Returns the value of attribute redirect_existing_links
43
44
45
|
# File 'app/models/wiki_page.rb', line 43
def redirect_existing_links
@redirect_existing_links
end
|
Class Method Details
.pretty_title(str) ⇒ Object
182
183
184
|
# File 'app/models/wiki_page.rb', line 182
def self.pretty_title(str)
(str && str.is_a?(String)) ? str.tr('_', ' ') : str
end
|
Instance Method Details
#annotate(version = nil) ⇒ Object
176
177
178
179
180
|
# File 'app/models/wiki_page.rb', line 176
def annotate(version=nil)
version = version ? version.to_i : self.content.version
c = content.versions.find_by_version(version)
c ? WikiAnnotate.new(c) : nil
end
|
#attachments_deletable?(usr = User.current) ⇒ Boolean
207
208
209
|
# File 'app/models/wiki_page.rb', line 207
def attachments_deletable?(usr=User.current)
editable_by?(usr) && super(usr)
end
|
#content_for_version(version = nil) ⇒ Object
155
156
157
158
159
160
161
|
# File 'app/models/wiki_page.rb', line 155
def content_for_version(version=nil)
if content
result = content.versions.find_by_version(version.to_i) if version
result ||= content
result
end
end
|
#delete_redirects ⇒ Object
Deletes redirects to this page
147
148
149
|
# File 'app/models/wiki_page.rb', line 147
def delete_redirects
WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all
end
|
#delete_selected_attachments ⇒ Object
261
262
263
264
265
266
|
# File 'app/models/wiki_page.rb', line 261
def delete_selected_attachments
if deleted_attachment_ids.present?
objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
attachments.delete(objects)
end
end
|
#diff(version_to = nil, version_from = nil) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'app/models/wiki_page.rb', line 163
def diff(version_to=nil, version_from=nil)
version_to = version_to ? version_to.to_i : self.content.version
content_to = content.versions.find_by_version(version_to)
content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
return nil unless content_to && content_from
if content_from.version > content_to.version
content_to, content_from = content_from, content_to
end
(content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
end
|
#editable_by?(usr) ⇒ Boolean
Returns true if usr is allowed to edit the page, otherwise false
203
204
205
|
# File 'app/models/wiki_page.rb', line 203
def editable_by?(usr)
!protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
end
|
#is_start_page ⇒ Object
221
222
223
224
225
226
|
# File 'app/models/wiki_page.rb', line 221
def is_start_page
if @is_start_page.nil?
@is_start_page = wiki.try(:start_page) == title_was
end
@is_start_page
end
|
#is_start_page=(arg) ⇒ Object
228
229
230
|
# File 'app/models/wiki_page.rb', line 228
def is_start_page=(arg)
@is_start_page = arg == '1' || arg == true
end
|
#parent_title ⇒ Object
211
212
213
|
# File 'app/models/wiki_page.rb', line 211
def parent_title
@parent_title || (self.parent && self.parent.pretty_title)
end
|
#parent_title=(t) ⇒ Object
215
216
217
218
219
|
# File 'app/models/wiki_page.rb', line 215
def parent_title=(t)
@parent_title = t
parent_page = t.blank? ? nil : self.wiki.find_page(t)
self.parent = parent_page
end
|
#pretty_title ⇒ Object
151
152
153
|
# File 'app/models/wiki_page.rb', line 151
def pretty_title
WikiPage.pretty_title(title)
end
|
#project ⇒ Object
186
187
188
|
# File 'app/models/wiki_page.rb', line 186
def project
wiki.try(:project)
end
|
#safe_attributes=(attrs, user = User.current) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'app/models/wiki_page.rb', line 87
def safe_attributes=(attrs, user=User.current)
if attrs.respond_to?(:to_unsafe_hash)
attrs = attrs.to_unsafe_hash
end
return unless attrs.is_a?(Hash)
attrs = attrs.deep_dup
if (w_id = attrs.delete('wiki_id')) && safe_attribute?('wiki_id')
if (w = Wiki.find_by_id(w_id)) && w.project && user.allowed_to?(:rename_wiki_pages, w.project)
self.wiki = w
end
end
super attrs, user
end
|
#save_with_content(content) ⇒ Object
Saves the page and its content if text was changed Return true if the page
was saved
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'app/models/wiki_page.rb', line 241
def save_with_content(content)
ret = nil
transaction do
ret = save
if content.text_changed?
begin
self.content = content
rescue ActiveRecord::RecordNotSaved
ret = false
end
end
raise ActiveRecord::Rollback unless ret
end
ret
end
|
#text ⇒ Object
190
191
192
|
# File 'app/models/wiki_page.rb', line 190
def text
content.text if content
end
|
#title=(value) ⇒ Object
82
83
84
85
|
# File 'app/models/wiki_page.rb', line 82
def title=(value)
value = Wiki.titleize(value)
write_attribute(:title, value)
end
|
#updated_on ⇒ Object
194
195
196
|
# File 'app/models/wiki_page.rb', line 194
def updated_on
content_attribute(:updated_on)
end
|
#version ⇒ Object
198
199
200
|
# File 'app/models/wiki_page.rb', line 198
def version
content_attribute(:version)
end
|
#visible?(user = User.current) ⇒ Boolean
78
79
80
|
# File 'app/models/wiki_page.rb', line 78
def visible?(user=User.current)
!user.nil? && user.allowed_to?(:view_wiki_pages, project)
end
|