Class: ActiveRecord::Refined::AST::JsonContains

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

Overview

JSON containment: whether the document holds what is given.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(operand, value) ⇒ JsonContains

Returns a new instance of JsonContains.



602
603
604
605
# File 'lib/active_record/refined/ast.rb', line 602

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

Instance Attribute Details

#operandObject (readonly)

Returns the value of attribute operand.



600
601
602
# File 'lib/active_record/refined/ast.rb', line 600

def operand
  @operand
end

#valueObject (readonly)

Returns the value of attribute value.



600
601
602
# File 'lib/active_record/refined/ast.rb', line 600

def value
  @value
end

Instance Method Details

#to_arel(table, model) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/active_record/refined/ast.rb', line 607

def to_arel(table, model)
  document = to_arel_operand(operand, table, model)
  json = Arel::Nodes.build_quoted(JSON.generate(value))
  case AST.adapter_family(model)
  when :postgresql then Arel::Nodes::Contains.new(document, json)
  when :mysql
    Arel::Nodes::NamedFunction.new('JSON_CONTAINS', [document, json])
  else
    # Later than the others, since the adapter is only known here.
    raise NotImplementedError,
      "contains? has no equivalent on #{model.connection_db_config.adapter}"
  end
end