Sha256: 1ba0b14fcef441c5115d40e197ac09bb33a907c89a32ef30920be105309715fb

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'active_support'

module NotificationHandler
  module NotificationLib
    extend ActiveSupport::Concern

    included do
      self.inheritance_column = :_type_disabled

      after_commit :cache

      serialize :metadata, Hash

      belongs_to :target, polymorphic: true
      belongs_to :object, polymorphic: true, optional: true

      include NotificationHandler::NotificationLib::InstanceMethods

      if defined?(NotificationRenderer)
        include NotificationRenderer::NotificationLib
      end
      if defined?(NotificationPusher)
        include NotificationPusher::NotificationLib
      end
      if defined?(NotificationSettings)
        include NotificationSettings::NotificationLib
      end
    end

    module ClassMethods
      def for_group(group, args: [], attrs: {})
        return if group.nil?

        target_scope = NotificationHandler::Group.find_by_name!(group)
                                                 .target_scope
        target_scope.call(*args)&.map do |target|
          Notification.create(attrs.merge(target: target))
        end
      end
    end

    module InstanceMethods
      def read?
        read
      end

      def unread?
        !read
      end

      private

      def cache
        return unless read_changed?

        target.read_notification_count = target.notifications.read.size
        target.unread_notification_count = target.notifications.unread.size
        target.save!
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notification-handler-3.0.2 lib/notification_handler/notification_lib.rb