Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'arel/nodes/binary'
module Arel
module Nodes
class ContainedInHStore < Arel::Nodes::Binary
def operator; :"<@" end
end
class ContainedInArray < Arel::Nodes::Binary
def operator; :"<@" end
end
end
end
require 'arel/visitors/postgresql'
module Arel
module Visitors
class PostgreSQL
private
def visit_Arel_Nodes_ContainedInHStore o, collector
infix_value o, collector, " <@ "
end
def visit_Arel_Nodes_ContainedInArray o, collector
infix_value o, collector, " <@ "
end
end
end
end
require 'arel/predications'
module Arel
module Predications
def contained_in_array(other)
Nodes::ContainedInArray.new self, Nodes.build_quoted(other, self)
end
end
end
module ActiveRecord
module QueryMethods
class WhereChain
def contained_in_array(opts, *rest)
build_where_chain(opts, rest) do |rel|
case rel
when Arel::Nodes::In, Arel::Nodes::Equality
column = left_column(rel) || column_from_association(rel)
equality_for_hstore(rel) if column.type == :hstore
if column.type == :hstore
Arel::Nodes::ContainedInHStore.new(rel.left, rel.right)
elsif column.respond_to?(:array) && column.array
Arel::Nodes::ContainedInArray.new(rel.left, rel.right)
else
Arel::Nodes::ContainsINet.new(rel.left, rel.right)
end
else
raise ArgumentError, "Invalid argument for .where.overlap(), got #{rel.class}"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.