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.



37
38
39
# File 'app/models/wiki_page.rb', line 37

def redirect_existing_links
  @redirect_existing_links
end

Class Method Details

.pretty_title(str) ⇒ Object



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

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

Instance Method Details

#after_initializeObject



53
54
55
56
57
# File 'app/models/wiki_page.rb', line 53

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

#annotate(version = nil) ⇒ Object



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

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)


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

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

#before_destroyObject



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

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

#before_saveObject



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

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



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

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



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

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)


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

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

#parent_titleObject



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

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

#parent_title=(t) ⇒ Object



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

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

#pretty_titleObject



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

def pretty_title
  WikiPage.pretty_title(title)
end

#projectObject



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

def project
  wiki.project
end

#textObject



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

def text
  content.text if content
end

#title=(value) ⇒ Object



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

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

#updated_onObject



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

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

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

Returns:

  • (Boolean)


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

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