Sha256: 274d8e14b854854bcb6f1ae5389c03976694fc260c2673252535b36e8e11a763

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Lcms
  module Engine
    module Searchable
      extend ActiveSupport::Concern

      included do
        attr_accessor :skip_indexing

        after_commit :index_document,  on: %i(create update), if: :should_index?
        after_commit :delete_document, on: :destroy, if: :should_index?

        def self.search(term, options = {})
          search_model.search term, options.merge!(model_type: name.underscore)
        end

        private

        def self.search_model
          @search_model ||= Search::Document
        end

        def delete_document
          search_repo.delete search_doc
        rescue Faraday::ConnectionFailed, Elasticsearch::Transport::Transport::Errors::NotFound => e
          Rails.logger.warn("index_document failed: #{e.message}")
        end

        def search_doc
          self.class.search_model.build_from self
        end

        def index_document
          doc = search_doc
          search_repo.save(doc) if doc.present?
        rescue Faraday::ConnectionFailed => e
          Rails.logger.warn("index_document failed: #{e.message}")
        end

        def search_repo
          @search_repo ||= Search::Repository.new
        end

        def should_index?
          !skip_indexing && search_repo.index_exists?
        end

        #
        # Explicitly skip indexing as we do not use it now
        #
        def skip_indexing
          true
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.5.4 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.5.3 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.5.2 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.5.1 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.5.0 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.4.2 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.4.1 app/models/concerns/lcms/engine/searchable.rb
lcms-engine-0.4.0 app/models/concerns/lcms/engine/searchable.rb