Class: Post

Inherits:
Object
  • Object
show all
Includes:
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



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

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.



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_pathObject

Deprecated.

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_pathObject

Deprecated.

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_indexFixnum

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

Returns:

  • (Fixnum)

    the path for this post



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

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



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

#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
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_dateDateTime

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

Returns:

  • (DateTime)

    publication or published-at date



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.

Returns:

  • (Boolean)

    true if published



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

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



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

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

#search_titleString

Returns this post’s title.

Returns:

  • (String)

    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.

Parameters:

  • state (String)


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

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

#statusString

Returns this post’s status.

Returns:

  • (String)

    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