Module: ApiKit::Filtering

Defined in:
lib/api_kit/filtering.rb

Class Method Summary collapse

Class Method Details

.extract_attributes_and_predicates(requested_field) ⇒ Array

Parses and returns the attribute and the predicate of a ransack field

Parameters:

  • requested_field (String)

    the field to parse

Returns:

  • (Array)

    with the fields and the predicate



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/api_kit/filtering.rb', line 11

def self.extract_attributes_and_predicates(requested_field)
  predicates = []
  field_name = requested_field.to_s.dup

  while Ransack::Predicate.detect_from_string(field_name).present? do
    predicate = Ransack::Predicate
      .detect_and_strip_from_string!(field_name)
    predicates << Ransack::Predicate.named(predicate)
  end

  [ field_name.split(/_and_|_or_/), predicates.reverse ]
end