Class: Staticpress::Content::Post
Instance Attribute Summary
Attributes inherited from Base
#params, #template_types
Class Method Summary
collapse
Instance Method Summary
collapse
#find_supported_extensions, #gather_resources_from, #load_resource
Methods inherited from Base
#==, #content, #content_type, #exist?, find_by_url_path, #full_title, #layout, #markup_template?, #meta, #optional_param_defaults, #output_path, #published?, #raw, #render, #render_partial, #save, #save!, #template_context, #template_engine_options, #template_extension, #template_path_content, #theme, theme, #to_s, type, #url_path
Methods included from Helpers
#config, #extensionless_basename, #extensionless_path, #hash_from_array, #hash_from_match_data, #paginate, #settings, #spider_map, #titleize
Constructor Details
#initialize(params) ⇒ Post
Returns a new instance of Post.
5
6
7
8
9
|
# File 'lib/staticpress/content/post.rb', line 5
def initialize(params)
super
@template_types = find_supported_extensions template_path
end
|
Class Method Details
.all ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/staticpress/content/post.rb', line 45
def self.all
if (posts_dir = Staticpress.blog_path + config.posts_source_path).directory?
posts_dir.children.map { |post| find_by_path post }
else
[]
end
end
|
.create(format, title) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/staticpress/content/post.rb', line 57
def self.create(format, title)
now = Time.now.utc
created_on = "#{now.year}-#{'%02d' % now.month}-#{'%02d' % now.day}"
name = title.gsub(/ /, '-').downcase
filename = "#{created_on}-#{name}.#{format}"
destination = Staticpress.blog_path + config.posts_source_path + filename
FileUtils.mkdir_p destination.dirname
destination.open('w') { |f| f.write template }
end
|
.find_by_path(path) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/staticpress/content/post.rb', line 69
def self.find_by_path(path)
if path.file?
stubs = Staticpress::Route::REGEX_STUBS
regex = /#{stubs[:year].regex}-#{stubs[:month].regex}-#{stubs[:day].regex}-#{stubs[:title].regex}\.(.+)/
if filename_parts = path.basename.to_s.match(regex)
new hash_from_match_data(filename_parts)
end
end
end
|
.published ⇒ Object
53
54
55
|
# File 'lib/staticpress/content/post.rb', line 53
def self.published
all.select &:published?
end
|
.template ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/staticpress/content/post.rb', line 80
def self.template
now = Time.now.utc
<<-TEMPLATE
---
created_at: #{now}
---
in post
TEMPLATE
end
|
Instance Method Details
#<=>(other) ⇒ Object
11
12
13
|
# File 'lib/staticpress/content/post.rb', line 11
def <=>(other)
other.respond_to?(:created_at) ? (created_at <=> other.created_at) : super
end
|
#created_at ⇒ Object
15
16
17
|
# File 'lib/staticpress/content/post.rb', line 15
def created_at
meta.created_at ? meta.created_at.utc : created_on
end
|
#created_on ⇒ Object
19
20
21
|
# File 'lib/staticpress/content/post.rb', line 19
def created_on
Time.utc params[:year], params[:month], params[:day]
end
|
#preferred_layout_names ⇒ Object
23
24
25
|
# File 'lib/staticpress/content/post.rb', line 23
def preferred_layout_names
[meta.layout, :post]
end
|
#template_path ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/staticpress/content/post.rb', line 27
def template_path
name = [
params[:year],
params[:month],
params[:day],
"#{params[:title]}#{template_extension}"
].join('-')
Staticpress.blog_path + config.posts_source_path + name
end
|
#title ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/staticpress/content/post.rb', line 37
def title
if meta.title
meta.title
else
titleize(params[:title])
end
end
|