Sha256: 378f635f38a55ccc89dc27c0731a080ac9f2a01d5c419203c8570708eedce38e

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
module Spree
  module Callbackable
    extend ActiveSupport::Concern

    included do
      include Elasticsearch::Model

      after_commit :index_document, on: :create
      after_commit :update_document, on: :update
      after_commit :delete_document, on: :destroy

      ##
      # Index document into elasticsearch
      #
      delegate :index_document, to: :__elasticsearch__

      ##
      # Updates elasticsearch document from featured model
      #
      def update_document
        __elasticsearch__.update_document
      rescue Elasticsearch::Transport::Transport::Errors::NotFound
        Rails.logger.warn "[#{self.class}] Document with id #{id} not found on elasticsearch cluster"
        __elasticsearch__.index_document
      end

      def delete_document
        __elasticsearch__.delete_document
      rescue Elasticsearch::Transport::Transport::Errors::NotFound
        Rails.logger.warn "[#{self.class}] Document with id #{id} not found on elasticsearch cluster"
      end

      private

      def version_options
        {
          version: (updated_at.to_f * 1000).to_i, # Include Milliseconds
          version_type: 'external'
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_recommendations-0.0.2 app/models/concerns/spree/callbackable.rb