Class: Wiki::Page

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty
Defined in:
app/models/wiki/page.rb

Constant Summary collapse

@@format =

a gollum page facade

:markdown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Page

Returns a new instance of Page.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/wiki/page.rb', line 46

def initialize(params={})
  wiki = params[:wiki]
  gollum_page = params[:gollum_page]
  path = params[:path]

  if wiki
    @wiki = wiki
  end
  if gollum_page
    self.gollum_page = gollum_page
  elsif path
    safepath = path.downcase
    @path = safepath
    new_page = @wiki.find_gollum_page(safepath)
    if new_page
      self.gollum_page = new_page
    else
      @name = safepath.split('/').last
      @blank = true
    end
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



17
18
19
# File 'app/models/wiki/page.rb', line 17

def content
  @content
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'app/models/wiki/page.rb', line 17

def name
  @name
end

#pathObject

Returns the value of attribute path.



17
18
19
# File 'app/models/wiki/page.rb', line 17

def path
  @path
end

#uploaded_filesObject

Returns the value of attribute uploaded_files.



17
18
19
# File 'app/models/wiki/page.rb', line 17

def uploaded_files
  @uploaded_files
end

#wikiObject

Returns the value of attribute wiki.



17
18
19
# File 'app/models/wiki/page.rb', line 17

def wiki
  @wiki
end

Instance Method Details

#attachmentsObject



128
129
130
# File 'app/models/wiki/page.rb', line 128

def attachments
  return @wiki.files_under_path(@path)
end

#authorObject

def to_param

@gollum_page.url_path unless @blank

end



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

def author
  @gollum_page.versions.first.author.name unless @blank
end

#authored_dateObject



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

def authored_date
  @gollum_page.versions.first.authored_date unless @blank
end

#childrenObject



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

def children
  @wiki.pages_under_path(@path)
end

#destroy!(user) ⇒ Object



196
197
198
199
200
201
# File 'app/models/wiki/page.rb', line 196

def destroy!(user)
  commit = {name: user.name, email: user.email, message: "removed #{@path}"}
  Rails.logger.debug("removing #{@path}")
  @wiki.gollum_wiki.delete_page(@gollum_page, commit)
  @wiki.push_repo
end

#gollum_pageObject



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

def gollum_page
  @gollum_page
end

#gollum_page=(new_gp) ⇒ Object



69
70
71
72
73
74
# File 'app/models/wiki/page.rb', line 69

def gollum_page=(new_gp)
  @gollum_page = new_gp
  @name = new_gp.name
  @path = new_gp.url_path
  @content = new_gp.raw_data
end

#history_urlObject



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

def history_url
  # @@format is :markdown, so let's presume
  @wiki.history_url + @path + '.md'
end

#htmlObject

may be out of sync with @content!



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

def html
  @gollum_page.formatted_data unless @blank
end

#idObject

Returns the value of attribute path.



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

def path
  @path
end

#inspectObject



203
204
205
# File 'app/models/wiki/page.rb', line 203

def inspect
  "Wiki::Page '#{@path}' (#{@gollum_page})"
end

#metadataObject



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

def 
  @gollum_page.
end

#new_page?Boolean

Returns:

  • (Boolean)


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

def new_page?
  @blank && true
end

#parent_pathObject



119
120
121
122
# File 'app/models/wiki/page.rb', line 119

def parent_path
  parent_path = File.dirname(@path)
  (parent_path == '.') ? '' : parent_path # + '/'
end

#parentsObject



109
110
111
112
113
114
115
116
117
# File 'app/models/wiki/page.rb', line 109

def parents
  parents = []
  ancestors = @path.split('/')
  for i in 1..(ancestors.length - 1) do
    parent_name = ancestors.take(i).join('/')
    parents << @wiki.find_page(parent_name)
  end
  return parents
end

#persisted?Boolean

Returns:

  • (Boolean)


207
208
209
210
# File 'app/models/wiki/page.rb', line 207

def persisted?
  # TODO: make this a real dirty flag
  ! self.new_page?
end

#save(user) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/wiki/page.rb', line 132

def save(user)
  if @blank
    commit = {name: user.name, email: user.email, message: "created #{@path}"}
    Rails.logger.debug("creating #{@path}")

    @wiki.pull_repo
    @wiki.gollum_wiki.write_page(@name, @@format, @content, commit, self.parent_path)

    @gollum_page = @wiki.find_gollum_page(@path)
    @blank = false
  else
    commit = {name: user.name, email: user.email, message: "updated #{@path}"}
    Rails.logger.debug("saving #{@path}")

    @wiki.pull_repo
    @wiki.gollum_wiki.update_page(@gollum_page, @name, @@format, @content, commit)
  end

  if @uploaded_files.present?
    Rails.logger.debug("found #{@uploaded_files.length} new files for #{@path}")
    head = @wiki.gollum_wiki.repo.head

    # NOTE: may raise Gollum::DuplicatePageError
    commit_options = {
      message: "added #{@uploaded_files.length} attachments to #{@path}",
      name: user.name,
      email: user.email
    }
    committer = Gollum::Committer.new(@wiki.gollum_wiki, commit_options)

    self.uploaded_files.each do |uploaded|
      # TODO: maybe this should be creating then saving new files
      fullname = uploaded.original_filename
      ext = File.extname(fullname)
      format = ext.split('.').last
      filename = File.basename(fullname, ext)
      contents = uploaded.read
      fsdir = File.join(@wiki.gollum_wiki.path, @path)
      pathname = File.join(@path, fullname)

      if !FileTest::directory?(fsdir)
        Dir::mkdir(fsdir)
      end
      fspath = File.join(fsdir, fullname)
      File.open(fspath, 'wb') do |file|
        file.write(contents)
        Rails.logger.debug("uploaded #{format} to #{fspath}")
      end

      committer.index.add(pathname, contents)
      # committer.add_to_index(@path, filename, format, contents)
      # # committer.add_to_index(@path, filename, format, "file")
      committer.after_commit do |committer, sha|
        @wiki.gollum_wiki.clear_cache
        committer.update_working_dir(@path, filename, format)
      end
    end
    committer.commit
  end

  @wiki.push_repo
  changes_applied
end

#to_paramObject



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

def to_param
  @path
end

#update(hash) ⇒ Object



38
39
40
41
42
43
44
# File 'app/models/wiki/page.rb', line 38

def update(hash)
  self.name = hash[:name]
  self.content = hash[:content]
  @uploaded_files = hash[:uploaded_files]

  return true
end