-
-
Save sashaegorov/2ad3f73cfb7810ab118436b78f126c0b to your computer and use it in GitHub Desktop.
Simple wrapper over arel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'arel' | |
# Ruby-like syntax in AR conditions using the underlying Arel layer (Rails >= 3.0). | |
# | |
# What you would usually write like this: | |
# | |
# User.where(["users.created_at > ? AND users.name LIKE ?", Date.yesterday, "Mary"]) | |
# | |
# can now be written like this (note those parentheses required by the operators precedences): | |
# | |
# User.where((User[:created_at] > Date.yesterday) & (User[:name] =~ "Mary")) | |
# | |
module ArelExtensions | |
module ActiveRecord | |
module ClassMethods | |
delegate :[], :to => :arel_table | |
end | |
end | |
module Nodes | |
def self.included(base) | |
base.class_eval do | |
alias_method :&, :and | |
alias_method :|, :or | |
end | |
end | |
end | |
module Predications | |
def self.included(base) | |
base.class_eval do | |
alias_method :<, :lt | |
alias_method :<=, :lteq | |
alias_method :==, :eq | |
alias_method :>=, :gteq | |
alias_method :>, :gt | |
alias_method :=~, :matches | |
alias_method :^, :not_eq | |
alias_method :>>, :in | |
alias_method :<<, :not_in | |
end | |
end | |
end | |
end | |
ActiveRecord::Base.extend(ArelExtensions::ActiveRecord::ClassMethods) | |
Arel::Nodes::Node.send(:include, ArelExtensions::Nodes) | |
Arel::Predications.send(:include, ArelExtensions::Predications) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment