Class: Page

Inherits:
Object
  • Object
show all
Includes:
PageLock
Defined in:
app/models/page.rb

Constant Summary

Constants included from PageLock

PageLock::LOCKING_PERIOD

Instance Attribute Summary collapse

Attributes included from PageLock

#locked_by

Instance Method Summary collapse

Methods included from PageLock

#lock, #lock_duration, #locked?, #unlock

Constructor Details

#initialize(web, name, content, created_at, author) ⇒ Page

Returns a new instance of Page.



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

def initialize(web, name, content, created_at, author)
  @web, @name, @revisions = web, name, []
  revise(content, created_at, author)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol) ⇒ Object (private)

Forward method calls to the current revision, so the page responds to all revision calls



108
109
110
# File 'app/models/page.rb', line 108

def method_missing(method_symbol)
  revisions.last.send(method_symbol)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#revisionsObject

Returns the value of attribute revisions.



11
12
13
# File 'app/models/page.rb', line 11

def revisions
  @revisions
end

#webObject (readonly)

Returns the value of attribute web.



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

def web
  @web
end

Instance Method Details



97
98
99
# File 'app/models/page.rb', line 97

def author_link(options = {})
  @web.make_link(author, nil, options)
end

#authorsObject



67
68
69
# File 'app/models/page.rb', line 67

def authors
  revisions.collect { |rev| rev.author }
end

#categoriesObject



63
64
65
# File 'app/models/page.rb', line 63

def categories
  display_content.find_chunks(Category).map { |cat| cat.list }.flatten
end

#idObject

used to build chunk ids.



89
90
91
# File 'app/models/page.rb', line 89

def id
  @id ||= name.unpack('H*').first
end

#in_category?(cat) ⇒ Boolean

Returns:

  • (Boolean)


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

def in_category?(cat)
  cat.nil? || cat.empty? || categories.include?(cat)
end

#included_fromObject



79
80
81
# File 'app/models/page.rb', line 79

def included_from
  @web.select.pages_that_include(name)
end


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

def link(options = {})
  @web.make_link(name, nil, options)
end

#linked_fromObject



75
76
77
# File 'app/models/page.rb', line 75

def linked_from
  @web.select.pages_that_link_to(name)
end

#plain_nameObject

Returns the original wiki-word name as separate words, so “MyPage” becomes “My Page”.



84
85
86
# File 'app/models/page.rb', line 84

def plain_name
  @web.brackets_only ? name : WikiWords.separate(name)
end

#referencesObject



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

def references
  @web.select.pages_that_reference(name)
end

#revise(content, created_at, author) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/page.rb', line 18

def revise(content, created_at, author)

  if not @revisions.empty? and content == @revisions.last.content
    raise Instiki::ValidationError.new(
        "You have tried to save page '#{name}' without changing its content")
  end

  # A user may change a page, look at it and make some more changes - several times.
  # Not to record every such iteration as a new revision, if the previous revision was done 
  # by the same author, not more than 30 minutes ago, then update the last revision instead of
  # creating a new one
  if !@revisions.empty? && continous_revision?(created_at, author)
    @revisions.last.created_at = created_at
    @revisions.last.content = content
    @revisions.last.clear_display_cache
  else
    @revisions << Revision.new(self, @revisions.length, content, created_at, author)
  end

  self.revisions.last.force_rendering
  # at this point the page may not be inserted in the web yet, and therefore 
  # references to the page itself are rendered as "unresolved". Clearing the cache allows 
  # the page to re-render itself once again, hopefully _after_ it is inserted in the web
  self.revisions.last.clear_display_cache
  
  @web.refresh_pages_with_references(@name) if @revisions.length == 1
end

#revised_onObject



55
56
57
# File 'app/models/page.rb', line 55

def revised_on
  created_on
end

#revisions?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/page.rb', line 51

def revisions?
  revisions.length > 1
end

#rollback(revision_number, created_at, author_ip = nil) ⇒ Object



46
47
48
49
# File 'app/models/page.rb', line 46

def rollback(revision_number, created_at, author_ip = nil)
  roll_back_revision = @revisions[revision_number].dup
  revise(roll_back_revision.content, created_at, Author.new(roll_back_revision.author, author_ip))
end