Class: Blog::Post

Inherits:
ApplicationRecord show all
Defined in:
app/models/blog/post.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tagged_with(name) ⇒ Object



8
9
10
# File 'app/models/blog/post.rb', line 8

def self.tagged_with(name)
  Tag.find_by_name!(name).posts.where('? >= published_at and draft = ?', DateTime.now, false).includes(:user)
end

Instance Method Details

#all_tagsObject



29
30
31
# File 'app/models/blog/post.rb', line 29

def all_tags
  self.tags.map(&:name).join(", ")
end

#all_tags=(names) ⇒ Object



23
24
25
26
27
# File 'app/models/blog/post.rb', line 23

def all_tags=(names)
  self.tags = names.split(",").map do |name|
    Tag.where(name: name.strip).first_or_create!
  end
end

#reading_timeObject



17
18
19
20
21
# File 'app/models/blog/post.rb', line 17

def reading_time
  words_per_minute = 150
  text = Nokogiri::HTML(self.body).at('body').inner_text
  (text.scan(/\w+/).length / words_per_minute).to_i
end

#textObject



12
13
14
15
# File 'app/models/blog/post.rb', line 12

def text
   return teaser if teaser.present? and !teaser.blank?
   body if body.present? and !body.blank?
end