ref: master
plugins/admin_notifications/models/admin_notifications_plugin/notification.rb
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 51 52 53 54 55 56 |
class AdminNotificationsPlugin::Notification < ApplicationRecord self.table_name = "admin_notifications_plugin_notifications" TYPE_LIST = [ "AdminNotificationsPlugin::WarningNotification", "AdminNotificationsPlugin::SuccessNotification", "AdminNotificationsPlugin::InformationNotification", "AdminNotificationsPlugin::DangerNotification" ] attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title, :target has_many :notifications_users, :class_name => "AdminNotificationsPlugin::NotificationsUser" has_many :users, :through => :notifications_users belongs_to :target, :polymorphic => true validates_presence_of :message validates_presence_of :target_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{|target| where(:target_id => (target.kind_of?(Organization) ? [target.id, target.environment.id] : target.id), :active => true)} def self.visibles(target, user, controller_path) notifications = AdminNotificationsPlugin::Notification.active(target).order('updated_at DESC') if user active_notifications_ids = notifications.pluck(:id) - user.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.not("display_only_in_homepage = ? AND target_type = ?",true,"Environment") if controller_path != "profile" notifications = notifications.where.not("display_only_in_homepage = ? AND target_type = ?",true,"Profile") end end notifications end def self.with_popup(target, user, previous_path) AdminNotificationsPlugin::Notification.visibles(target, user, previous_path).where(display_popup: true) end end |