module SearchableBy::Concern

Public Instance Methods

search_by(query, profile: :default) click to toggle source

@param [String] query the search query @return [ActiveRecord::Relation] the scoped relation

# File lib/searchable_by/concern.rb, line 21
def search_by(query, profile: :default)
  config  = _searchable_by_profiles[profile]
  columns = config.columns
  return all if columns.empty?

  values = Util.norm_values(query, min_length: config.min_length).first(config.max_terms)
  return all if values.empty?

  columns.each do |col|
    col.node ||= col.attr.is_a?(Proc) ? col.attr.call : arel_table[col.attr]
  end
  clauses = Util.build_clauses(columns, values)
  return all if clauses.empty?

  scope = instance_exec(&config.scoping)
  clauses.each do |clause|
    scope = scope.where(clause)
  end
  scope
end
searchable_by(profile = :default, max_terms: nil, min_length: 0, **options, &block) click to toggle source
# File lib/searchable_by/concern.rb, line 14
def searchable_by(profile = :default, max_terms: nil, min_length: 0, **options, &block)
  _searchable_by_profiles[profile].configure(max_terms, min_length, **options, &block)
  _searchable_by_profiles
end