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.



196
197
198
199
200
201
# File 'app/models/auth/concerns/notification_concern.rb', line 196

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

returns the name of a partial to be rendered.



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

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

#format_for_android(resource) ⇒ Object



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

def format_for_android(resource)

end

#format_for_ios(resource) ⇒ Object



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

def format_for_ios(resource)

end

#format_for_sms(resource) ⇒ Object



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

def format_for_sms(resource)

end

#format_for_web(resource) ⇒ Object



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

def format_for_web(resource)

end

#get_resourcesArray

Returns of resource objects.

Returns:

  • (Array)

    of resource objects.



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/auth/concerns/notification_concern.rb', line 206

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



220
221
222
# File 'app/models/auth/concerns/notification_concern.rb', line 220

def get_topics
	topics || []
end

#max_retry_countObject

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



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

def max_retry_count
	2
end

#resource_class_constantizedObject



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

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)


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

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)


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

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)


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

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)


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

def send_by_sms?(resource)
	true
end

#send_email(resource) ⇒ Object

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



185
186
187
188
189
190
191
# File 'app/models/auth/concerns/notification_concern.rb', line 185

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.



179
180
181
# File 'app/models/auth/concerns/notification_concern.rb', line 179

def send_email_background(resource)
	send_email(resource)
end

#send_notificationObject



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

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) 
		#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.



167
168
169
170
171
172
173
# File 'app/models/auth/concerns/notification_concern.rb', line 167

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

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



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

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