Module: Auth::Concerns::NotificationConcern

Extended by:
ActiveSupport::Concern
Includes:
ChiefModelConcern
Defined in:
app/models/auth/concerns/notification_concern.rb

Instance Method Summary collapse

Instance Method Details

#create_notification_response(response, type) ⇒ Object

@response : a response received after sending a notification. webhook identifier is called before_create by a callback.



199
200
201
202
203
204
# File 'app/models/auth/concerns/notification_concern.rb', line 199

def create_notification_response(response,type)
	notification_response = Auth.configuration.notification_response_class.constantize.new(notification_type: type, parent_notification_id: self.id.to_s)
	notification_response.add_response(response)
	notification_response.set_webhook_identifier(response)
	notification_response.save
end

#email_partialObject

FORMAT METHODS #################### returns the name of a partial to be rendered.



84
85
86
# File 'app/models/auth/concerns/notification_concern.rb', line 84

def email_partial
	"auth/notifier/email.html.erb"
end

#format_for_android(resource) ⇒ Object



93
94
95
# File 'app/models/auth/concerns/notification_concern.rb', line 93

def format_for_android(resource)

end

#format_for_ios(resource) ⇒ Object



97
98
99
# File 'app/models/auth/concerns/notification_concern.rb', line 97

def format_for_ios(resource)

end

#format_for_sms(resource) ⇒ Object



89
90
91
# File 'app/models/auth/concerns/notification_concern.rb', line 89

def format_for_sms(resource)

end

#format_for_web(resource) ⇒ Object



101
102
103
# File 'app/models/auth/concerns/notification_concern.rb', line 101

def format_for_web(resource)

end

#get_resourcesArray

Returns of resource objects.

Returns:

  • (Array)

    of resource objects.



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/auth/concerns/notification_concern.rb', line 209

def get_resources
	resources = []

	JSON.parse(resource_ids).each do |class_name,ids|
		resources << ids.map{|c|
			class_name.capitalize.constantize.find(c)
		}
	end
	JSON.parse(resources_by_query).each do |class_name,query|
		resources << class_name.capitalize.constantize.where(query)
	end
	resources.flatten
end

#get_topicsObject



223
224
225
# File 'app/models/auth/concerns/notification_concern.rb', line 223

def get_topics
	topics || []
end

#max_retry_countObject

default max retry count ###################



107
108
109
# File 'app/models/auth/concerns/notification_concern.rb', line 107

def max_retry_count
	2
end

#resource_class_constantizedObject



231
232
233
# File 'app/models/auth/concerns/notification_concern.rb', line 231

def resource_class_constantized
	self.class.name.capitalize.constantize
end

#send_by_desktop?(resource) ⇒ Boolean

if the notification should be sent by desktop or not. override in the implementing model. default is true

Returns:

  • (Boolean)


140
141
142
# File 'app/models/auth/concerns/notification_concern.rb', line 140

def send_by_desktop?(resource)
	true
end

#send_by_email?(resource) ⇒ Boolean

if the notification should be sent by email or not. override in the implementing model. default is true

Returns:

  • (Boolean)


117
118
119
# File 'app/models/auth/concerns/notification_concern.rb', line 117

def send_by_email?(resource)
	true
end

#send_by_mobile?(resource) ⇒ Boolean

if the notification should be sent by mobile or not. override in the implementing model. default is true

Returns:

  • (Boolean)


132
133
134
# File 'app/models/auth/concerns/notification_concern.rb', line 132

def send_by_mobile?(resource)
	true
end

#send_by_sms?(resource) ⇒ Boolean

if the notification should be sent by sms or not. override in the implementing model. default is true

Returns:

  • (Boolean)


125
126
127
# File 'app/models/auth/concerns/notification_concern.rb', line 125

def send_by_sms?(resource)
	true
end

#send_email(resource) ⇒ Object

Auth.configuration.mailer_class.constantize.notification(resource,notification).deliver_later if resource.email



188
189
190
191
192
193
194
# File 'app/models/auth/concerns/notification_concern.rb', line 188

def send_email(resource)
	if self.email_send_count < max_retry_count
		self.email_send_count+=1
		create_notification_response(yield,"email")
		self.save
	end
end

#send_email_background(resource) ⇒ Object

defaults to sending the email directly. override to send email in a background job.



182
183
184
# File 'app/models/auth/concerns/notification_concern.rb', line 182

def send_email_background(resource)
	send_email(resource)
end

#send_notificationObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/auth/concerns/notification_concern.rb', line 145

def send_notification
	recipients = send_to
	recipients[:resources].map{|r|
		send_email_background(r) if send_by_email?(r)
		send_sms_background(r) if send_by_sms?(r) 
		## so here i still need to implement aws for the notifications.
		## that part is still pending.
		## so i can override send_sms_background to use a handle_asynchronously with the specified delay.
		#r.send_mobile_notification(self) if send_by_mobile?(r)
		#r.send_desktop_notification(self) if send_by_desktop?(r)
	}
end

#send_sms(resource) ⇒ Object

creates a notification response object using the yield block from calling this method. block passed if any must return a string as the yield.



170
171
172
173
174
175
176
# File 'app/models/auth/concerns/notification_concern.rb', line 170

def send_sms(resource)
	if self.sms_send_count < max_retry_count
		self.sms_send_count+=1
		create_notification_response(yield,"sms")
		self.save
	end
end

#send_sms_background(resource) ⇒ Object

SEND SMS NOTIFICATION #################### defaults to just sending the sms direclty. override using a background job to actually call the send_sms method.



161
162
163
# File 'app/models/auth/concerns/notification_concern.rb', line 161

def send_sms_background(resource)
	send_sms(resource)
end

#send_toObject



227
228
229
# File 'app/models/auth/concerns/notification_concern.rb', line 227

def send_to
	{:resources => get_resources, :topics => get_topics}
end