Sha256: 7da43e606d88a78aad6b8eb149b097b900deb61b0f03f8f7be84d2712ecc3b02

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Notifiable
  class Notification < ActiveRecord::Base
    
    serialize :params
    
    has_many :notification_statuses, :class_name => 'Notifiable::NotificationStatus', :dependent => :destroy
    belongs_to :app, :class_name => 'Notifiable::App'
    
    validates_presence_of :app
    
    def batch  
      yield(self)
      close
    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
      
      notifier = @notifiers[provider]
      if d.is_valid? && !notifier.nil? 
  		  notifier.send_notification(d)
      end
    end
    
    def send_params
      @send_params ||= (self.params ? self.params : {}).merge({:notification_id => self.id})
    end
    
    private
      def notifiers
        @notifiers ||= {}
      end
    
      def close
        notifiers.each_value {|n| n.close}
        @notifiers = nil
        summarise
      end
    
      def summarise
        self.sent_count = self.notification_statuses.count
        self.gateway_accepted_count = self.notification_statuses.where(:status => 0).count
        self.save
      end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
notifiable-rails-0.19.6 lib/notifiable/notification.rb
notifiable-rails-0.19.5 lib/notifiable/notification.rb
notifiable-rails-0.19.4 lib/notifiable/notification.rb
notifiable-rails-0.19.3 lib/notifiable/notification.rb
notifiable-rails-0.19.2 lib/notifiable/notification.rb
notifiable-rails-0.19.1 lib/notifiable/notification.rb
notifiable-rails-0.19.0 lib/notifiable/notification.rb
notifiable-rails-0.17.0 lib/notifiable/notification.rb