Sha256: badcc6098bd3b81839a451ce9fb07a375ce2e1bfe9160e67876b5b2fb67f39cb

Contents?: true

Size: 1.74 KB

Versions: 20

Compression:

Stored size: 1.74 KB

Contents

module Loco
  class Notification < ActiveRecord::Base
    serialize :data, JSON

    validates :obj_class, presence: true
    validates :event, presence: true

    before_validation :prepare

    class << self
      def table_name_prefix; 'loco_' end
    end

    def obj= val
      if val.instance_of? Class
        self.obj_class = val.to_s
      else
        self.obj_class = val.class.name
        self.obj_id = val.id
        @obj = val
      end
    end

    def obj; @obj end

    def recipient= val
      return nil if val.nil?
      return nil if val == :all
      if val.is_a? String
        self.recipient_token = val
      elsif val.instance_of? Class
        self.recipient_class = val.to_s
      else
        self.recipient_class = val.class.name
        self.recipient_id = val.id
      end
    end

    def recipient opts = {}
      return recipient_token if recipient_token
      return nil if recipient_class.nil? && recipient_id.nil?
      return recipient_class.constantize if recipient_id.nil?
      if opts[:shallow]
        return recipient_class.constantize.new id: recipient_id
      end
      recipient_class.constantize.find recipient_id
    end

    def prepare
      set_event
      set_data
    end

    def compact
      [obj_class, obj_id, event, data]
    end

    private

      def set_event
        return if event.present?
        return if obj.instance_of? Class
        self.event = "creating" if obj.new_record?
        self.event = "created" if obj.created_at.present? && obj.created_at == obj.updated_at
        self.event = "updated" if obj.created_at.present? && obj.created_at != obj.updated_at
      end

      def set_data
        self.data ||= {}
        return if obj.nil?
        self.data.merge!(id: obj.id)
      end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
loco-rails-2.5.3 app/models/loco/notification.rb
loco-rails-2.5.2 app/models/loco/notification.rb
loco-rails-2.5.1 app/models/loco/notification.rb
loco-rails-2.5.0 app/models/loco/notification.rb
loco-rails-2.4.0 app/models/loco/notification.rb
loco-rails-2.3.0 app/models/loco/notification.rb
loco-rails-3.0.0 app/models/loco/notification.rb
loco-rails-2.2.2 app/models/loco/notification.rb
loco-rails-2.2.1 app/models/loco/notification.rb
loco-rails-2.2.0 app/models/loco/notification.rb
loco-rails-2.1.0 app/models/loco/notification.rb
loco-rails-2.0.0 app/models/loco/notification.rb
loco-rails-1.5.2 app/models/loco/notification.rb
loco-rails-1.5.1 app/models/loco/notification.rb
loco-rails-1.5.0 app/models/loco/notification.rb
loco-rails-1.4.0 app/models/loco/notification.rb
loco-rails-1.3.3 app/models/loco/notification.rb
loco-rails-1.3.2 app/models/loco/notification.rb
loco-rails-1.3.1 app/models/loco/notification.rb
loco-rails-1.3.0 app/models/loco/notification.rb