Class: Post
- Inherits:
-
Object
- Object
- Post
- Extended by:
- ActiveModel::Naming
- Includes:
- Gravtastic
- Defined in:
- app/models/post.rb
Defined Under Namespace
Classes: Sanitizer
Constant Summary collapse
- FILENAME_FORMAT =
/^(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
- TagHelper =
Class.new.extend ActionView::Helpers::TagHelper
Instance Attribute Summary collapse
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
- .all ⇒ 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
- #content_html ⇒ Object
- #date ⇒ Object
- #email ⇒ Object
-
#initialize(path) ⇒ Post
constructor
A new instance of Post.
- #metadata ⇒ Object
- #permalink_format ⇒ Object
- #summary_html ⇒ 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.
11 12 13 14 |
# File 'app/models/post.rb', line 11 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.
7 8 9 |
# File 'app/models/post.rb', line 7 def slug @slug end |
Class Method Details
.all ⇒ Object
96 97 98 99 100 101 |
# File 'app/models/post.rb', line 96 def all file_extensions = Postmarkdown::Config.[:markdown_file_extensions].join(',') @@posts ||= Dir.glob(Rails.root + "app/posts/*.{#{file_extensions}}").map do |filename| Post.new filename end.select(&:visible?).sort_by(&:date).reverse end |
.feed ⇒ Object
126 127 128 |
# File 'app/models/post.rb', line 126 def feed all.first(10) end |
.feed_last_modified ⇒ Object
130 131 132 |
# File 'app/models/post.rb', line 130 def feed_last_modified feed.first.try(:last_modified) || Time.now.utc end |
.find(id) ⇒ Object
114 115 116 |
# File 'app/models/post.rb', line 114 def find(id) where(:to_param => id).first or raise ActiveRecord::RecordNotFound, "Could not find post with ID #{id.inspect}" end |
.first ⇒ Object
118 119 120 |
# File 'app/models/post.rb', line 118 def first all.first end |
.last ⇒ Object
122 123 124 |
# File 'app/models/post.rb', line 122 def last all.last end |
.reset! ⇒ Object
134 135 136 |
# File 'app/models/post.rb', line 134 def reset! @@posts = nil end |
.where(conditions = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/post.rb', line 103 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
47 48 49 |
# File 'app/models/post.rb', line 47 def [:author] end |
#content ⇒ Object
38 39 40 41 |
# File 'app/models/post.rb', line 38 def content load_content @content end |
#content_html ⇒ Object
74 75 76 |
# File 'app/models/post.rb', line 74 def content_html RDiscount.new(content).to_html.html_safe end |
#date ⇒ Object
55 56 57 |
# File 'app/models/post.rb', line 55 def date @date ||= Time.zone.parse([:date] || @date_str).to_date end |
#email ⇒ Object
51 52 53 |
# File 'app/models/post.rb', line 51 def email [:email] end |
#metadata ⇒ Object
33 34 35 36 |
# File 'app/models/post.rb', line 33 def load_content @metadata end |
#permalink_format ⇒ Object
25 26 27 |
# File 'app/models/post.rb', line 25 def permalink_format Postmarkdown::Config.[:permalink_format] end |
#summary_html ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/post.rb', line 84 def summary_html if [:summary].present? TagHelper.content_tag :p, [:summary] else html = Sanitizer.new.sanitize(content_html) doc = Nokogiri::HTML.fragment(html) para = doc.search('p').detect { |p| p.text.present? } para.try(:to_html).try(:html_safe) end end |
#timestamp ⇒ Object Also known as: last_modified
61 62 63 |
# File 'app/models/post.rb', line 61 def date.to_time_in_current_zone end |
#title ⇒ Object
43 44 45 |
# File 'app/models/post.rb', line 43 def title [:title] || slug.titleize end |
#to_key ⇒ Object
29 30 31 |
# File 'app/models/post.rb', line 29 def to_key [slug] end |
#to_param ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'app/models/post.rb', line 16 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
70 71 72 |
# File 'app/models/post.rb', line 70 def to_s "#{title.inspect} (#{slug})" end |
#visible? ⇒ Boolean
66 67 68 |
# File 'app/models/post.rb', line 66 def visible? <= Time.zone.now end |