Class: Staticpress::Content::Post

Inherits:
Base
  • Object
show all
Includes:
ResourceContent
Defined in:
lib/staticpress/content/post.rb

Instance Attribute Summary

Attributes inherited from Base

#params, #template_types

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceContent

#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
  # FIXME calculate template_path
  @template_types = find_supported_extensions template_path
end

Class Method Details

.allObject



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

.publishedObject



53
54
55
# File 'lib/staticpress/content/post.rb', line 53

def self.published
  all.select &:published?
end

.templateObject



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_atObject



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_onObject



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_namesObject



23
24
25
# File 'lib/staticpress/content/post.rb', line 23

def preferred_layout_names
  [meta.layout, :post]
end

#template_pathObject



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

#titleObject



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