Sha256: 53c7373ad1d4a4506afe9afdb788bed78814b702f7be09a966313dae551f5bb8

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module LinkedRails
  module Model
    module Cacheable
      extend ActiveSupport::Concern

      included do
        if respond_to?(:after_commit)
          after_commit :publish_create, on: :create, if: :should_publish_changes
          after_commit :publish_update, on: :update, if: :should_publish_changes
          after_commit :publish_delete, on: :destroy, if: :should_publish_changes
        end
      end

      def cacheable?
        true
      end

      private

      def publish_create
        publish_message('io.ontola.transactions.Created')
      end

      def publish_update
        publish_message('io.ontola.transactions.Updated')
      end

      def publish_delete
        publish_message('io.ontola.transactions.Deleted')
      end

      def publish_message(type)
        LinkedRails::InvalidationStreamWorker.perform_now(type, iri.to_s, self.class.iri.to_s)
      rescue StandardError
        LinkedRails::InvalidationStreamWorker.perform_later(type, iri.to_s, self.class.iri.to_s)
      end

      private

      def should_publish_changes
        cacheable? && !Rails.env.test?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.ge09501794 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gc3dfc6914 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g92825d924 lib/linked_rails/model/cacheable.rb