Module: ActsAsNotifiable::NotifiableInstanceMethods

Defined in:
lib/acts_as_notifiable/notifiable.rb

Instance Method Summary collapse

Instance Method Details

#create_notificationObject

Callback method to create the notification



61
62
63
64
65
66
67
68
# File 'lib/acts_as_notifiable/notifiable.rb', line 61

def create_notification
	if self.class.polymorphic?
		polymorphic_array = polymorphic_sender
		Notification.find_or_create_by_user_id_and_notifiable_id_and_notifiable_type_and_parent_type_and_parent_id(self.user_id, self.id, self.class.to_s, polymorphic_array[0], polymorphic_array[1])
	else
		Notification.find_or_create_by_user_id_and_notifiable_id_and_notifiable_type(self.user_id, self.id, self.class.to_s)
	end
end

#mark_as_noticed_by(user, type = nil, type_id = nil) ⇒ Object

mark the notifiable object as read (remove the Notification)



54
55
56
57
58
# File 'lib/acts_as_notifiable/notifiable.rb', line 54

def mark_as_noticed_by(user, type=nil, type_id=nil)
	@notifications = Notification.where(notifiable_id: self.id, notifiable_type: self.class.to_s, user_id: user.id)
	@notifications = self.class.polymorphic_queries(@notifications, type, type_id)
	@notifications.first.destroy if self.unnoticed_by?(user, type, type_id)
end

#polymorphic_senderObject

Returns class of polymorphic association and id of the association in a array



71
72
73
74
75
76
77
# File 'lib/acts_as_notifiable/notifiable.rb', line 71

def polymorphic_sender
	attribute_type = ""
	self.attributes.each{|a| attribute_type = a[0] if a[0] =~ /ble_type/ }
	attribute_type = attribute_type.gsub('_type', '')
	polymorphic_parent = self.send(attribute_type)
	[polymorphic_parent.class.to_s, polymorphic_parent.id]
end

#unnoticed_by?(user, type = nil, type_id = nil) ⇒ Boolean

Check if the notifiable object has been read by the user (no Notification will exist)



47
48
49
50
51
# File 'lib/acts_as_notifiable/notifiable.rb', line 47

def unnoticed_by?(user, type=nil, type_id=nil)
	@notifications = Notification.where(notifiable_id: self.id, notifiable_type: self.class.to_s, user_id: user.id)
	@notifications = self.class.polymorphic_queries(@notifications, type, type_id)
	!@notifications.empty?		
end