Class: Post

Inherits:
Object
  • Object
show all
Includes:
LuckySneaks::StringExtensions, Mongoid::Document, Mongoid::Timestamps, Tanker
Defined in:
app/models/post.rb

Constant Summary collapse

STATES =
['draft', 'published']

Instance Method Summary collapse

Instance Method Details

#draft?Boolean

Returns true if this post is an unpublished draft.

Returns:

  • (Boolean)

    true if draft



64
65
66
# File 'app/models/post.rb', line 64

def draft?
  self.state == 'draft' || self.state.nil?
end

#fix_blog_category_joinObject

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_pathObject

Deprecated.

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_pathObject

Deprecated.

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_indexFixnum

Returns the index of this post in the blog’s collection of posts.

Returns:

  • (Fixnum)

    the path for this post



92
93
94
# File 'app/models/post.rb', line 92

def my_index
  self.blog.posts.published.to_a.index self
end

#next_postPost

Returns the next post in the blog.

Returns:

  • (Post)

    the next post



99
100
101
102
# File 'app/models/post.rb', line 99

def next_post
  i = self.my_index - 1
  self.blog.posts.published[i]
end

#pathString

Returns this post’s path.

Returns:

  • (String)

    the path for this post



107
108
109
# File 'app/models/post.rb', line 107

def path
  "#{self.blog.path}/#{self.slug}".gsub('//', '/')
end

#path_tsString

Returns this post’s path with a trailing slash.

Returns:

  • (String)

    the path for this post



114
115
116
# File 'app/models/post.rb', line 114

def path_ts
  "#{self.path}/"
end

#previous_postPost

Returns the previous post in the blog.

Returns:

  • (Post)

    the previous post



121
122
123
124
125
# File 'app/models/post.rb', line 121

def previous_post
  i = self.my_index + 1
  i = 0 if i == self.blog.posts.published.size
  self.blog.posts.published[i]
end

#publication_dateDateTime

Returns this post’s publication date, defaulting to published-at.

Returns:

  • (DateTime)

    publication or published-at date



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.



128
129
130
# File 'app/models/post.rb', line 128

def publish!
  self.update_attributes :state => 'published', :published_at => Time.zone.now
end

#published?Boolean

Returns whether this post is publically available.

Returns:

  • (Boolean)

    true if published



135
136
137
# File 'app/models/post.rb', line 135

def published?
  self.state == 'published'
end

#search_descriptionString

Returns the first 200 characters of this post’s summary, suitable for use as a meta description.

Returns:

  • (String)

    search description



142
143
144
# File 'app/models/post.rb', line 142

def search_description
  self.summary.gsub(/<\/?[^>]*>/, '')[0..199].html_safe
end

#search_titleString

Returns this post’s title.

Returns:

  • (String)

    title



149
150
151
# File 'app/models/post.rb', line 149

def search_title
  self.title
end

#state=(arg) ⇒ Object

Sets the specified state for this post, forcing lowercase.

Parameters:

  • state (String)


161
162
163
# File 'app/models/post.rb', line 161

def state=(arg)
  self[:state] = arg.downcase
end

#statusString

Returns this post’s status.

Returns:

  • (String)

    status



168
169
170
# File 'app/models/post.rb', line 168

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.



154
155
156
# File 'app/models/post.rb', line 154

def unpublish!
  self.update_attributes :state => 'draft'
end