Class: Post
- Inherits:
-
Object
- Object
- Post
- Includes:
- LuckySneaks::StringExtensions, 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.
-
#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.
64 65 66 |
# File 'app/models/post.rb', line 64 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.
70 71 72 73 74 75 |
# File 'app/models/post.rb', line 70 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
78 79 80 81 |
# File 'app/models/post.rb', line 78 def full_path warn "[DEPRECATION] `full_path` is deprecated. Please use `path` instead." self.path end |
#humanize_path ⇒ Object
Please use #path instead
84 85 86 87 |
# File 'app/models/post.rb', line 84 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.
92 93 94 |
# File 'app/models/post.rb', line 92 def my_index self.blog.posts.index(self) end |
#next_post ⇒ Post
Returns the next post in the blog.
99 100 101 102 103 |
# File 'app/models/post.rb', line 99 def next_post i = self.my_index + 1 i = 0 if i > (self.blog.posts.size - 1) self.blog.posts[i] end |
#path ⇒ String
Returns this post’s path.
108 109 110 |
# File 'app/models/post.rb', line 108 def path "#{self.blog.path}/#{self.slug}".gsub('//', '/') end |
#previous_post ⇒ Post
Returns the previous post in the blog.
115 116 117 118 119 |
# File 'app/models/post.rb', line 115 def previous_post i = self.my_index - 1 i = self.blog.posts.size - 1 if i < 0 self.blog.posts[i] end |
#publication_date ⇒ DateTime
Returns this post’s publication date, defaulting to published-at.
55 56 57 |
# File 'app/models/post.rb', line 55 def publication_date self[:publication_date] || self.published_at end |
#publish! ⇒ Object
Publishes this post so it’s publically available.
122 123 124 |
# File 'app/models/post.rb', line 122 def publish! self.update_attributes :state => 'published', :published_at => Time.zone.now end |
#published? ⇒ Boolean
Returns whether this post is publically available.
129 130 131 |
# File 'app/models/post.rb', line 129 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.
136 137 138 |
# File 'app/models/post.rb', line 136 def search_description self.summary.gsub(/<\/?[^>]*>/, '')[0..199].html_safe end |
#search_title ⇒ String
Returns this post’s title.
143 144 145 |
# File 'app/models/post.rb', line 143 def search_title self.title end |
#state=(arg) ⇒ Object
Sets the specified state for this post, forcing lowercase.
155 156 157 |
# File 'app/models/post.rb', line 155 def state=(arg) self[:state] = arg.downcase end |
#status ⇒ String
Returns this post’s status.
162 163 164 |
# File 'app/models/post.rb', line 162 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.
148 149 150 |
# File 'app/models/post.rb', line 148 def unpublish! self.update_attributes :state => 'draft' end |