Sha256: da66aac9e9771caa899267f53c00e072a6314c536f62619463a4ad08e22dac41

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Care::AutoFinder
  #
  # Содержит методы для полнотекстового поиска в коллекции
  #
  # @example
  #   class Finder
  #     include Care::AutoFinder::Searchable
  #
  #     attr_reader :params
  #
  #     search_by :name, :version
  #     search_by :name, address: %i[locality street house flat]
  #       # columns => [:name, {:address=>[:locality, :street, :house, :flat]}]
  #
  #     def call
  #       params = { search: 'сзи' }
  #       search(Document)
  #     end
  #   end
  #
  module Searchable
    extend ActiveSupport::Concern

    def search(items)
      if params[:search].present?
        Searcher.call(items: items, columns: columns, search: params[:search])
      else
        items
      end
    end

    def search_into_array_field(column)
      relation.where("array_to_string(#{column}, '||') LIKE ?", "%#{params[:search]}%")
    end

    def search_into_joined_field(field_path)
      relation.where("#{field_path} LIKE ?", "%#{params[:search]}%")
    end

    module ClassMethods
      attr_accessor :columns

      def search_by(*columns)
        self.columns = columns
      end
    end

    protected

    def columns
      self.class.columns
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
care-0.1.3 lib/care/auto_finder/searchable.rb
care-0.1.1 lib/care/auto_finder/searchable.rb
care-0.1.0 lib/care/auto_finder/searchable.rb