environment_notification.rb
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Base
self.table_name = "environment_notifications"
TYPE_LIST = [
"EnvironmentNotificationPlugin::WarningNotification",
"EnvironmentNotificationPlugin::SuccessNotification",
"EnvironmentNotificationPlugin::InformationNotification",
"EnvironmentNotificationPlugin::DangerNotification"
]
attr_accessible :message, :environment_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title
has_many :environment_notifications_users
has_many :users, :through => :environment_notifications_users
validates_presence_of :message
validates_presence_of :environment_id
validate :notification_type_must_be_in_type_list
def notification_type_must_be_in_type_list
unless TYPE_LIST.include?(type)
errors.add(:type, "invalid notification type")
end
end
scope :active, lambda{|environment| { :conditions => { :environment_id => environment.id, :active => true } } }
def self.visibles(environment, user, controller_path)
notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(environment).order('updated_at DESC')
if user
active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id)
notifications = notifications.where(id: active_notifications_ids)
else
notifications = notifications.where(display_to_all_users: true)
end
if controller_path != "home"
notifications = notifications.where(display_only_in_homepage: false)
end
notifications
end
def self.with_popup(environment, user, previous_path)
notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(environment, user, previous_path).where(display_popup: true)
end
end