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
# File 'app/models/post.rb', line 19

def initialize(path)
  @path = path
  @date_str, _, @slug = File.basename(path).match(FILENAME_FORMAT).captures
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



87
88
89
90
91
92
# File 'app/models/post.rb', line 87

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

.directoryObject



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

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

.feedObject



121
122
123
# File 'app/models/post.rb', line 121

def feed
  all.first(10)
end

.feed_last_modifiedObject



125
126
127
# File 'app/models/post.rb', line 125

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

.find(id) ⇒ Object



109
110
111
# File 'app/models/post.rb', line 109

def find(id)
  where(:to_param => id).first or raise ActiveRecord::RecordNotFound, "Could not find post with ID #{id.inspect}"
end

.firstObject



113
114
115
# File 'app/models/post.rb', line 113

def first
  all.first
end

.lastObject



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

def last
  all.last
end

.reset!Object



129
130
131
# File 'app/models/post.rb', line 129

def reset!
  @@posts = nil
end

.where(conditions = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'app/models/post.rb', line 98

def where(conditions = {})
  conditions = conditions.symbolize_keys
  conditions.assert_valid_keys :year, :month, :day, :slug, :to_param
  [: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



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

def author
  [:author]
end

#contentObject



46
47
48
49
# File 'app/models/post.rb', line 46

def content
  load_content
  @content
end

#dateObject



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

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

#emailObject



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

def email
  [:email]
end

#metadataObject



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

def 
  load_content
  @metadata
end


33
34
35
# File 'app/models/post.rb', line 33

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

#summaryObject



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

def summary
  [:summary]
end

#timestampObject Also known as: last_modified



73
74
75
# File 'app/models/post.rb', line 73

def timestamp
  date.to_time_in_current_zone
end

#titleObject



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

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

#to_keyObject



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

def to_key
  [slug]
end

#to_paramObject



24
25
26
27
28
29
30
31
# File 'app/models/post.rb', line 24

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



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

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

#visible?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/post.rb', line 78

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