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) ⇒ Page

Returns a new instance of Page.



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

def initialize(web, name)
  raise 'nil web' if web.nil?
  raise 'nil name' if name.nil?
  @web, @name, @revisions = web, name, []
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



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

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



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

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

#authorsObject



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

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

#categoriesObject



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

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

#idObject

used to build chunk ids.



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

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

#in_category?(cat) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#included_fromObject



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

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


101
102
103
# File 'app/models/page.rb', line 101

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

#linked_fromObject



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

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”.



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

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

#referencesObject



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

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

#revise(content, created_at, author) ⇒ Object



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
45
46
47
48
49
50
51
52
# File 'app/models/page.rb', line 19

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

  # Try to render content to make sure that markup engine can take it,
  # before addin a revision to the page
  Revision.new(self, @revisions.length, content, created_at, author).force_rendering

  # 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
  
  self
  
end

#revised_onObject



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

def revised_on
  created_on
end

#revisions?Boolean

Returns:

  • (Boolean)


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

def revisions?
  revisions.length > 1
end

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



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

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