Class: IndexQueryBuilder::QueryDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/index_query_builder/query_definition.rb

Overview

Provides a DSL to build a query definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryDefinition

Returns a new instance of QueryDefinition.



7
8
9
10
# File 'lib/index_query_builder/query_definition.rb', line 7

def initialize
  @arel_filters = {}
  @arel_ordering = []
end

Instance Attribute Details

#arel_filtersObject (readonly)

Returns the value of attribute arel_filters.



5
6
7
# File 'lib/index_query_builder/query_definition.rb', line 5

def arel_filters
  @arel_filters
end

#arel_orderingObject (readonly)

Returns the value of attribute arel_ordering.



5
6
7
# File 'lib/index_query_builder/query_definition.rb', line 5

def arel_ordering
  @arel_ordering
end

Instance Method Details

#filter_field(field_name, predicates = {equal_to: field_name}) ⇒ Object

Specifies how to filter a field

Operators

Operators will apply where clauses to query only if the filter_name is present in filters hash.

:equal_to

Applies ‘field_name = filter_value’

:contains

Applies substring (ILIKE ‘%filter_value%’)

:greater_than_or_equal_to

Applies ‘field_name >= filter_value’

:less_than

Applies ‘field_name < filter_value’

:present_if

Applies

  • ‘field_name IS NOT NULL’ if filter_value is truthy

  • ‘field_name IS NULL’ if filter_value is falsey

Examples

query.filter_field :received
query.filter_field :reference, contains: :reference
query.filter_field [:vendor, :name], equal_to: :vendor_name
query.filter_field [:receive_order_items, :sku, :code], equal_to: :sku_code
query.filter_field :expected_delivery_at, greater_than_or_equal_to: :from_expected_delivery_at, less_than: :to_expected_delivery_at
query.filter_field :expected_delivery_at, less_than_or_equal_to: :to_expected_delivery_at
query.filter_field :outbound_trailer_id, present_if: :has_trailer

Parameters:

  • field_name (Symbol | [Symbol])

    database field name (if one element) or table names to join plus database field name (if an array of more than 1 element)

  • predicates (Hash) (defaults to: {equal_to: field_name})

    of the form operator => filter_name. filter_name should match the key in filters hash that is passed to IndexQueryBuilder.query methods



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/index_query_builder/query_definition.rb', line 46

def filter_field(field_name, predicates={equal_to: field_name})
  predicates.each do |operator, filter|
    @arel_filters[filter] = []

    if operator == :contains
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        arel.where("#{table_name}.#{field} ILIKE ?", "%#{value}%")
      end
    elsif operator == :equal_to
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        arel.where(table_name => {field => value})
      end
    elsif operator == :greater_than_or_equal_to
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        arel.where("#{table_name}.#{field} >= ?", value)
      end
    elsif operator == :less_than
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        arel.where("#{table_name}.#{field} < ?", value)
      end
    elsif operator == :less_than_or_equal_to
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        arel.where("#{table_name}.#{field} <= ?", value)
      end
    elsif operator == :present_if
      @arel_filters[filter] << ->(arel, value) do
        table_name, field = apply_joins(arel, field_name, filter)
        if value
          arel.where("#{table_name}.#{field} IS NOT NULL")
        else
          arel.where("#{table_name}.#{field} IS NULL")
        end
      end
    else
      raise UnknownOperator.new("Unknown operator #{operator}.")
    end
  end
end

#filter_namesObject



103
104
105
# File 'lib/index_query_builder/query_definition.rb', line 103

def filter_names
  arel_filters.keys
end

#order_by(*args) ⇒ Object

Specifies how to order the result. Uses same syntax as Arel#order (guides.rubyonrails.org/active_record_querying.html#ordering)

Examples

query.order_by "expected_delivery_at DESC, receive_orders.id DESC"



97
98
99
100
101
# File 'lib/index_query_builder/query_definition.rb', line 97

def order_by(*args)
  @arel_ordering << ->(arel) do
    arel.order(*args)
  end
end