class ActiveRecord::Relation

def build_with(arel) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
def build_with(arel) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
  return if with_values.empty?
  with_statements = with_values.map do |with_value|
    case with_value
    when String then Arel::Nodes::SqlLiteral.new(with_value)
    when Arel::Nodes::As then with_value
    when Hash then build_with_value_from_hash(with_value)
    when Array then build_with_value_from_array(with_value)
    else
      raise ArgumentError, "Unsupported argument type: #{with_value} #{with_value.class}"
    end
  end
  recursive_with? ? arel.with(:recursive, with_statements) : arel.with(with_statements)
end