Sha256: 3ee7884326254f74c7f044576cb26e2b704e9cde0c41c4d2fb8e6b97316342d1

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# typed: true
# frozen_string_literal: true

module ActsAsMessageable
  module Relation
    extend T::Sig
    extend T::Helpers

    sig { returns(ActsAsMessageable::Model::InstanceMethods) }
    attr_accessor :relation_context

    requires_ancestor { ActiveRecord::Relation }

    # @yield [ActsAsMessageable::Message] message
    # @param [ActiveRecord::Base] context of relation (most of the time current_user object)
    # @return [ActiveRecord::Relation]
    sig { params(context: ActsAsMessageable::Model::InstanceMethods).returns(T.untyped) }
    def process(context = relation_context)
      relation = T.cast(self, ActiveRecord::Relation)

      relation.each do |message|
        yield(message) if block_given?
        context.delete_message(message) if message.removed
        context.restore_message(message) if message.restored
      end
    end

    # @return [Array<ActsAsMessageable::Message>]
    # @return [ActiveRecord::Relation]
    def conversations
      relation = T.cast(self, ActiveRecord::Relation)
      relation.map { |message| message.root.subtree.order('id desc').first }.uniq
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts-as-messageable-1.0.2 lib/acts_as_messageable/relation.rb
acts-as-messageable-1.0.1 lib/acts_as_messageable/relation.rb
acts-as-messageable-1.0.0 lib/acts_as_messageable/relation.rb