Class: WikiPage

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

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods inherited from ActiveRecord::Base

quoted_table_name

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

+ (Object) pretty_title(str)



103
104
105
# File 'app/models/wiki_page.rb', line 103

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

Instance Method Details

- (Object) annotate(version = nil)



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

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

- (Boolean) attachments_deletable?(usr = User.current)

Returns:

  • (Boolean)


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

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

- (Object) before_destroy



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

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

- (Object) before_save



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/wiki_page.rb', line 54

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

- (Object) content_for_version(version = nil)



80
81
82
83
84
# File 'app/models/wiki_page.rb', line 80

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

- (Object) diff(version_to = nil, version_from = nil)



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

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

- (Boolean) editable_by?(usr)

Returns true if usr is allowed to edit the page, otherwise false

Returns:

  • (Boolean)


116
117
118
# File 'app/models/wiki_page.rb', line 116

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

- (Object) parent_title



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

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

- (Object) parent_title=(t)



128
129
130
131
132
# File 'app/models/wiki_page.rb', line 128

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

- (Object) pretty_title



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

def pretty_title
  WikiPage.pretty_title(title)
end

- (Object) project



107
108
109
# File 'app/models/wiki_page.rb', line 107

def project
  wiki.project
end

- (Object) text



111
112
113
# File 'app/models/wiki_page.rb', line 111

def text
  content.text if content
end

- (Object) title=(value)



48
49
50
51
52
# File 'app/models/wiki_page.rb', line 48

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

- (Boolean) visible?(user = User.current)

Returns:

  • (Boolean)


44
45
46
# File 'app/models/wiki_page.rb', line 44

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