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.index(self)
end

#next_postPost

Returns the next post in the blog.

Returns:

  • (Post)

    the next post



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

#pathString

Returns this post’s path.

Returns:

  • (String)

    the path for this post



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

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

#previous_postPost

Returns the previous post in the blog.

Returns:

  • (Post)

    the previous post



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_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.



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.

Returns:

  • (Boolean)

    true if published



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

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



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

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

#search_titleString

Returns this post’s title.

Returns:

  • (String)

    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.

Parameters:

  • state (String)


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

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

#statusString

Returns this post’s status.

Returns:

  • (String)

    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