Module: ActsAsNotifiable

Defined in:
lib/acts_as_notifiable/notifiable.rb

Defined Under Namespace

Modules: NotifiableInstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_all_unnoticed_by(user, type = nil, type_id = nil) ⇒ Object

Find all notifications by type



17
18
19
20
21
# File 'lib/acts_as_notifiable/notifiable.rb', line 17

def self.find_all_unnoticed_by(user, type=nil, type_id=nil)
	@notifications = Notification.where(notifiable_type: self.to_s, user_id: user.id)
	@notifications = self.polymorphic_queries(@notifications, type, type_id)
	@notifications
end

.mark_all_noticed_by(user, type = nil, type_id = nil) ⇒ Object

Mark all notifications noticed by type



24
25
26
27
28
# File 'lib/acts_as_notifiable/notifiable.rb', line 24

def self.mark_all_noticed_by(user, type=nil, type_id=nil)
	@notifications = Notification.where(user_id: user.id, notifiable_type: self.to_s)
	@notifications = self.polymorphic_queries(@notifications, type, type_id)
	@notifications.destroy_all
end

.polymorphic?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/acts_as_notifiable/notifiable.rb', line 38

def self.polymorphic?
	self.readable_options[:polymorphic] == true
end

.polymorphic_queries(notifications, type, type_id) ⇒ Object



32
33
34
35
36
# File 'lib/acts_as_notifiable/notifiable.rb', line 32

def self.polymorphic_queries(notifications, type, type_id)
	notifications = notifications.where(parent_type: type)
	notifications = notifications.where(parent_id: type_id) if type_id
	notifications
end

Instance Method Details

#acts_as_notifiable(options = {}) ⇒ Object



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
# File 'lib/acts_as_notifiable/notifiable.rb', line 2

def acts_as_notifiable(options={})
	if respond_to?(:class_attribute)
       	class_attribute :readable_options
	else
        class_inheritable_accessor :readable_options
	end
	self.readable_options = options

	has_many :notifications, as: :notifiable, dependent: :destroy

	after_create :create_notification

	# CLASS METHODS

	# Find all notifications by type
	def self.find_all_unnoticed_by(user, type=nil, type_id=nil)
		@notifications = Notification.where(notifiable_type: self.to_s, user_id: user.id)
		@notifications = self.polymorphic_queries(@notifications, type, type_id)
		@notifications
	end

	# Mark all notifications noticed by type
	def self.mark_all_noticed_by(user, type=nil, type_id=nil)
		@notifications = Notification.where(user_id: user.id, notifiable_type: self.to_s)
		@notifications = self.polymorphic_queries(@notifications, type, type_id)
		@notifications.destroy_all
	end

	private

	def self.polymorphic_queries(notifications, type, type_id)
		notifications = notifications.where(parent_type: type)
		notifications = notifications.where(parent_id: type_id) if type_id
		notifications
	end

	def self.polymorphic?
		self.readable_options[:polymorphic] == true
	end

	include NotifiableInstanceMethods
end