2016-12-12 22:28:10 -05:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 11:23:47 -08:00
|
|
|
|
2016-10-16 20:22:00 -04:00
|
|
|
class AccessRequest < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :map
|
2017-02-09 16:53:19 -05:00
|
|
|
has_one :user_map
|
2017-11-25 11:23:47 -08:00
|
|
|
|
2017-02-09 16:53:19 -05:00
|
|
|
after_create :after_created_async
|
2016-10-16 20:22:00 -04:00
|
|
|
|
|
|
|
def approve
|
|
|
|
self.approved = true
|
|
|
|
self.answered = true
|
2016-12-12 22:28:10 -05:00
|
|
|
save
|
2016-12-09 12:04:17 -05:00
|
|
|
|
2016-12-11 17:29:48 -05:00
|
|
|
Mailboxer::Notification.where(notified_object: self).find_each do |notification|
|
2016-12-09 12:04:17 -05:00
|
|
|
Mailboxer::Receipt.where(notification: notification).update_all(is_read: true)
|
|
|
|
end
|
2016-12-11 17:29:48 -05:00
|
|
|
|
2017-02-09 16:53:19 -05:00
|
|
|
UserMap.create(user: user, map: map, access_request: self)
|
2016-10-16 20:22:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def deny
|
|
|
|
self.approved = false
|
|
|
|
self.answered = true
|
2016-12-12 22:28:10 -05:00
|
|
|
save
|
2016-12-09 12:04:17 -05:00
|
|
|
|
2016-12-11 17:29:48 -05:00
|
|
|
Mailboxer::Notification.where(notified_object: self).find_each do |notification|
|
2016-12-09 12:04:17 -05:00
|
|
|
Mailboxer::Receipt.where(notification: notification).update_all(is_read: true)
|
|
|
|
end
|
2016-10-16 20:22:00 -04:00
|
|
|
end
|
2017-11-25 11:23:47 -08:00
|
|
|
|
2017-02-09 16:53:19 -05:00
|
|
|
protected
|
2017-11-25 11:23:47 -08:00
|
|
|
|
2017-02-09 16:53:19 -05:00
|
|
|
def after_created_async
|
|
|
|
NotificationService.access_request(self)
|
|
|
|
end
|
|
|
|
handle_asynchronously :after_created_async
|
2016-10-16 20:22:00 -04:00
|
|
|
end
|