Files
nairobi/app/models/proposal_notification.rb
iagirre 894c5d1eca Fix restore notifications from admins page
The restore feature was not working properly. When pushed, the button
was removing the notification from the admins panel, but it was not
restoring in the proposal.

I added an `after_restore` function (that I missed in the first PR)
so that the notification is unmarked as moderated.
2018-07-02 18:19:49 +02:00

59 lines
1.6 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
def after_restore
update(moderated: false)
end
private
def set_author
self.update(author_id: self.proposal.author_id) if self.proposal
end
end