class Octodmin::Post

Constants

ATTRIBUTES_FOR_SERIALIZAION

Attributes

post[RW]

Public Class Methods

create(options = {}) click to toggle source
# File lib/octodmin/post.rb, line 15
def self.create(options = {})
  site = Octodmin::Site.new

  # Prepare slug
  options["slug"] = slug_for(site, options[:title])

  # Create post
  post = Octopress::Post.new(Octopress.site, Jekyll::Utils.stringify_hash_keys(options))
  post.write

  site.posts.first
rescue RuntimeError
end
find(id) click to toggle source
# File lib/octodmin/post.rb, line 10
def self.find(id)
  site = Octodmin::Site.new
  site.posts.find { |post| post.identifier == id }
end
new(site, post) click to toggle source
# File lib/octodmin/post.rb, line 29
def initialize(site, post)
  @site = site
  @post = post
end

Private Class Methods

slug_for(site, title) click to toggle source
# File lib/octodmin/post.rb, line 121
def self.slug_for(site, title)
  title.to_slug.transliterate(
    site.config["octodmin"]["transliterate"]
  ).normalize.to_s
end

Public Instance Methods

delete() click to toggle source
# File lib/octodmin/post.rb, line 88
def delete
  octopost = octopost_for(@post)
  git = Git.open(Octodmin::App.dir)
  git.lib.send(:command, "rm", ["-f", "--cached", octopost.path])
rescue Git::GitExecuteError
  File.delete(octopost.path)
end
identifier() click to toggle source
# File lib/octodmin/post.rb, line 38
def identifier
  path.split("/").last.split(".").first
end
path() click to toggle source
# File lib/octodmin/post.rb, line 34
def path
  @post.path
end
restore() click to toggle source
# File lib/octodmin/post.rb, line 96
def restore
  return unless deleted?

  octopost = octopost_for(@post)
  git = Git.open(Octodmin::App.dir)
  git.add(octopost.path)

  @site.reset
  @post = post_for(octopost)
end
revert() click to toggle source
# File lib/octodmin/post.rb, line 107
def revert
  return unless changed?

  octopost = octopost_for(@post)
  git = Git.open(Octodmin::App.dir)
  git.reset(octopost.path)
  git.checkout(octopost.path)

  @site.reset
  @post = post_for(octopost)
end
serializable_hash() click to toggle source
# File lib/octodmin/post.rb, line 42
def serializable_hash
  hash = @post.to_liquid(ATTRIBUTES_FOR_SERIALIZAION).dup

  excerpt = Jekyll::Excerpt.new(@post)
  excerpt.do_layout({ "site" => @site.config }, {})

  hash.merge(
    excerpt: excerpt.content,
    identifier: identifier,
    slug: @post.slug,
    added: added?,
    changed: changed?,
    deleted: deleted?,
  )
end
update(params) click to toggle source
# File lib/octodmin/post.rb, line 58
def update(params)
  # Remove old post
  delete

  # Init the new one
  octopost = Octopress::Post.new(Octopress.site, {
    "path" => @post.path,
    "date" => params["date"],
    "slug" => params["slug"],
    "title" => params["title"],
    "force" => true,
  })

  options = Hash[@site.config["octodmin"]["front_matter"].keys.map do |key|
    [key, params[key]]
  end]

  content = params["content"].gsub("\r\n", "\n").strip
  result = "---\n#{options.map { |k, v| "#{k}: \"#{v}\"" }.join("\n")}\n---\n\n#{content}\n"
  octopost.instance_variable_set(:@content, result)
  octopost.write

  @post = post_for(octopost)

  # Add/reset post just in case
  git = Git.open(Octodmin::App.dir)
  git.add(octopost.path)
  git.reset(octopost.path)
end

Private Instance Methods

added?() click to toggle source
# File lib/octodmin/post.rb, line 149
def added?
  has_status?(@post, :untracked) && !changed? && !deleted?
end
changed?() click to toggle source
# File lib/octodmin/post.rb, line 153
def changed?
  has_status?(@post, :changed)
end
deleted?() click to toggle source
# File lib/octodmin/post.rb, line 157
def deleted?
  has_status?(@post, :deleted)
end
has_status?(post, status) click to toggle source
# File lib/octodmin/post.rb, line 142
def has_status?(post, status)
  path = File.join(@site.source, post.path)
  changed = @site.status.public_send(status).keys
  paths = changed.map { |path| File.join(Octodmin::App.dir, path) }
  paths.include?(path)
end
octopost_for(post) click to toggle source
# File lib/octodmin/post.rb, line 127
def octopost_for(post)
  Octopress::Post.new(Octopress.site, {
    "path" => post.path,
    "date" => post.date,
    "slug" => post.slug,
    "title" => post.title,
  })
end
post_for(octopost) click to toggle source
# File lib/octodmin/post.rb, line 136
def post_for(octopost)
  @site.posts.find do |post|
    File.join(@site.site.source, post.post.path) == octopost.path
  end.post
end