Skip to content

Commit

Permalink
[#47] Add ActiveText to Alert model with validation to avoid attachme…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
fabionl committed Nov 28, 2021
1 parent 494a8bd commit 6024185
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/alert.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

class Alert < ApplicationRecord
has_rich_text :content

validates :title, :content, presence: true
# Disable ability to add attachments to ActionText (needed because ActiveStorage is not currently setup.
validates :content, no_attachments: true

scope :timeline, -> { order(updated_at: :desc) }
scope :active, -> { where(active: true) }
Expand Down
7 changes: 7 additions & 0 deletions app/models/concerns/no_attachments_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class NoAttachmentsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value.body&.attachments&.any?

record.errors[attribute] << "attachments are not allowed" # I18n.t('errors.messages.attachments_not_allowed')
end
end

0 comments on commit 6024185

Please sign in to comment.