Class: ActiveRecord::Refined::AST::In

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

Overview

IN for a list of values, BETWEEN for a range, IN (SELECT ...) for a relation.

Defined Under Namespace

Classes: QuotedRange

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #collate, #desc

Constructor Details

#initialize(operand, values, negated: false) ⇒ In

Returns a new instance of In.



2104
2105
2106
2107
2108
# File 'lib/active_record/refined/ast.rb', line 2104

def initialize(operand, values, negated: false)
  @operand = operand
  @values = values
  @negated = negated
end

Instance Attribute Details

#negatedObject (readonly)

Returns the value of attribute negated.



2102
2103
2104
# File 'lib/active_record/refined/ast.rb', line 2102

def negated
  @negated
end

#operandObject (readonly)

Returns the value of attribute operand.



2102
2103
2104
# File 'lib/active_record/refined/ast.rb', line 2102

def operand
  @operand
end

#valuesObject (readonly)

Returns the value of attribute values.



2102
2103
2104
# File 'lib/active_record/refined/ast.rb', line 2102

def values
  @values
end

Instance Method Details

#to_arel(table, model) ⇒ Object



2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
# File 'lib/active_record/refined/ast.rb', line 2110

def to_arel(table, model)
  arel_operand = to_arel_operand(operand, table, model)
  case values
  when Range, QuotedRange
    lower = quote_value(values.begin, table, model)
    upper = quote_value(values.end, table, model)
    if json_between?(model) && lower && upper && !negated
      return arel_operand.gteq(lower).and(
        values.exclude_end? ? arel_operand.lt(upper) : arel_operand.lteq(upper))
    end
    range = QuotedRange.new(lower, upper, values.exclude_end?)
    arel_operand.public_send(negated ? :not_between : :between, range)
  when ActiveRecord::Relation
    arel_operand.public_send(negated ? :not_in : :in, set_subquery(values, "IN"))
  else
    arg = values
    if arg.is_a?(::Array)
      arg = arg.map { |value| quote_value(value, table, model) }
      return json_list(arel_operand, arg) if json_list?(model)
    end
    arel_operand.public_send(negated ? :not_in : :in, arg)
  end
end