Class: Post
- Inherits:
-
Object
- Object
- Post
- Includes:
- Mongoid::Document, Mongoid::Timestamps, Tanker
- Defined in:
- app/models/post.rb
Constant Summary collapse
- STATES =
['draft', 'published']
Instance Method Summary collapse
-
#draft? ⇒ Boolean
Returns true if this post is an unpublished draft.
-
#fix_blog_category_join ⇒ Object
Joins this post to its associated blog categories, insuring data integrity at both ends of the join.
-
#full_path ⇒ Object
deprecated
Deprecated.
Please use #path instead
-
#humanize_path ⇒ Object
deprecated
Deprecated.
Please use #path instead
-
#my_index ⇒ Fixnum
Returns the index of this post in the blog’s collection of posts.
-
#next_post ⇒ Post
Returns the next post in the blog.
-
#path ⇒ String
Returns this post’s path.
-
#path_ts ⇒ String
Returns this post’s path with a trailing slash.
-
#previous_post ⇒ Post
Returns the previous post in the blog.
-
#publication_date ⇒ DateTime
Returns this post’s publication date, defaulting to published-at.
-
#publish! ⇒ Object
Publishes this post so it’s publically available.
-
#published? ⇒ Boolean
Returns whether this post is publically available.
-
#search_description ⇒ String
Returns the first 200 characters of this post’s summary, suitable for use as a meta description.
-
#search_title ⇒ String
Returns this post’s title.
-
#state=(arg) ⇒ Object
Sets the specified state for this post, forcing lowercase.
-
#status ⇒ String
Returns this post’s status.
-
#unpublish! ⇒ Object
Reverts this post’s status to draft so it is no longer publically available.
Instance Method Details
#draft? ⇒ Boolean
Returns true if this post is an unpublished draft.
63 64 65 |
# File 'app/models/post.rb', line 63 def draft? self.state == 'draft' || self.state.nil? end |
#fix_blog_category_join ⇒ Object
Joins this post to its associated blog categories, insuring data integrity at both ends of the join.
69 70 71 72 73 74 |
# File 'app/models/post.rb', line 69 def fix_blog_category_join self.blog_categories.each do |cat| cat.post_ids << self.id cat.save end end |
#full_path ⇒ Object
Please use #path instead
77 78 79 80 |
# File 'app/models/post.rb', line 77 def full_path warn "[DEPRECATION] `full_path` is deprecated. Please use `path` instead." self.path end |
#humanize_path ⇒ Object
Please use #path instead
83 84 85 86 |
# File 'app/models/post.rb', line 83 def humanize_path warn "[DEPRECATION] `humanize_path` is deprecated. Please use `path` instead." self.path end |
#my_index ⇒ Fixnum
Returns the index of this post in the blog’s collection of posts.
91 92 93 |
# File 'app/models/post.rb', line 91 def my_index self.blog.posts.published.to_a.index self end |
#next_post ⇒ Post
Returns the next post in the blog.
98 99 100 101 102 |
# File 'app/models/post.rb', line 98 def next_post return if draft? i = self.my_index - 1 self.blog.posts.published[i] end |
#path ⇒ String
Returns this post’s path.
107 108 109 |
# File 'app/models/post.rb', line 107 def path "#{self.blog.path}/#{self.slug}".gsub('//', '/') end |
#path_ts ⇒ String
Returns this post’s path with a trailing slash.
114 115 116 |
# File 'app/models/post.rb', line 114 def path_ts "#{self.path}/" end |
#previous_post ⇒ Post
Returns the previous post in the blog.
121 122 123 124 125 126 |
# File 'app/models/post.rb', line 121 def previous_post return if draft? i = self.my_index + 1 i = 0 if i == self.blog.posts.published.size self.blog.posts.published[i] end |
#publication_date ⇒ DateTime
Returns this post’s publication date, defaulting to published-at.
54 55 56 |
# File 'app/models/post.rb', line 54 def publication_date self[:publication_date] || self.published_at end |
#publish! ⇒ Object
Publishes this post so it’s publically available.
129 130 131 |
# File 'app/models/post.rb', line 129 def publish! self.update_attributes :state => 'published', :published_at => Time.zone.now end |
#published? ⇒ Boolean
Returns whether this post is publically available.
136 137 138 |
# File 'app/models/post.rb', line 136 def published? self.state == 'published' end |
#search_description ⇒ String
Returns the first 200 characters of this post’s summary, suitable for use as a meta description.
143 144 145 |
# File 'app/models/post.rb', line 143 def search_description self.summary.gsub(/<\/?[^>]*>/, '')[0..199].html_safe end |
#search_title ⇒ String
Returns this post’s title.
150 151 152 |
# File 'app/models/post.rb', line 150 def search_title self.title end |
#state=(arg) ⇒ Object
Sets the specified state for this post, forcing lowercase.
162 163 164 |
# File 'app/models/post.rb', line 162 def state=(arg) self[:state] = arg.downcase end |
#status ⇒ String
Returns this post’s status.
169 170 171 |
# File 'app/models/post.rb', line 169 def status self.state ? self.state.capitalize : 'Draft' end |
#unpublish! ⇒ Object
Reverts this post’s status to draft so it is no longer publically available.
155 156 157 |
# File 'app/models/post.rb', line 155 def unpublish! self.update_attributes :state => 'draft' end |