Sha256: 249456358c5d9b6ee4478276a45e1b3bcd3922340a7439a96f9c8dc510e083ce

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module Gossiper
  class Notification < ActiveRecord::Base
    STATUSES = %w(pending delivered)

    validates :kind, presence: true

    def user=(user)
      @user           = user
      self.user_class = user.class.to_s
      self.user_id    = user.id
    end

    def user
      @user ||= user_class.constantize.find(user_id)
    end

    def user_class
      read_attribute(:user_class).presence || Gossiper.configuration.default_notification_user_class
    end

    def status
      read_attribute(:status).presence || STATUSES.first
    end

    def deliver
      mail.deliver
      update_delivered_at!
    end

    def deliver!
      mail.deliver!
      update_delivered_at!
    end

    def kind=(value)
      value = value.present? ? value.parameterize.underscore : nil
      write_attribute(:kind, value)
    end

    def method_missing(method, *args, &block)
      STATUSES.each do |status|
        if method.to_s == "#{status}?"
          return self.status == status
        end
      end
      super(method, *args, &block)
    end

    protected
      def mail
        Gossiper::Mailer.mail_for(self)
      end

      def update_delivered_at!
        self.delivered_at = Time.now
        save!
      end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gossiper-0.3.1 app/models/gossiper/notification.rb
gossiper-0.3.0 app/models/gossiper/notification.rb
gossiper-0.1.0 app/models/gossiper/notification.rb