Class: Railspress::Post

Inherits:
ApplicationRecord show all
Includes:
Entity, HasFocalPoint, Taggable
Defined in:
app/models/railspress/post.rb

Constant Summary

Constants included from Entity

Entity::PER_PAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasFocalPoint

#clear_image_override, #focal_point, #focal_point_css, #has_focal_point?, #has_image_override?, #image_css_for, #image_for, #image_override, #reset_focal_point!, #set_image_override

Methods included from Taggable

#tag_list, #tag_list=

Instance Attribute Details

#remove_header_imageObject

Virtual attribute for removing header image via checkbox



49
50
51
# File 'app/models/railspress/post.rb', line 49

def remove_header_image
  @remove_header_image
end

Instance Method Details

#authorObject

Author association - only functional when Railspress.authors_enabled? The author class is configured via Railspress.configure { |c| c.author_class_name = “User” }



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

def author
  return nil unless author_id.present? && Railspress.authors_enabled?
  Railspress.author_class.find_by(id: author_id)
end

#author=(user) ⇒ Object



34
35
36
# File 'app/models/railspress/post.rb', line 34

def author=(user)
  self.author_id = user&.id
end

#calculate_reading_timeObject

Calculate reading time from content word count



116
117
118
119
120
121
122
123
# File 'app/models/railspress/post.rb', line 116

def calculate_reading_time
  return 1 unless content.present?

  words_per_minute = Railspress.words_per_minute
  word_count = content.to_plain_text.split(/\s+/).count
  minutes = (word_count.to_f / words_per_minute).ceil
  [ minutes, 1 ].max
end

#display_statusObject

Returns the status to display in UI (accounts for scheduled state)



65
66
67
68
# File 'app/models/railspress/post.rb', line 65

def display_status
  return "scheduled" if published? && scheduled?
  status
end

#live?Boolean

Predicate: post is live (published_at in past or present)

Returns:

  • (Boolean)


60
61
62
# File 'app/models/railspress/post.rb', line 60

def live?
  published_at.present? && published_at <= Time.current
end

#reading_time_displayObject

Display reading time with fallback to calculated value



126
127
128
# File 'app/models/railspress/post.rb', line 126

def reading_time_display
  reading_time.presence || calculate_reading_time
end

#scheduled?Boolean

Predicate: post is scheduled for future publication

Returns:

  • (Boolean)


55
56
57
# File 'app/models/railspress/post.rb', line 55

def scheduled?
  published_at.present? && published_at > Time.current
end