Class: Post

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, Gravtastic
Defined in:
app/models/post.rb

Constant Summary collapse

TIME_FORMAT =
/-\d{6}/
DATE_FORMAT =
/\d{4}-\d{2}-\d{2}(?:#{TIME_FORMAT})?/
SLUG_FORMAT =
/[A-Za-z0-9_\-]+/
EXTENSION_FORMAT =
/\.[^.]+/
FILENAME_FORMAT =
/^(#{DATE_FORMAT})-(#{SLUG_FORMAT})(#{EXTENSION_FORMAT})$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Post

Returns a new instance of Post.



19
20
21
22
23
24
25
# File 'app/models/post.rb', line 19

def initialize(path)
  @path = path
  @date_str, @slug = File.basename(path).match(FILENAME_FORMAT).try(:captures)
  unless @date_str.present? && @slug.present?
    raise "Invalid post filename: #{File.basename(path).inspect}"
  end
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



10
11
12
# File 'app/models/post.rb', line 10

def slug
  @slug
end

Class Method Details

.allObject



91
92
93
94
95
96
# File 'app/models/post.rb', line 91

def all
  file_extensions = Postmarkdown::Config.options[:markdown_file_extensions].join(',')
  @@posts ||= Dir.glob("#{directory}/*.{#{file_extensions}}").map do |filename|
    Post.new filename
  end.sort_by { |post| [post.date, post.slug] }.reverse
end

.directoryObject



102
103
104
# File 'app/models/post.rb', line 102

def directory
  Rails.root.join('app', 'posts')
end

.feedObject



131
132
133
# File 'app/models/post.rb', line 131

def feed
  visible.first(10)
end

.feed_last_modifiedObject



135
136
137
# File 'app/models/post.rb', line 135

def feed_last_modified
  feed.first.try(:last_modified) || Time.now.utc
end

.find(id) ⇒ Object



117
118
119
120
121
# File 'app/models/post.rb', line 117

def find(id)
  options = {:to_param => id}
  options[:visible] = true unless Postmarkdown::Config.options[:allow_preview]
  where(options).first or raise ActiveRecord::RecordNotFound, "Could not find post with ID #{id.inspect}"
end

.firstObject



123
124
125
# File 'app/models/post.rb', line 123

def first
  visible.first
end

.lastObject



127
128
129
# File 'app/models/post.rb', line 127

def last
  visible.last
end

.reset!Object



139
140
141
# File 'app/models/post.rb', line 139

def reset!
  @@posts = nil
end

.visibleObject



98
99
100
# File 'app/models/post.rb', line 98

def visible
  all.select(&:visible?)
end

.where(conditions = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'app/models/post.rb', line 106

def where(conditions = {})
  conditions = conditions.symbolize_keys
  conditions.assert_valid_keys :year, :month, :day, :slug, :to_param, :visible
  [:year, :month, :day].each do |key|
    conditions[key] = conditions[key].to_i if conditions[key].present?
  end
  all.select do |post|
    conditions.all? { |key, value| post.send(key) == value }
  end
end

Instance Method Details

#authorObject



62
63
64
# File 'app/models/post.rb', line 62

def author
  [:author]
end

#contentObject



49
50
51
52
# File 'app/models/post.rb', line 49

def content
  load_content
  @content
end

#dateObject



70
71
72
# File 'app/models/post.rb', line 70

def date
  @date ||= Time.zone.parse([:date] || @date_str).to_date
end

#emailObject



66
67
68
# File 'app/models/post.rb', line 66

def email
  [:email]
end

#metadataObject



44
45
46
47
# File 'app/models/post.rb', line 44

def 
  load_content
  @metadata
end


36
37
38
# File 'app/models/post.rb', line 36

def permalink_format
  Postmarkdown::Config.options[:permalink_format]
end

#summaryObject



58
59
60
# File 'app/models/post.rb', line 58

def summary
  [:summary]
end

#timestampObject Also known as: last_modified



76
77
78
# File 'app/models/post.rb', line 76

def timestamp
  date.respond_to?(:in_time_zone) ? date.in_time_zone : date.to_time_in_current_zone
end

#titleObject



54
55
56
# File 'app/models/post.rb', line 54

def title
  [:title] || slug.titleize
end

#to_keyObject



40
41
42
# File 'app/models/post.rb', line 40

def to_key
  [slug]
end

#to_paramObject



27
28
29
30
31
32
33
34
# File 'app/models/post.rb', line 27

def to_param
  case permalink_format
  when :day   then "%04d/%02d/%02d/%s" % [year, month, day, slug]
  when :month then "%04d/%02d/%s" % [year, month, slug]
  when :year  then "%04d/%s" % [year, slug]
  when :slug  then slug
  end
end

#to_sObject



86
87
88
# File 'app/models/post.rb', line 86

def to_s
  "#{title.inspect} (#{slug})"
end

#visible?Boolean Also known as: visible

Returns:

  • (Boolean)


81
82
83
# File 'app/models/post.rb', line 81

def visible?
  timestamp <= Time.zone.now
end