Sha256: c998485d4620c2b75ff4ce5829f2cc06738910c93b79998af4dcb738766aec93

Contents?: true

Size: 1.12 KB

Versions: 26

Compression:

Stored size: 1.12 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

      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

      private

      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

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.g3aeec2263 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gacd5f04d4 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gb0b6aea40 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g792a1b887 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g58af47992 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g0ec6e30ca lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g9e3bcac55 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g222bfeeca lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gb254a2893 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g2c6c26e86 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.ge881247c3 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gfabf7e9f5 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g8c045be3a lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g8d7195543 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gfe77aae19 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.gc866c1806 lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g3e8f5731a lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g58e27be7f lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g96885856b lib/linked_rails/model/cacheable.rb
linked_rails-0.0.4.pre.g1faca9643 lib/linked_rails/model/cacheable.rb