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
- .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
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
#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
87 88 89 90 91 92 |
# File 'app/models/post.rb', line 87 def all file_extensions = Postmarkdown::Config.[:markdown_file_extensions].join(',') @@posts ||= Dir.glob("#{directory}/*.{#{file_extensions}}").map do |filename| Post.new filename end.select(&:visible?).sort_by(&:date).reverse end |
.directory ⇒ Object
94 95 96 |
# File 'app/models/post.rb', line 94 def directory Rails.root.join('app', 'posts') end |
.feed ⇒ Object
121 122 123 |
# File 'app/models/post.rb', line 121 def feed all.first(10) end |
.feed_last_modified ⇒ Object
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 |
.first ⇒ Object
113 114 115 |
# File 'app/models/post.rb', line 113 def first all.first end |
.last ⇒ Object
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
#author ⇒ Object
59 60 61 |
# File 'app/models/post.rb', line 59 def [:author] end |
#content ⇒ Object
46 47 48 49 |
# File 'app/models/post.rb', line 46 def content load_content @content end |
#date ⇒ Object
67 68 69 |
# File 'app/models/post.rb', line 67 def date @date ||= Time.zone.parse([:date] || @date_str).to_date end |
#email ⇒ Object
63 64 65 |
# File 'app/models/post.rb', line 63 def email [:email] end |
#metadata ⇒ Object
41 42 43 44 |
# File 'app/models/post.rb', line 41 def load_content @metadata end |
#permalink_format ⇒ Object
33 34 35 |
# File 'app/models/post.rb', line 33 def permalink_format Postmarkdown::Config.[:permalink_format] end |
#summary ⇒ Object
55 56 57 |
# File 'app/models/post.rb', line 55 def summary [:summary] end |
#timestamp ⇒ Object Also known as: last_modified
73 74 75 |
# File 'app/models/post.rb', line 73 def date.to_time_in_current_zone end |
#title ⇒ Object
51 52 53 |
# File 'app/models/post.rb', line 51 def title [:title] || slug.titleize end |
#to_key ⇒ Object
37 38 39 |
# File 'app/models/post.rb', line 37 def to_key [slug] end |
#to_param ⇒ Object
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_s ⇒ Object
82 83 84 |
# File 'app/models/post.rb', line 82 def to_s "#{title.inspect} (#{slug})" end |
#visible? ⇒ Boolean
78 79 80 |
# File 'app/models/post.rb', line 78 def visible? <= Time.zone.now end |