Class: Post
- Inherits:
-
Object
- Object
- Post
- 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
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
- .all ⇒ Object
- .directory ⇒ Object
- .feed ⇒ Object
- .feed_last_modified ⇒ Object
- .find(id) ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .reset! ⇒ Object
- .visible ⇒ Object
- .where(conditions = {}) ⇒ Object
Instance Method Summary collapse
- #author ⇒ Object
- #content ⇒ Object
- #date ⇒ Object
- #email ⇒ Object
-
#initialize(path) ⇒ Post
constructor
A new instance of Post.
- #metadata ⇒ Object
- #permalink_format ⇒ Object
- #summary ⇒ Object
- #timestamp ⇒ Object (also: #last_modified)
- #title ⇒ Object
- #to_key ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #visible? ⇒ Boolean (also: #visible)
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
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
10 11 12 |
# File 'app/models/post.rb', line 10 def slug @slug end |
Class Method Details
.all ⇒ Object
91 92 93 94 95 96 |
# File 'app/models/post.rb', line 91 def all file_extensions = Postmarkdown::Config.[: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 |
.directory ⇒ Object
102 103 104 |
# File 'app/models/post.rb', line 102 def directory Rails.root.join('app', 'posts') end |
.feed ⇒ Object
131 132 133 |
# File 'app/models/post.rb', line 131 def feed visible.first(10) end |
.feed_last_modified ⇒ Object
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) = {:to_param => id} [:visible] = true unless Postmarkdown::Config.[:allow_preview] where().first or raise ActiveRecord::RecordNotFound, "Could not find post with ID #{id.inspect}" end |
.first ⇒ Object
123 124 125 |
# File 'app/models/post.rb', line 123 def first visible.first end |
.last ⇒ Object
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 |
.visible ⇒ Object
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
#author ⇒ Object
62 63 64 |
# File 'app/models/post.rb', line 62 def [:author] end |
#content ⇒ Object
49 50 51 52 |
# File 'app/models/post.rb', line 49 def content load_content @content end |
#date ⇒ Object
70 71 72 |
# File 'app/models/post.rb', line 70 def date @date ||= Time.zone.parse([:date] || @date_str).to_date end |
#email ⇒ Object
66 67 68 |
# File 'app/models/post.rb', line 66 def email [:email] end |
#metadata ⇒ Object
44 45 46 47 |
# File 'app/models/post.rb', line 44 def load_content @metadata end |
#permalink_format ⇒ Object
36 37 38 |
# File 'app/models/post.rb', line 36 def permalink_format Postmarkdown::Config.[:permalink_format] end |
#summary ⇒ Object
58 59 60 |
# File 'app/models/post.rb', line 58 def summary [:summary] end |
#timestamp ⇒ Object Also known as: last_modified
76 77 78 |
# File 'app/models/post.rb', line 76 def date.respond_to?(:in_time_zone) ? date.in_time_zone : date.to_time_in_current_zone end |
#title ⇒ Object
54 55 56 |
# File 'app/models/post.rb', line 54 def title [:title] || slug.titleize end |
#to_key ⇒ Object
40 41 42 |
# File 'app/models/post.rb', line 40 def to_key [slug] end |
#to_param ⇒ Object
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_s ⇒ Object
86 87 88 |
# File 'app/models/post.rb', line 86 def to_s "#{title.inspect} (#{slug})" end |
#visible? ⇒ Boolean Also known as: visible
81 82 83 |
# File 'app/models/post.rb', line 81 def visible? <= Time.zone.now end |