Skip to content

Instantly share code, notes, and snippets.

@hardywu
Last active December 19, 2015 05:58
Show Gist options
  • Save hardywu/5907550 to your computer and use it in GitHub Desktop.
Save hardywu/5907550 to your computer and use it in GitHub Desktop.
A rails model for gollum-lib, which is used to embed `gollum` into existing rails project. The `unindexable` and `unreadable` statuses are introduced. The `pull` and `push` method is added to have the git repository sync with its **remote origin**.
class Post
@data = Gollum::Wiki.new('doc/blog.git', :mathjax => true)
@@unindexable = 'page'
@@unreadable = 'pending'
def self.data
@data
end
def self.reload
@data = Gollum::Wiki.new('doc/blog.git', :mathjax => true)
end
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
require "html_truncator"
attr_accessor :name, :date, :raw_data, :formatted_data, :commit, :page,
:title, :path, :versions, :format, :email, :filename
alias :id :name
def initialize(attributes = {})
attributes.each do |name, value|
__send__("#{name}=", value)
end
end
def self.find(name)
post = @data.page(name)
if post
new :name => post.name,
:raw_data => post.raw_data,
:formatted_data => post.formatted_data,
:title => post.title,
:path => post.path,
:versions => post.versions,
:page => post,
:format => post.format,
:filename => post.filename
end
end
def self.pages
@data.pages.reverse.select { |page| page.versions.first.message != @@unindexable and page.versions.first.message != @@unreadable }
end
def self.truncate(text, number, tag = 'div' )
HTML_Truncator.ellipsable_tags << tag
HTML_Truncator.truncate(text, number, :ellipsis => '...')
end
def persisted?
self.class.data.page(name)
end
def save
self.class.data.write_page(name, :markdown, raw_data, commit)
end
def destroy
self.class.data.delete_page(page, commit)
end
def preview
self.class.data.preview_page(name, raw_data, :markdown)
end
def update_attributes(hash)
_raw_data = hash[:raw_data]
_name = hash[:name]
_commit = hash[:commit]
self.class.data.update_page(page, _name, :markdown, _raw_data, _commit)
end
def self.push
`cd #{@data.path} && git push origin master 2>&1`
end
def self.pull
`cd #{@data.path} && git pull origin master 2>&1`
end
end
@hardywu
Copy link
Author

hardywu commented Jul 2, 2013

A model class embedding gollum-lib into rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment