foreman/app/models/mail_notification.rb @ 783fb4aa
3a36bdf6 | Stephen Benjamin | class MailNotification < ActiveRecord::Base
|
|
include Authorizable
|
|||
INTERVALS = [N_("Daily"), N_("Weekly"), N_("Monthly")]
|
|||
SUBSCRIPTION_TYPES = %w(alert report)
|
|||
bc9558f7 | Shlomi Zadok | attr_accessible :description, :mailer, :method, :name, :subscriptable, :subscription_type, :category, :queryable
|
|
3a36bdf6 | Stephen Benjamin | ||
has_many :user_mail_notifications, :dependent => :destroy
|
|||
has_many :users, :through => :user_mail_notifications
|
|||
scoped_search :on => :name, :complete_value => true
|
|||
scoped_search :on => :description, :complete_value => true
|
|||
scoped_search :in => :users, :on => :login, :complete_value => true, :rename => :user
|
|||
bb3572ff | Daniel Lobato | scope :subscriptable, -> { where(:subscriptable => true) }
|
|
3a36bdf6 | Stephen Benjamin | ||
validates :name, :presence => true, :uniqueness => true
|
|||
validates :subscription_type, :inclusion => { :in => SUBSCRIPTION_TYPES }, :allow_blank => true
|
|||
validates :mailer, :presence => true
|
|||
validates :method, :presence => true
|
|||
51a88438 | Shlomi Zadok | alias_attribute :mailer_method, :method
|
|
3a36bdf6 | Stephen Benjamin | ||
bb3572ff | Daniel Lobato | default_scope -> { order("mail_notifications.name") }
|
|
3a36bdf6 | Stephen Benjamin | ||
# Easy way to reference the notification to support something like:
|
|||
# MailNotification[:some_error_notification].deliver(options)
|
|||
def self.[](name)
|
|||
self.find_by_name(name)
|
|||
end
|
|||
c8042418 | Dominic Cleal | def deliver(*args)
|
|
# args can be anything really, treat it carefully
|
|||
# handle args=[.., :users => [..]] specially and instantiate a single mailer per user, with :user set on each
|
|||
if args.last.is_a?(Hash) && args.last.has_key?(:users)
|
|||
options = args.pop
|
|||
options.delete(:users).each do |user|
|
|||
mailer.constantize.send(method, *args, options.merge(:user => user)).deliver
|
|||
end
|
|||
else
|
|||
mailer.constantize.send(method, *args).deliver
|
|||
end
|
|||
3a36bdf6 | Stephen Benjamin | end
|
|
end
|