Sha256: cadc33a8bbff42acbd776d1b7f746d752cc05fd3d786f32ffd2179f5bcc890d4

Contents?: true

Size: 1.59 KB

Versions: 22

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Motor
  module ApiQuery
    module Filter
      LIKE_FILTER_VALUE_REGEXP = /\A%?(.*?)%?\z/.freeze

      module_function

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

        normalized_params = normalize_params(Array.wrap(params))

        rel.filter(normalized_params).distinct
      end

      def normalize_params(params)
        params.map do |item|
          next item if item.is_a?(String)
          next normalize_params(item) if item.is_a?(Array)

          item = item.to_unsafe_h if item.respond_to?(:to_unsafe_h)

          item.transform_values do |filter|
            if filter.is_a?(Hash)
              normalize_filter_hash(filter)
            else
              filter
            end
          end
        end.split('OR').product(['OR']).flatten(1)[0...-1]
      end

      def normalize_filter_hash(hash)
        hash.each_with_object({}) do |(action, value), acc|
          new_action, new_value =
            if value.is_a?(Hash)
              [action, normalize_filter_hash(value)]
            else
              normalize_action(action, value)
            end

          acc[new_action] = new_value

          acc
        end
      end

      def normalize_action(action, value)
        case action
        when 'contains'
          ['ilike', value.sub(LIKE_FILTER_VALUE_REGEXP, '%\1%')]
        when 'starts_with'
          ['ilike', value.sub(LIKE_FILTER_VALUE_REGEXP, '\1%')]
        when 'ends_with'
          ['ilike', value.sub(LIKE_FILTER_VALUE_REGEXP, '%\1')]
        else
          [action, value]
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
motor-admin-0.1.55 lib/motor/api_query/filter.rb
motor-admin-0.1.54 lib/motor/api_query/filter.rb
motor-admin-0.1.53 lib/motor/api_query/filter.rb
motor-admin-0.1.52 lib/motor/api_query/filter.rb
motor-admin-0.1.51 lib/motor/api_query/filter.rb
motor-admin-0.1.50 lib/motor/api_query/filter.rb
motor-admin-0.1.49 lib/motor/api_query/filter.rb
motor-admin-0.1.48 lib/motor/api_query/filter.rb
motor-admin-0.1.47 lib/motor/api_query/filter.rb
motor-admin-0.1.46 lib/motor/api_query/filter.rb
motor-admin-0.1.44 lib/motor/api_query/filter.rb
motor-admin-0.1.43 lib/motor/api_query/filter.rb
motor-admin-0.1.42 lib/motor/api_query/filter.rb
motor-admin-0.1.41 lib/motor/api_query/filter.rb
motor-admin-0.1.40 lib/motor/api_query/filter.rb
motor-admin-0.1.39 lib/motor/api_query/filter.rb
motor-admin-0.1.37 lib/motor/api_query/filter.rb
motor-admin-0.1.36 lib/motor/api_query/filter.rb
motor-admin-0.1.35 lib/motor/api_query/filter.rb
motor-admin-0.1.34 lib/motor/api_query/filter.rb