Permalink
Browse files

Added inequality DSL for AREL

  • Loading branch information...
1 parent 6a8acd9 commit 6087e8d58b4ddf4500eba267be9ea145d5249c90 @mavenik committed Apr 14, 2012
@@ -0,0 +1,50 @@
+require 'arel/nodes/inequality'
+
+module ActiveRecord
+ class InequalityBuilder
+
+ def initialize(engine)
+ @engine = engine
+ end
+
+ def build_from_hash(attributes, operand, default_table)
+ inequalities = attributes.map do |column, value|
+ table = default_table
+
+ if value.is_a?(Hash)
+ table = Arel::Table.new(column, :engine => @engine)
+ build_from_hash(value, table)
+ else
+ column = column.to_s
+
+ if column.include?('.')
+ table_name, column = column.split('.', 2)
+ table = Arel::Table.new(table_name, :engine => @engine)
+ end
+
+ attribute = table[column] || Arel::Attribute.new(table, column)
+
+# case value
+# when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation
+# values = value.to_a.map { |x|
+# x.is_a?(ActiveRecord::Base) ? x.id : x
+# }
+# attribute.in(values)
+# when Range, Arel::Relation
+# attribute.in(value)
+# when ActiveRecord::Base
+# attribute.eq(value.id)
+# when Class
+# # FIXME: I think we need to deprecate this behavior
+# attribute.eq(value.name)
+# else
+ Arel::Nodes::Inequality.new attribute, value, operand
+# end
+ end
+ end
+
+ inequalities.flatten
+ end
+
+ end
+end
@@ -0,0 +1,36 @@
+require 'active_record/relation/inequality_builder'
+
+module ActiveRecord
+ module InequalityMethods
+ extend ActiveSupport::Concern
+
+ def lt(options)
+ build_relation options, "<"
+ end #lt
+
+ def gt(options)
+ build_relation options, ">"
+ end #gt
+
+ def leq(options)
+ build_relation options, "<="
+ end #leq
+
+ def geq(options)
+ build_relation options, ">="
+ end #geq
+
+ private
+ def build_relation(inequalities, operand)
+ relation = clone
+ relation.where_values += build_inequality(inequalities, operand) if inequalities
+ relation
+ end
+
+ def build_inequality(inequalities, operand)
+ ActiveRecord::InequalityBuilder.new(table).build_from_hash inequalities, operand, table
+ end
+ end #InequalityMethods
+end #ActiveRecord
+
+ActiveRecord::Relation.send :include, ActiveRecord::InequalityMethods
@@ -3,6 +3,7 @@
require 'active_record/hybrid_database_connector'
require 'active_record/connection_adapters/neo4j/graph_methods'
require 'active_record/relation/graph_methods'
+require 'active_record/relation/inequality_methods'
require 'build_tasks'
module Activerecord
@@ -0,0 +1,9 @@
+class Arel::Nodes::Inequality
+ attr_accessor :left, :right, :operand
+
+ def initialize attribute, value, op
+ @left = attribute
+ @right = value
+ @operand = op
+ end
+end
@@ -370,6 +370,10 @@ def visit_Arel_Nodes_Equality o
end
end
+ def visit_Arel_Nodes_Inequality o
+ "it.#{visit(o.left).to_s} #{o.operand} #{visit o.right}"
+ end
+
def visit_Arel_Nodes_NotEqual o
right = o.right
@@ -464,4 +468,4 @@ def quote_column_name name
end # Sql
end # Neo4j
end # Visitors
-end # Arel
+end # Arel

0 comments on commit 6087e8d

Please sign in to comment.