Class: Revision

Inherits:
Object
  • Object
show all
Defined in:
app/models/revision.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, number, content, created_at, author) ⇒ Revision

Returns a new instance of Revision.



12
13
14
15
16
# File 'app/models/revision.rb', line 12

def initialize(page, number, content, created_at, author)
  @page, @number, @created_at, @author = page, number, created_at, author
  self.content = content
  @display_cache = nil
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



10
11
12
# File 'app/models/revision.rb', line 10

def author
  @author
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'app/models/revision.rb', line 10

def content
  @content
end

#created_atObject

Returns the value of attribute created_at.



10
11
12
# File 'app/models/revision.rb', line 10

def created_at
  @created_at
end

#numberObject

Returns the value of attribute number.



10
11
12
# File 'app/models/revision.rb', line 10

def number
  @number
end

#pageObject

Returns the value of attribute page.



10
11
12
# File 'app/models/revision.rb', line 10

def page
  @page
end

Instance Method Details

#clear_display_cacheObject



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

def clear_display_cache
  @wiki_words_cache = @published_cache = @display_cache = @wiki_includes_cache = 
    @wiki_references_cache = nil
end

#created_onObject



18
19
20
# File 'app/models/revision.rb', line 18

def created_on
  Date.new(@created_at.year, @created_at.mon, @created_at.day)
end

#display_contentObject

Explicit check for new type of display cache with chunks_by_type method. Ensures new version works with older snapshots.



80
81
82
83
84
85
86
# File 'app/models/revision.rb', line 80

def display_content
  unless @display_cache && @display_cache.respond_to?(:chunks_by_type)
    @display_cache = WikiContent.new(self)
    @display_cache.render!
  end
  @display_cache
end

#display_content_for_exportObject



105
106
107
# File 'app/models/revision.rb', line 105

def display_content_for_export
  WikiContent.new(self, {:mode => :export} ).render!
end

#display_diffObject



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

def display_diff
  previous_revision ? HTMLDiff.diff(previous_revision.display_content, display_content) : display_content
end

#display_publishedObject



97
98
99
100
101
102
103
# File 'app/models/revision.rb', line 97

def display_published
  unless @published_cache && @published_cache.respond_to?(:chunks_by_type)
    @published_cache = WikiContent.new(self, {:mode => :publish})
    @published_cache.render!
  end
  @published_cache
end

#existing_pagesObject

Returns an array of all the WikiWords present in the content of this revision. that already exists as a page in the web.



68
69
70
# File 'app/models/revision.rb', line 68

def existing_pages
  wiki_words.select { |wiki_word| page.web.pages[wiki_word] }
end

#force_renderingObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/revision.rb', line 109

def force_rendering
  begin
    display_content.render!
  rescue => e
    ApplicationController.logger.error "Failed rendering page #{@name}"
    ApplicationController.logger.error e
    message = e.message
    # substitute content with an error message
    self.content = <<-EOL
        <p>Markup engine has failed to render this page, raising the following error:</p>
        <p>#{message}</p>
        <pre>#{self.content}</pre>
    EOL
    clear_display_cache
    raise e
  end
end

#next_revisionObject

todo: drop next_revision, previuous_revision and number from here - unused code



31
32
33
# File 'app/models/revision.rb', line 31

def next_revision
  page.revisions[number + 1]
end

#pretty_created_atObject



22
23
24
25
26
27
# File 'app/models/revision.rb', line 22

def pretty_created_at
  # Must use DateTime because Time doesn't support %e on at least some platforms
  DateTime.new(
    @created_at.year, @created_at.mon, @created_at.day, @created_at.hour, @created_at.min
  ).strftime "%B %e, %Y %H:%M" 
end

#previous_revisionObject



35
36
37
# File 'app/models/revision.rb', line 35

def previous_revision
  number > 0 ? page.revisions[number - 1] : nil
end

#unexisting_pagesObject

Returns an array of all the WikiWords present in the content of this revision that *doesn’t* already exists as a page in the web.



74
75
76
# File 'app/models/revision.rb', line 74

def unexisting_pages
  wiki_words - existing_pages
end

#wiki_includesObject

Returns an array of all the WikiIncludes present in the content of this revision.



40
41
42
43
44
45
46
# File 'app/models/revision.rb', line 40

def wiki_includes
  unless @wiki_includes_cache 
    chunks = display_content.find_chunks(Include)
    @wiki_includes_cache = chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
  end
  @wiki_includes_cache
end

#wiki_referencesObject

Returns an array of all the WikiReferences present in the content of this revision.



49
50
51
52
53
54
55
# File 'app/models/revision.rb', line 49

def wiki_references
  unless @wiki_references_cache 
    chunks = display_content.find_chunks(WikiChunk::WikiReference)
    @wiki_references_cache = chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
  end
  @wiki_references_cache
end

#wiki_wordsObject

Returns an array of all the WikiWords present in the content of this revision.



58
59
60
61
62
63
64
# File 'app/models/revision.rb', line 58

def wiki_words
  unless @wiki_words_cache
    wiki_chunks = display_content.find_chunks(WikiChunk::WikiLink)
    @wiki_words_cache = wiki_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
  end
  @wiki_words_cache
end