Class: WikiPage

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/wiki_page.rb

Constant Summary

DEFAULT_PROTECTED_PAGES =

Wiki pages that are protected by default

%w(sidebar)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute?, #safe_attribute_names

Constructor Details

#initialize(attributes = nil, *args) ⇒ WikiPage

Returns a new instance of WikiPage



66
67
68
69
70
71
# File 'app/models/wiki_page.rb', line 66

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

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



173
174
175
# File 'app/models/wiki_page.rb', line 173

def self.pretty_title(str)
  (str && str.is_a?(String)) ? str.tr('_', ' ') : str
end

Instance Method Details

#annotate(version = nil) ⇒ Object



167
168
169
170
171
# File 'app/models/wiki_page.rb', line 167

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

Returns:

  • (Boolean)


198
199
200
# File 'app/models/wiki_page.rb', line 198

def attachments_deletable?(usr=User.current)
  editable_by?(usr) && super(usr)
end

#content_for_version(version = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'app/models/wiki_page.rb', line 146

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_redirectsObject

Deletes redirects to this page



138
139
140
# File 'app/models/wiki_page.rb', line 138

def delete_redirects
  WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all
end

#diff(version_to = nil, version_from = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/wiki_page.rb', line 154

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

Returns:

  • (Boolean)


194
195
196
# File 'app/models/wiki_page.rb', line 194

def editable_by?(usr)
  !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
end

#parent_titleObject



202
203
204
# File 'app/models/wiki_page.rb', line 202

def parent_title
  @parent_title || (self.parent && self.parent.pretty_title)
end

#parent_title=(t) ⇒ Object



206
207
208
209
210
# File 'app/models/wiki_page.rb', line 206

def parent_title=(t)
  @parent_title = t
  parent_page = t.blank? ? nil : self.wiki.find_page(t)
  self.parent = parent_page
end

#pretty_titleObject



142
143
144
# File 'app/models/wiki_page.rb', line 142

def pretty_title
  WikiPage.pretty_title(title)
end

#projectObject



177
178
179
# File 'app/models/wiki_page.rb', line 177

def project
  wiki.try(:project)
end

#safe_attributes=(attrs, user = User.current) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/wiki_page.rb', line 82

def safe_attributes=(attrs, user=User.current)
  return unless attrs.is_a?(Hash)
  attrs = attrs.deep_dup

  # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
  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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/models/wiki_page.rb', line 214

def save_with_content(content)
  ret = nil
  transaction do
    ret = save
    if content.text_changed?
      begin
        self.content = content
        ret = ret && content.changed?
      rescue ActiveRecord::RecordNotSaved
        ret = false
      end
    end
    raise ActiveRecord::Rollback unless ret
  end
  ret
end

#textObject



181
182
183
# File 'app/models/wiki_page.rb', line 181

def text
  content.text if content
end

#title=(value) ⇒ Object



77
78
79
80
# File 'app/models/wiki_page.rb', line 77

def title=(value)
  value = Wiki.titleize(value)
  write_attribute(:title, value)
end

#updated_onObject



185
186
187
# File 'app/models/wiki_page.rb', line 185

def updated_on
  content_attribute(:updated_on)
end

#versionObject



189
190
191
# File 'app/models/wiki_page.rb', line 189

def version
  content_attribute(:version)
end

#visible?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/wiki_page.rb', line 73

def visible?(user=User.current)
  !user.nil? && user.allowed_to?(:view_wiki_pages, project)
end