Skip to content

Instantly share code, notes, and snippets.

@codesnik
Created September 3, 2015 07:20
Show Gist options
  • Save codesnik/2ebba1940c05b08b17f9 to your computer and use it in GitHub Desktop.
Save codesnik/2ebba1940c05b08b17f9 to your computer and use it in GitHub Desktop.
using ActiveRecord::PredicateBuilder.register_handler
module AROpts
module Helpers
def GT(val); AROpts::GT.new(val); end
def LT(val); AROpts::LT.new(val); end
def GTE(val); AROpts::GTE.new(val); end
def LTE(val); AROpts::LTE.new(val); end
end
GenericOp = Struct.new(:expression)
class GT < GenericOp; end
class LT < GenericOp; end
class GTE < GenericOp; end
class LTE < GenericOp; end
def self.register!
ActiveRecord::PredicateBuilder.register_handler(GT, proc { |column, op| Arel::Nodes::GreaterThan.new(column, op.expression) })
ActiveRecord::PredicateBuilder.register_handler(LT, proc { |column, op| Arel::Nodes::LessThan.new(column, op.expression) })
ActiveRecord::PredicateBuilder.register_handler(GTE, proc { |column, op| Arel::Nodes::GreaterThanOrEqual.new(column, op.expression) })
ActiveRecord::PredicateBuilder.register_handler(LTE, proc { |column, op| Arel::Nodes::LessThanOrEqual.new(column, op.expression) })
end
end
AROpts.register!
include AROpts::Helpers
Payment.where(total_cents: LTE(1000)).first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment