about summary refs log tree commit diff stats
path: root/app/models/blog.rb
blob: 495c6eb2768f272b59b66104cba5dc0af1d3cfe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Blog < ApplicationRecord
  has_many :records, as: :recordable, inverse_of: :recordable

  validates :title, :body, presence: true
  validates :slug, presence: true, format: /\A[-a-z0-9]+\z/

  accepts_nested_attributes_for :records, allow_destroy: true

  before_save :set_published_at

  def path
    "/says/#{slug}"
  end

  def posted_at
    if published
      published_at
    else
      updated_at
    end
  end

  private
    def set_published_at
      if self.published
        if self.published_at.blank?
          self.published_at = DateTime.now
        end
      else
        self.published_at = nil
      end
    end
end