Class: HQ::GraphQL::FilterOperations::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/graphql/filter_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, arel:, check_for_null: false, sanitize: nil) ⇒ Operation

Returns a new instance of Operation.



11
12
13
14
15
16
# File 'lib/hq/graphql/filter_operations.rb', line 11

def initialize(name:, arel:, check_for_null: false, sanitize: nil)
  @name           = name
  @arel           = arel
  @check_for_null = check_for_null
  @sanitize       = sanitize
end

Instance Attribute Details

#arelObject (readonly)

Returns the value of attribute arel.



9
10
11
# File 'lib/hq/graphql/filter_operations.rb', line 9

def arel
  @arel
end

#check_for_nullObject (readonly)

Returns the value of attribute check_for_null.



9
10
11
# File 'lib/hq/graphql/filter_operations.rb', line 9

def check_for_null
  @check_for_null
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/hq/graphql/filter_operations.rb', line 9

def name
  @name
end

#sanitizeObject (readonly)

Returns the value of attribute sanitize.



9
10
11
# File 'lib/hq/graphql/filter_operations.rb', line 9

def sanitize
  @sanitize
end

Instance Method Details

#sanitize_value(value) ⇒ Object



18
19
20
# File 'lib/hq/graphql/filter_operations.rb', line 18

def sanitize_value(value)
  sanitize ? sanitize.call(value) : value
end

#to_arel(table:, column_name:, value:) ⇒ Object



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

def to_arel(table:, column_name:, value:)
  sanitized_value = sanitize_value(value)

  if arel.is_a?(Proc)
    return arel.call(table: table, column_name: column_name, value: sanitized_value)
  end

  expression = table[column_name].send(arel, sanitized_value)

  if check_for_null
    expression = expression.or(table[column_name].eq(nil))
  end

  expression
end