Sha256: 9a2f9416f66d7807ecd820c11f2406af945472a2dffb59c0866c8a3eaca78d8f

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

module SimpleNotifications
  class Record < ActiveRecord::Base
    self.table_name = 'simple_notifications'

    # Class Attribute Accessors
    cattr_accessor :before_notify, :after_notify

    # Associations
    belongs_to :sender, polymorphic: true
    belongs_to :entity, polymorphic: true
    has_many :deliveries, class_name: 'SimpleNotifications::Delivery',
             inverse_of: :simple_notification,
             foreign_key: :simple_notification_id,
             dependent: :destroy
    has_many :read_deliveries, -> {where(is_read: true)},
             class_name: 'SimpleNotifications::Delivery',
             inverse_of: :simple_notification,
             foreign_key: :simple_notification_id,
             dependent: :destroy
    has_many :unread_deliveries, -> {where(is_read: false)},
             class_name: 'SimpleNotifications::Delivery',
             inverse_of: :simple_notification,
             foreign_key: :simple_notification_id,
             dependent: :destroy

    # Scopes
    scope :read, -> {where(is_read: true)}
    scope :unread, -> {where.not(is_read: true)}

    # Validations
    validates :message, presence: true, length: {minimum: 1, maximum: 199}

    # Callbacks
    before_create :before_actions
    after_create_commit :after_actions

    private

    %w(before after).each do |call_type|
      define_method("#{call_type}_actions".to_sym) do
        _method = SimpleNotifications::Base.send(:options)["#{call_type}_notify".to_sym]
        if _method.present?
          if _method.class == Symbol
            entity.method(_method).call if entity.class.instance_methods(false).include?(_method)
          elsif _method.class == Proc
            _method.call
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_notifications-1.1.3 lib/simple_notifications/app/models/simple_notification.rb
simple_notifications-1.1.2 lib/simple_notifications/app/models/simple_notification.rb
simple_notifications-1.1.1 lib/simple_notifications/app/models/simple_notification.rb