Class: Wiki::Page
- Inherits:
-
Object
- Object
- Wiki::Page
- 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
-
#content ⇒ Object
Returns the value of attribute content.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#uploaded_files ⇒ Object
Returns the value of attribute uploaded_files.
-
#wiki ⇒ Object
Returns the value of attribute wiki.
Instance Method Summary collapse
- #attachments ⇒ Object
-
#author ⇒ Object
def to_param @gollum_page.url_path unless @blank end.
- #authored_date ⇒ Object
- #children ⇒ Object
- #destroy!(user) ⇒ Object
- #gollum_page ⇒ Object
- #gollum_page=(new_gp) ⇒ Object
- #history_url ⇒ Object
-
#html ⇒ Object
may be out of sync with @content!.
-
#id ⇒ Object
Returns the value of attribute path.
-
#initialize(params = {}) ⇒ Page
constructor
A new instance of Page.
- #inspect ⇒ Object
- #metadata ⇒ Object
- #new_page? ⇒ Boolean
- #parent_path ⇒ Object
- #parents ⇒ Object
- #persisted? ⇒ Boolean
- #save(user) ⇒ Object
- #to_param ⇒ Object
- #update(hash) ⇒ Object
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
#content ⇒ Object
Returns the value of attribute content.
17 18 19 |
# File 'app/models/wiki/page.rb', line 17 def content @content end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'app/models/wiki/page.rb', line 17 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
17 18 19 |
# File 'app/models/wiki/page.rb', line 17 def path @path end |
#uploaded_files ⇒ Object
Returns the value of attribute uploaded_files.
17 18 19 |
# File 'app/models/wiki/page.rb', line 17 def uploaded_files @uploaded_files end |
#wiki ⇒ Object
Returns the value of attribute wiki.
17 18 19 |
# File 'app/models/wiki/page.rb', line 17 def wiki @wiki end |
Instance Method Details
#attachments ⇒ Object
128 129 130 |
# File 'app/models/wiki/page.rb', line 128 def return @wiki.files_under_path(@path) end |
#author ⇒ Object
def to_param
@gollum_page.url_path unless @blank
end
97 98 99 |
# File 'app/models/wiki/page.rb', line 97 def @gollum_page.versions.first..name unless @blank end |
#authored_date ⇒ Object
100 101 102 |
# File 'app/models/wiki/page.rb', line 100 def @gollum_page.versions.first. unless @blank end |
#children ⇒ Object
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_page ⇒ Object
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_url ⇒ Object
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 |
#html ⇒ Object
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 |
#id ⇒ Object
Returns the value of attribute path.
18 19 20 |
# File 'app/models/wiki/page.rb', line 18 def path @path end |
#inspect ⇒ Object
203 204 205 |
# File 'app/models/wiki/page.rb', line 203 def inspect "Wiki::Page '#{@path}' (#{@gollum_page})" end |
#metadata ⇒ Object
89 90 91 |
# File 'app/models/wiki/page.rb', line 89 def @gollum_page. end |
#new_page? ⇒ Boolean
80 81 82 |
# File 'app/models/wiki/page.rb', line 80 def new_page? @blank && true end |
#parent_path ⇒ Object
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 |
#parents ⇒ Object
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
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 = { message: "added #{@uploaded_files.length} attachments to #{@path}", name: user.name, email: user.email } committer = Gollum::Committer.new(@wiki.gollum_wiki, ) 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_param ⇒ Object
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 |