Class: ActiveRecord::Refined::AST::JsonHasKey

Inherits:
Predicate show all
Defined in:
lib/active_record/refined/ast.rb

Overview

Whether a key is in the document. PostgreSQL's spelling is the ? operator rather than jsonb_exists, the function it is shorthand for, because a GIN index matches the operator and never the function. A ? is a bind placeholder only to sanitize_sql, which none of the SQL written here passes through.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #collate, #desc

Constructor Details

#initialize(operand, key) ⇒ JsonHasKey

Returns a new instance of JsonHasKey.



1138
1139
1140
1141
# File 'lib/active_record/refined/ast.rb', line 1138

def initialize(operand, key)
  @operand = operand
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



1136
1137
1138
# File 'lib/active_record/refined/ast.rb', line 1136

def key
  @key
end

#operandObject (readonly)

Returns the value of attribute operand.



1136
1137
1138
# File 'lib/active_record/refined/ast.rb', line 1136

def operand
  @operand
end

Instance Method Details

#to_arel(table, model) ⇒ Object



1143
1144
1145
1146
1147
1148
# File 'lib/active_record/refined/ast.rb', line 1143

def to_arel(table, model)
  Dialect.for(model).json_has_key(
    to_arel_operand(operand, table, model),
    Arel::Nodes.build_quoted(key.to_s),
    Arel::Nodes.build_quoted("$.#{key}"), model)
end