Class: WikiPage

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

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

Instance Attribute Details

Returns the value of attribute redirect_existing_links


34
35
36
# File 'app/models/wiki_page.rb', line 34

def redirect_existing_links
  @redirect_existing_links
end

Class Method Details

.pretty_title(str) ⇒ Object


119
120
121
# File 'app/models/wiki_page.rb', line 119

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

Instance Method Details

#after_initializeObject


50
51
52
53
54
# File 'app/models/wiki_page.rb', line 50

def after_initialize
  if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
    self.protected = true
  end
end

#annotate(version = nil) ⇒ Object


113
114
115
116
117
# File 'app/models/wiki_page.rb', line 113

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)

151
152
153
# File 'app/models/wiki_page.rb', line 151

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

#before_destroyObject


87
88
89
90
# File 'app/models/wiki_page.rb', line 87

def before_destroy
  # Remove redirects to this page
  wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
end

#before_saveObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/wiki_page.rb', line 70

def before_save
  self.title = Wiki.titleize(title)
  # Manage redirects if the title has changed
  if !@previous_title.blank? && (@previous_title != title) && !new_record?
    # Update redirects that point to the old title
    wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
      r.redirects_to = title
      r.title == r.redirects_to ? r.destroy : r.save
    end
    # Remove redirects for the new title
    wiki.redirects.find_all_by_title(title).each(&:destroy)
    # Create a redirect to the new title
    wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
    @previous_title = nil
  end
end

#content_for_version(version = nil) ⇒ Object


96
97
98
99
100
# File 'app/models/wiki_page.rb', line 96

def content_for_version(version=nil)
  result = content.versions.find_by_version(version.to_i) if version
  result ||= content
  result
end

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


102
103
104
105
106
107
108
109
110
111
# File 'app/models/wiki_page.rb', line 102

def diff(version_to=nil, version_from=nil)
  version_to = version_to ? version_to.to_i : self.content.version
  version_from = version_from ? version_from.to_i : version_to - 1
  version_to, version_from = version_from, version_to unless version_from < version_to

  content_to = content.versions.find_by_version(version_to)
  content_from = content.versions.find_by_version(version_from)

  (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)

147
148
149
# File 'app/models/wiki_page.rb', line 147

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

#parent_titleObject


155
156
157
# File 'app/models/wiki_page.rb', line 155

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

#parent_title=(t) ⇒ Object


159
160
161
162
163
# File 'app/models/wiki_page.rb', line 159

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

#pretty_titleObject


92
93
94
# File 'app/models/wiki_page.rb', line 92

def pretty_title
  WikiPage.pretty_title(title)
end

#projectObject


123
124
125
# File 'app/models/wiki_page.rb', line 123

def project
  wiki.project
end

#textObject


127
128
129
# File 'app/models/wiki_page.rb', line 127

def text
  content.text if content
end

#title=(value) ⇒ Object


64
65
66
67
68
# File 'app/models/wiki_page.rb', line 64

def title=(value)
  value = Wiki.titleize(value)
  @previous_title = read_attribute(:title) if @previous_title.blank?
  write_attribute(:title, value)
end

#to_liquidObject


56
57
58
# File 'app/models/wiki_page.rb', line 56

def to_liquid
  WikiPageDrop.new(self)
end

#updated_onObject


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/wiki_page.rb', line 131

def updated_on
  unless @updated_on
    if time = read_attribute(:updated_on)
      # content updated_on was eager loaded with the page
      unless time.is_a? Time
        time = Time.parse(time) rescue nil
      end
      @updated_on = time
    else
      @updated_on = content && content.updated_on
    end
  end
  @updated_on
end

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

Returns:

  • (Boolean)

60
61
62
# File 'app/models/wiki_page.rb', line 60

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