Sha256: 8b5bdb9e33aca9b924b6f1068716e39d8e30c0f46bc0823daaa2e1124471b9e5

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true
require_relative 'searcher'

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?
        Care::AutoFinder::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

2 entries across 2 versions & 1 rubygems

Version Path
care-0.1.6 lib/care/auto_finder/searchable.rb
care-0.1.5 lib/care/auto_finder/searchable.rb