Sha256: 642114f9761f1f31651d914cad30fb60a26f3c78c381bff0628d59dd432d2bfd

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module Notifiable
  class Notification < ActiveRecord::Base
    
    serialize :params
    
    has_many :localized_notifications, :class_name => 'Notifiable::LocalizedNotification', :dependent => :destroy
    accepts_nested_attributes_for :localized_notifications, reject_if: proc { |attributes| attributes['message'].blank? }
    
    belongs_to :app, :class_name => 'Notifiable::App'    
    validates :app, presence: true
    
    def notification_statuses
      Notifiable::NotificationStatus.joins(:localized_notification).where('notifiable_localized_notifications.notification_id' => self.id)
    end
    
    def batch  
      yield(self)
      close
    end
    
    def localized_notification(locale)
      self.localized_notifications.find_by(:locale => locale)
    end
    
    def add_notifiable(notifiable)
      notifiable.device_tokens.each do |d|
        self.add_device_token(d)
      end
    end
    
    def add_device_token(d)
      provider = d.provider.to_sym

      unless notifiers[provider]
        clazz = Notifiable.notifier_classes[provider]
        raise "Notifier #{provider} not configured" unless clazz
        notifier = clazz.new(Rails.env, self)
        self.app.configure(provider, notifier)
        @notifiers[provider] = notifier
      end
      
  		notifiers[provider].send_notification(d) if d.is_valid?
    end
    
    private
      def notifiers
        @notifiers ||= {}
      end
    
      def close
        notifiers.each_value {|n| n.close}
        @notifiers = nil
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
notifiable-rails-0.24.3 lib/notifiable/notification.rb
notifiable-rails-0.24.2 lib/notifiable/notification.rb
notifiable-rails-0.24.1 lib/notifiable/notification.rb
notifiable-rails-0.24.0 lib/notifiable/notification.rb
notifiable-rails-0.23.0 lib/notifiable/notification.rb