Sha256: 73d97f995450b6bdcd5b3c6ccc33dcfa16ed6b045c847206d7bf2a882ae12b41

Contents?: true

Size: 937 Bytes

Versions: 3

Compression:

Stored size: 937 Bytes

Contents

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments

  enum status: %i(upcoming published rejected)

  counter :hits

  before_save :generate_summary
  before_create :generate_published_at

  def to_param
    self.slug.blank? ? self.id : self.slug
  end

  def self.find_by_slug!(slug)
    Post.find_by(slug: slug) || Post.find_by(id: slug) || raise(ActiveRecord::RecordNotFound.new(slug: slug))
  end

  def published_at
    super || self.updated_at
  end

  def published!
    self.update(published_at: Time.now)
    super
    if self.user_id
      Notification.create(notify_type: 'press_published',
                          user_id: self.user_id,
                          target: self)
    end
  end

  private

  def generate_published_at
    self.published_at = Time.now
  end

  def generate_summary
    if self.summary.blank?
      self.summary = self.body.split("\n").first(10).join("\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
homeland-press-0.2.0 app/models/post.rb
homeland-press-0.1.1 app/models/post.rb
homeland-press-0.1.0 app/models/post.rb