petities/petitions.eu

View on GitHub
app/models/update.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
class Update < ApplicationRecord
  self.table_name = 'newsitems'

  before_validation :fill_date

  extend FriendlyId
  friendly_id :slug_candidates, use: :slugged

  default_scope { order('created_at DESC') }

  scope :show_on_home, -> { where(show_on_home: true) }

  belongs_to :petition
  belongs_to :office

  has_many :images, as: :imageable, dependent: :destroy

  validates :title, presence: true
  validates :text, presence: true

  def slug_candidates
    [:title, [:title, :date]]
  end

  private

  def fill_date
    self.date = Time.zone.today if date.blank?
  end
end