Implement validation for announcements

This commit is contained in:
Karina Kwiatek 2020-04-19 21:11:22 +01:00
parent 6187cb0b6c
commit 2ecc746e23
1 changed files with 15 additions and 0 deletions

View File

@ -1,3 +1,18 @@
class Announcement < ApplicationRecord
belongs_to :user
validates :content, presence: true
validates :starts_at, presence: true
validates :link_href, presence: true, if: -> { link_text.present? }
validate :starts_at, :validate_date_range
def link_present?
link_text.present?
end
def validate_date_range
if starts_at > ends_at
errors.add(:starts_at, "Start date must be before end date")
end
end
end