Class: Octodmin::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/octodmin/post.rb

Constant Summary collapse

ATTRIBUTES_FOR_SERIALIZAION =
Jekyll::Post::ATTRIBUTES_FOR_LIQUID - %w[
  previous
  next
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, post) ⇒ Post

Returns a new instance of Post.



29
30
31
32
# File 'lib/octodmin/post.rb', line 29

def initialize(site, post)
  @site = site
  @post = post
end

Instance Attribute Details

#postObject

Returns the value of attribute post.



3
4
5
# File 'lib/octodmin/post.rb', line 3

def post
  @post
end

Class Method Details

.create(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



10
11
12
13
# File 'lib/octodmin/post.rb', line 10

def self.find(id)
  site = Octodmin::Site.new
  site.posts.find { |post| post.identifier == id }
end

Instance Method Details

#deleteObject



88
89
90
91
92
93
94
# 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

#identifierObject



38
39
40
# File 'lib/octodmin/post.rb', line 38

def identifier
  path.split("/").last.split(".").first
end

#pathObject



34
35
36
# File 'lib/octodmin/post.rb', line 34

def path
  @post.path
end

#restoreObject



96
97
98
99
100
101
102
103
104
105
# 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

#revertObject



107
108
109
110
111
112
113
114
115
116
117
# 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_hashObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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