Module: Motor::ApiQuery::Search

Defined in:
lib/motor/api_query/search.rb

Constant Summary collapse

SELECT_COLUMNS_AMOUNT =
2
COLUMN_TYPES =
BuildSchema::SEARCHABLE_COLUMN_TYPES
ID_REGEXP =
/\A\d+\z/.freeze

Class Method Summary collapse

Class Method Details

.build_arel_or_query(filter_array) ⇒ Object


38
39
40
41
42
43
44
# File 'lib/motor/api_query/search.rb', line 38

def build_arel_or_query(filter_array)
  filter_array.reduce(nil) do |acc, filter|
    next acc = filter unless acc

    acc.or(filter)
  end
end

.call(rel, keyword) ⇒ Object


12
13
14
15
16
17
18
19
20
# File 'lib/motor/api_query/search.rb', line 12

def call(rel, keyword)
  return rel if keyword.blank?

  filters = fetch_filters(rel, keyword)

  arel_where = build_arel_or_query(filters)

  rel.where(arel_where)
end

.fetch_filters(rel, keyword) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/motor/api_query/search.rb', line 22

def fetch_filters(rel, keyword)
  arel_filters = []

  klass = rel.klass
  arel_table = klass.arel_table

  arel_filters << arel_table[klass.primary_key].eq(keyword) if keyword.match?(ID_REGEXP)

  string_column_names = find_searchable_columns(klass)
  selected_columns = select_columns(string_column_names)

  selected_columns.each { |name| arel_filters << arel_table[name].matches("%#{keyword}%") }

  arel_filters
end

.find_searchable_columns(model) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/motor/api_query/search.rb', line 55

def find_searchable_columns(model)
  model.columns.map do |column|
    next unless column.type.in?(COLUMN_TYPES)
    next if column.respond_to?(:array?) && column.array?
    next if model.validators_on(column.name).any?(ActiveModel::Validations::InclusionValidator)

    column.name
  end.compact
end

.select_columns(columns) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/motor/api_query/search.rb', line 46

def select_columns(columns)
  selected_columns =
    columns.grep(BuildSchema::FindDisplayColumn::DISPLAY_NAME_REGEXP).presence

  selected_columns ||= columns.first(SELECT_COLUMNS_AMOUNT)

  selected_columns
end