Files
nairobi/app/models/proposal_notification.rb
Javi Martín 8b13daad95 Add and apply rules for multi-line hashes
For the HashAlignment rule, we're using the default `key` style (keys
are aligned and values aren't) instead of the `table` style (both keys
and values are aligned) because, even if we used both in the
application, we used the `key` style a lot more. Furthermore, the
`table` style looks strange in places where there are both very long and
very short keys and sometimes we weren't even consistent with the
`table` style, aligning some keys without aligning other keys.

Ideally we could align hashes to "either key or table", so developers
can decide whether keeping the symmetry of the code is worth it in a
case-per-case basis, but Rubocop doesn't allow this option.
2023-08-18 14:56:16 +02:00

76 lines
1.9 KiB
Ruby

class ProposalNotification < ApplicationRecord
include Graphqlable
include Notifiable
include Searchable
belongs_to :author, class_name: "User"
belongs_to :proposal
validates :title, presence: true
validates :body, presence: true
validates :proposal, presence: true
validate :minimum_interval
scope :public_for_api, -> { where(proposal: Proposal.public_for_api) }
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&.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 moderate_system_email(moderator)
Notification.where(notifiable_type: "ProposalNotification", notifiable: self).destroy_all
Activity.log(moderator, :hide, self)
end
def ignore_flag
update(ignored_at: Time.current)
end
def ignored?
ignored_at.present?
end
def after_restore
update(moderated: false)
end
def searchable_values
{
title => "A",
body => "B"
}
end
def self.search(terms)
pg_search(terms)
end
private
def set_author
update(author_id: proposal.author_id) if proposal
end
end