Sha256: c389a40b554cfe363a1231485a732e428787ad19fe3eea252d6d48524e052e19

Contents?: true

Size: 1.74 KB

Versions: 25

Compression:

Stored size: 1.74 KB

Contents

require 'elasticsearch/model'
require 'custom_attributes/fluent_search_query'

module CustomAttributes
  module Searchable
    extend ActiveSupport::Concern

    included do
      include Elasticsearch::Model

      index_name "#{CustomAttributes.config[:search_index_prefix]}-#{name.downcase.pluralize}-#{Rails.env}"

      # For ActiveRecord-based models, use the after_commit callback to
      # protect your data against inconsistencies caused by transaction rollbacks
      if any? { |obj| obj.is_a?(ActiveRecord::Base) }
        after_commit on: [:create] do
          index_document_or_fail
        end

        after_commit on: [:update] do
          index_document_or_fail
        end

        after_commit on: [:destroy] do
          delete_document_or_fail
        end
      else
        # everything else can use the standard implementation
        include Elasticsearch::Model::Callbacks
      end

      # helper function to initialize index
      def self.set_up_search
        __elasticsearch__.create_index!
        __elasticsearch__.refresh_index!
        import
      end

      # entrypoint for the fluent query builder
      def self.searchable
        CustomAttributes::FluentSearchQuery.new new
      end

      # default connection error handling, override in model!
      def self.handle_search_connection_error(exception)
        raise exception
      end

      private

      def index_document_or_fail
        __elasticsearch__.index_document
      rescue Faraday::ConnectionFailed => e
        self.class.handle_search_connection_error(e)
      end

      def delete_document_or_fail
        __elasticsearch__.delete_document
      rescue Faraday::ConnectionFailed => e
        self.class.handle_search_connection_error(e)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
create_custom_attributes-0.6.3 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.6.2 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.6.1 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.25 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.24 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.23 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.22 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.21 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.20 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.19 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.18 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.17 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.16 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.15 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.14 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.13 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.12 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.11 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.10 lib/custom_attributes/concerns/searchable.rb
create_custom_attributes-0.5.9 lib/custom_attributes/concerns/searchable.rb