Sha256: ecd7c0a7ba1de9c8956d45e76111bfce51aa42228c342a747221da9c961f2491

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true
module Timeful
  module Model
    # An actor can publish activities.
    #
    # @author Alessandro Desantis
    module Actor
      def self.included(klass)
        klass.include InstanceMethods
      end

      module InstanceMethods # rubocop:disable Style/Documentation
        # Publishes an activity and creates a feed item for each subscriber.
        #
        # @param action [Symbol] the action (or activity type) to create
        # @param target [ActiveRecord::Base] the target the action was taken upon
        #
        # @return [Activity] the created activity
        #
        # @example
        #   actor.publish_activity :post_created, target: post # => #<PostCreatedActivity>
        def publish_activity(action, target:)
          activity = activity_klass(action).create! target: target, actor: self
          DeliverActivityToSubscribersJob.perform_later activity
        end

        private

        def activity_klass(action)
          "#{action.to_s.camelize}Activity".constantize
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timeful-2.0.0 lib/timeful/model/actor.rb