cirandas.net

ref: master

plugins/push_notification/lib/ext/user.rb


 1
 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
require_dependency "user"
require_relative "../notification_subscription"
require_relative "../notification_settings"
require_relative "../device_token"

class User
  has_many :device_tokens, class_name: "PushNotificationPlugin::DeviceToken", :dependent => :destroy
  has_one :notification_settings, class_name: "PushNotificationPlugin::NotificationSettings",  :dependent => :destroy, :autosave => true

  after_save :save_notification_settings
  after_initialize :setup_notification_settings

  def device_token_list
    device_tokens.map { |t| t.token }
  end

  def enabled_notifications
    notification_settings.notifications
  end

  def enabled_notifications=(notifications)
    notification_settings.notifications=notifications
  end

  private

  def save_notification_settings
    notification_settings.save if notification_settings
  end

  def setup_notification_settings
    self.notification_settings = PushNotificationPlugin::NotificationSettings.new(:user => self) if self.notification_settings.nil?
  end

end