Add new routes for the proposal notifications edition and abilities to let moderators edit it (mark as ignored, hide, etc.). The notifications are not flaggable because they doesn't work like that, but in a similar way. The moderator/administrator is in charge of hidding them through the UI, so the normal users don't flag it as inappropriate. New controller Moderation::ProposalNotification to manage the moderators work.
55 lines
1.5 KiB
Ruby
55 lines
1.5 KiB
Ruby
class ProposalNotification < ActiveRecord::Base
|
|
include Graphqlable
|
|
include Notifiable
|
|
|
|
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
|
|
belongs_to :proposal
|
|
|
|
validates :title, presence: true
|
|
validates :body, presence: true
|
|
validates :proposal, presence: true
|
|
validate :minimum_interval
|
|
|
|
scope :public_for_api, -> { where(proposal_id: Proposal.public_for_api.pluck(:id)) }
|
|
scope :sort_by_created_at, -> { reorder(created_at: :desc) }
|
|
scope :sort_by_moderated, -> { reorder(moderated: :desc) }
|
|
|
|
scope :moderated, -> { where(moderated: true) }
|
|
scope :not_moderated, -> { where(moderated: false) }
|
|
scope :pending_review, -> { moderated.where(ignored_at: nil) }
|
|
scope :ignored, -> { moderated.where.not(ignored_at: nil) }
|
|
|
|
acts_as_paranoid column: :hidden_at
|
|
include ActsAsParanoidAliases
|
|
|
|
after_create :set_author
|
|
|
|
def minimum_interval
|
|
return true if proposal.try(:notifications).blank?
|
|
interval = Setting[:proposal_notification_minimum_interval_in_days]
|
|
minimum_interval = (Time.current - interval.to_i.days).to_datetime
|
|
if proposal.notifications.last.created_at > minimum_interval
|
|
errors.add(:title, I18n.t('activerecord.errors.models.proposal_notification.attributes.minimum_interval.invalid', interval: interval))
|
|
end
|
|
end
|
|
|
|
def notifiable
|
|
proposal
|
|
end
|
|
|
|
def ignore_flag
|
|
update(ignored_at: Time.current)
|
|
end
|
|
|
|
def ignored?
|
|
ignored_at.present?
|
|
end
|
|
|
|
private
|
|
|
|
def set_author
|
|
self.update(author_id: self.proposal.author_id) if self.proposal
|
|
end
|
|
|
|
end
|