Sha256: 7ecbb2768777d85e7c24657cfb7a0569a1b66b818c25760015089d58717e97b6

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 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?
        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

1 entries across 1 versions & 1 rubygems

Version Path
care-0.1.4 lib/care/auto_finder/searchable.rb