Skip to content

Instantly share code, notes, and snippets.

@nathancolgate
Created June 2, 2016 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathancolgate/dc36ffdc6832836eaa7a52371c3868e8 to your computer and use it in GitHub Desktop.
Save nathancolgate/dc36ffdc6832836eaa7a52371c3868e8 to your computer and use it in GitHub Desktop.
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