Revision a36689ab
Added by Ohad Levy about 7 years ago
app/models/notification.rb | ||
---|---|---|
|
||
belongs_to :notification_blueprint
|
||
belongs_to :initiator, :class_name => User, :foreign_key => 'user_id'
|
||
belongs_to :subject, :polymorphic => true
|
||
has_many :notification_recipients, :dependent => :delete_all
|
||
has_many :recipients, :class_name => User, :through => :notification_recipients, :source => :user
|
||
store :actions, :accessors => [:links], :coder => JSON
|
||
|
||
validates :notification_blueprint, :presence => true
|
||
validates :initiator, :presence => true
|
||
... | ... | |
:in => [AUDIENCE_USER, AUDIENCE_GROUP, AUDIENCE_TAXONOMY,
|
||
AUDIENCE_GLOBAL, AUDIENCE_ADMIN]
|
||
}, :presence => true
|
||
validates :message, :presence => true
|
||
before_validation :set_custom_attributes
|
||
before_create :set_expiry, :set_notification_recipients,
|
||
:set_default_initiator
|
||
|
||
... | ... | |
when AUDIENCE_GLOBAL
|
||
User.reorder('').pluck(:id)
|
||
when AUDIENCE_TAXONOMY
|
||
notification_blueprint.subject.user_ids.uniq
|
||
subject.user_ids.uniq
|
||
when AUDIENCE_USER
|
||
[initiator.id]
|
||
when AUDIENCE_ADMIN
|
||
User.only_admin.reorder('').uniq.pluck(:id)
|
||
when AUDIENCE_GROUP
|
||
notification_blueprint.subject.all_users.uniq.map(&:id) # This needs to be rewritten in usergroups.
|
||
subject.all_users.uniq.map(&:id) # This needs to be rewritten in usergroups.
|
||
end
|
||
end
|
||
|
||
... | ... | |
subscribers = subscriber_ids
|
||
notification_recipients.build subscribers.map{|id| { :user_id => id}}
|
||
end
|
||
|
||
def set_custom_attributes
|
||
return unless notification_blueprint # let validation catch this.
|
||
self.actions = UINotifications::URLResolver.new(
|
||
subject,
|
||
notification_blueprint.actions
|
||
).actions if notification_blueprint.actions.any?
|
||
# copy notification message in case we didn't create a custom one.
|
||
self.message ||= UINotifications::StringParser.new(
|
||
notification_blueprint.message,
|
||
{subject: subject, initator: initiator}
|
||
).to_s
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #18681 - moves polymorphic subject into notification object
also adds host build, destroyed and missing owner UI notifications