Class: WikiPage

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, StaticModel
Defined in:
app/models/wiki_page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StaticModel

#==, #[], #destroyed?, #new_record?

Constructor Details

#initialize(wiki, page = nil, persisted = false) ⇒ WikiPage

Returns a new instance of WikiPage


36
37
38
39
40
41
42
43
# File 'app/models/wiki_page.rb', line 36

def initialize(wiki, page = nil, persisted = false)
  @wiki       = wiki
  @page       = page
  @persisted  = persisted
  @attributes = {}.with_indifferent_access

  set_attributes if persisted?
end

Instance Attribute Details

#attributesObject

The attributes Hash used for storing and validating new Page values before writing to the Gollum repository.


30
31
32
# File 'app/models/wiki_page.rb', line 30

def attributes
  @attributes
end

#pageObject (readonly)

The raw Gollum::Page instance.


26
27
28
# File 'app/models/wiki_page.rb', line 26

def page
  @page
end

#wikiObject (readonly)

The Gitlab ProjectWiki instance.


23
24
25
# File 'app/models/wiki_page.rb', line 23

def wiki
  @wiki
end

Class Method Details

.model_nameObject


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

def self.model_name
  ActiveModel::Name.new(self, nil, 'wiki')
end

.primary_keyObject


7
8
9
# File 'app/models/wiki_page.rb', line 7

def self.primary_key
  'slug'
end

Instance Method Details

#commitObject


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

def commit
  versions.first
end

#contentObject

The raw content of this page.


67
68
69
70
71
# File 'app/models/wiki_page.rb', line 67

def content
  @attributes[:content] ||= if @page
                              @page.text_data
                            end
end

#create(attr = {}) ⇒ Object

Creates a new Wiki Page.

attr - Hash of attributes to set on the new page.

:title   - The title for the new page.
:content - The raw markup content.
:format  - Optional symbol representing the
           content format. Can be any type
           listed in the ProjectWiki::MARKUPS
           Hash.
:message - Optional commit message to set on
           the new page.

Returns the String SHA1 of the newly created page or False if the save was unsuccessful.


140
141
142
143
144
# File 'app/models/wiki_page.rb', line 140

def create(attr = {})
  @attributes.merge!(attr)

  save :create_page, title, content, format, message
end

#created_atObject

Returns the Date that this latest version was created on.


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

def created_at
  @page.version.date
end

#deleteObject

Destroys the Wiki Page.

Returns boolean True or False.


165
166
167
168
169
170
171
# File 'app/models/wiki_page.rb', line 165

def delete
  if wiki.delete_page(@page)
    true
  else
    false
  end
end

#formatObject

The markup format for the page.


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

def format
  @attributes[:format] || :markdown
end

#formatted_contentObject

The processed/formatted content of this page.


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

def formatted_content
  @attributes[:formatted_content] ||= if @page
                                        @page.formatted_data
                                      end
end

#historical?Boolean

Returns boolean True or False if this instance is an old version of the page.

Returns:

  • (Boolean)

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

def historical?
  @page.historical? && versions.first.sha != version.sha
end

#hook_attrsObject


32
33
34
# File 'app/models/wiki_page.rb', line 32

def hook_attrs
  attributes
end

#messageObject

The commit message for this page version.


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

def message
  version.try(:message)
end

#persisted?Boolean

Returns boolean True or False if this instance has been fully saved to disk or not.

Returns:

  • (Boolean)

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

def persisted?
  @persisted == true
end

#slugObject Also known as: to_param

The escaped URL path of this page.


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

def slug
  @attributes[:slug]
end

#titleObject

The formatted title of this page.


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

def title
  if @attributes[:title]
    @attributes[:title].gsub(/-+/, ' ')
  else
    ""
  end
end

#title=(new_title) ⇒ Object

Sets the title of this page.


62
63
64
# File 'app/models/wiki_page.rb', line 62

def title=(new_title)
  @attributes[:title] = new_title
end

#to_keyObject


15
16
17
# File 'app/models/wiki_page.rb', line 15

def to_key
  [:slug]
end

#update(new_content = "", format = :markdown, message = nil) ⇒ Object

Updates an existing Wiki Page, creating a new version.

new_content - The raw markup content to replace the existing. format - Optional symbol representing the content format.

See ProjectWiki::MARKUPS Hash for available formats.

message - Optional commit message to set on the new version.

Returns the String SHA1 of the newly created page or False if the save was unsuccessful.


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

def update(new_content = "", format = :markdown, message = nil)
  @attributes[:content] = new_content
  @attributes[:format] = format

  save :update_page, @page, content, format, message
end

#versionObject

The Gitlab Commit instance for this page.


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

def version
  return nil unless persisted?

  @version ||= @page.version
end

#versionsObject

Returns an array of Gitlab Commit instances.


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

def versions
  return [] unless persisted?

  @page.versions
end