Class: Schnitzelpress::Post
- Inherits:
-
Object
- Object
- Schnitzelpress::Post
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/schnitzelpress/post.rb
Instance Method Summary collapse
- #article_post? ⇒ Boolean
- #day ⇒ Object
- #disqus? ⇒ Boolean
- #disqus_identifier ⇒ Object
- #draft? ⇒ Boolean
- #home_page? ⇒ Boolean
- #link_post? ⇒ Boolean
- #month ⇒ Object
- #nil_if_blank ⇒ Object
- #page? ⇒ Boolean
- #post? ⇒ Boolean
- #previous_slugs ⇒ Object
- #published? ⇒ Boolean
- #published_at=(v) ⇒ Object
- #render ⇒ Object
- #set_defaults ⇒ Object
- #slug ⇒ Object
- #slug=(v) ⇒ Object
- #to_html ⇒ Object
- #to_url ⇒ Object
- #update_body_html ⇒ Object
- #validate_slug ⇒ Object
- #year ⇒ Object
Instance Method Details
#article_post? ⇒ Boolean
149 150 151 |
# File 'lib/schnitzelpress/post.rb', line 149 def article_post? link.nil? end |
#day ⇒ Object
161 162 163 |
# File 'lib/schnitzelpress/post.rb', line 161 def day published_at.day end |
#disqus? ⇒ Boolean
177 178 179 |
# File 'lib/schnitzelpress/post.rb', line 177 def disqus? disqus && published? end |
#disqus_identifier ⇒ Object
61 62 63 |
# File 'lib/schnitzelpress/post.rb', line 61 def disqus_identifier "post-#{id}" end |
#draft? ⇒ Boolean
141 142 143 |
# File 'lib/schnitzelpress/post.rb', line 141 def draft? status == :draft end |
#home_page? ⇒ Boolean
165 166 167 |
# File 'lib/schnitzelpress/post.rb', line 165 def home_page? slug == 'home' end |
#link_post? ⇒ Boolean
145 146 147 |
# File 'lib/schnitzelpress/post.rb', line 145 def link_post? link.present? end |
#month ⇒ Object
157 158 159 |
# File 'lib/schnitzelpress/post.rb', line 157 def month published_at.month end |
#nil_if_blank ⇒ Object
102 103 104 105 106 107 |
# File 'lib/schnitzelpress/post.rb', line 102 def nil_if_blank attributes.keys.each do |attr| self[attr].strip! if self[attr].is_a?(String) self[attr] = nil if self[attr] == "" end end |
#page? ⇒ Boolean
133 134 135 |
# File 'lib/schnitzelpress/post.rb', line 133 def page? !post? end |
#post? ⇒ Boolean
129 130 131 |
# File 'lib/schnitzelpress/post.rb', line 129 def post? published_at.present? end |
#previous_slugs ⇒ Object
69 70 71 |
# File 'lib/schnitzelpress/post.rb', line 69 def previous_slugs slugs[0..-2] end |
#published? ⇒ Boolean
137 138 139 |
# File 'lib/schnitzelpress/post.rb', line 137 def published? status == :published end |
#published_at=(v) ⇒ Object
73 74 75 76 |
# File 'lib/schnitzelpress/post.rb', line 73 def published_at=(v) v = Chronic.parse(v) if v.is_a?(String) super(v) end |
#render ⇒ Object
122 123 124 125 126 127 |
# File 'lib/schnitzelpress/post.rb', line 122 def render @@markdown ||= Redcarpet::Markdown.new(MarkdownRenderer, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true) @@markdown.render(body.to_s) end |
#set_defaults ⇒ Object
85 86 87 88 89 |
# File 'lib/schnitzelpress/post.rb', line 85 def set_defaults if slug.blank? self.slug = (title || body.truncate(40, :separator => ' ')).parameterize end end |
#slug ⇒ Object
65 66 67 |
# File 'lib/schnitzelpress/post.rb', line 65 def slug slugs.try(:last) end |
#slug=(v) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/schnitzelpress/post.rb', line 78 def slug=(v) unless v.blank? slugs.delete(v) slugs << v end end |
#to_html ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/schnitzelpress/post.rb', line 113 def to_html if body_html.nil? update_body_html save end body_html end |
#to_url ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/schnitzelpress/post.rb', line 169 def to_url if home_page? '/' else published_at.present? ? "/#{sprintf '%04d', year}/#{sprintf '%02d', month}/#{sprintf '%02d', day}/#{slug}/" : "/#{slug}/" end end |
#update_body_html ⇒ Object
109 110 111 |
# File 'lib/schnitzelpress/post.rb', line 109 def update_body_html self.body_html = render end |
#validate_slug ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/schnitzelpress/post.rb', line 91 def validate_slug conflicting_posts = Post.where(:slugs => slug) if published_at.present? conflicting_posts = conflicting_posts.for_day(published_at.year, published_at.month, published_at.day) end if conflicting_posts.any? && conflicting_posts.first != self errors[:slug] = "This slug is already in use by another post." end end |
#year ⇒ Object
153 154 155 |
# File 'lib/schnitzelpress/post.rb', line 153 def year published_at.year end |