Sha256: 64db5223e93948b1d9d8c34fcbbebd84b4b39b01cdf21c85bceeba87c974b775

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Catarse
class Notification < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
  belongs_to :notification_type
  belongs_to :backer
  # Update was an unfortunate decision, we should rename it soon
  belongs_to :project_update, class_name: "Update", foreign_key: :update_id
  validates_presence_of :user
  scope :not_dismissed, where(dismissed: false)
  attr_accessor :mail_params

  def self.create_notification_once(notification_type_name, user, filter, mail_params = {})
    create_notification(notification_type_name, user, mail_params) if self.where(filter.merge(notification_type_id: find_notification(notification_type_name))).empty?
  end

  def self.create_notification(notification_type_name, user, mail_params = {})
    if (nt = find_notification notification_type_name)
      create! user: user,
        project: mail_params[:project],
        backer: mail_params[:backer],
        project_update: mail_params[:update],
        notification_type: nt,
        mail_params: mail_params
    end
  end

  def send_email
    unless dismissed
      self.update_attributes dismissed: true
      NotificationsMailer.notify(self).deliver
    end
  end

  protected
  def self.find_notification notification_type_name
    NotificationType.where(name: notification_type_name.to_s).first
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catarse_core-1.0.0.beta app/models/catarse/notification.rb