Sha256: 95f084ae41578fa0cb93a5abe193b6395678b0753f915e855f3e90ddcef9c67f

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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
    
    def summarise
      self.sent_count = self.notification_statuses.count
      self.gateway_accepted_count = self.notification_statuses.where(:status => 0).count
      self.save
    end
    
    private
      def notifiers
        @notifiers ||= {}
      end
    
      def close
        notifiers.each_value {|n| n.close}
        @notifiers = nil
        summarise
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
notifiable-rails-0.22.1 lib/notifiable/notification.rb
notifiable-rails-0.22.0 lib/notifiable/notification.rb
notifiable-rails-0.21.3 lib/notifiable/notification.rb
notifiable-rails-0.21.2 lib/notifiable/notification.rb