Class: Post

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ecrire/app/models/post.rb

Overview

Posts written by users stored in database

Available scopes

Scopes are builtin methods to facilitate retrieving posts from the database. Scopes can also be chained to narrow your query even more.

published: Return posts that are published

drafted: Return posts that are NOT published

search_by_title: Return all post that have a title that match the key you provide

Direct Known Subclasses

Admin::Post

Defined Under Namespace

Classes: Content

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tagsObject

Returns the list of Tags link set for this post



159
160
161
# File 'lib/ecrire/app/models/post.rb', line 159

def tags
  @tags ||= Tag.where("tags.id in (?)", super || [])
end

Instance Method Details

#contentObject

Returns the content of this post. Depending on the current status of this post, it can be one of three things:

  • Compiled content;

  • Markdown content;

  • Empty string.

This method should be used when trying to render the body of a post to a page in HTML.



131
132
133
134
135
136
# File 'lib/ecrire/app/models/post.rb', line 131

def content
  @content ||= begin
    content = read_attribute('content')
    Content.new(content.fetch('raw', ''), content.fetch('html', ''))
  end
end

#draft?Boolean

The opposite of published?

Returns:

  • (Boolean)


116
117
118
# File 'lib/ecrire/app/models/post.rb', line 116

def draft?
  published_at.nil?
end

#excerptObject

Returns the compiled excerpt for this post. The excerpt is based on content but only returns text.

The excerpt is parsed from the content and it also filters out images and header.



145
146
147
# File 'lib/ecrire/app/models/post.rb', line 145

def excerpt
  (self.compiled_excerpt || "").html_safe
end

#header?Boolean

Returns true if an image header was set for this post

Returns:

  • (Boolean)


152
153
154
# File 'lib/ecrire/app/models/post.rb', line 152

def header?
  !self.header.nil? && !self.header.url.blank?
end

#monthObject

Returns the month this post was published



84
85
86
# File 'lib/ecrire/app/models/post.rb', line 84

def month
  published_at.month
end

#publish!Object

Publish this post if it is not yet published

Update the database with the publish date



100
101
102
103
104
# File 'lib/ecrire/app/models/post.rb', line 100

def publish!
  return unless published_at.nil?
  self.published_at = DateTime.now
  save!
end

#published?Boolean

Returns whether this post is published or not

Returns:

  • (Boolean)


109
110
111
# File 'lib/ecrire/app/models/post.rb', line 109

def published?
  !draft?
end

#slugObject

Return the slug link to the current title



70
71
72
# File 'lib/ecrire/app/models/post.rb', line 70

def slug
  self.titles.first.slug
end

#status=(new_status) ⇒ Object

:nodoc:



89
90
91
92
93
# File 'lib/ecrire/app/models/post.rb', line 89

def status=(new_status)
  if new_status.eql? "publish"
    publish!
  end
end

#titleObject

Return the current title for this post



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

def title
  (self.titles.first || self.titles.new)
end

#title=(new_title) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
# File 'lib/ecrire/app/models/post.rb', line 50

def title=(new_title)
  if self.published?
    self.titles.new(name: new_title)
  else
    title = self.titles.first || self.titles.new
    title.post = self
    title.name = new_title
  end
end

#yearObject

Returns the year this post was published



77
78
79
# File 'lib/ecrire/app/models/post.rb', line 77

def year
  published_at.year
end