diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-12-28 12:58:30 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-12-28 12:58:30 -0300 |
commit | bd00a461e042a77aef48822040897467c544cd1a (patch) | |
tree | 8272c2d78da319bde1ab6d736ce6f66b352a25e6 /lib/arel/algebra | |
parent | 4fe6bdf195336d54b082ca26b96c5294d8aae3d1 (diff) | |
download | rails-bd00a461e042a77aef48822040897467c544cd1a.tar.gz rails-bd00a461e042a77aef48822040897467c544cd1a.tar.bz2 rails-bd00a461e042a77aef48822040897467c544cd1a.zip |
Added having to use in combination with group to filter records.
Diffstat (limited to 'lib/arel/algebra')
-rw-r--r-- | lib/arel/algebra/relations.rb | 1 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/having.rb | 13 | ||||
-rw-r--r-- | lib/arel/algebra/relations/relation.rb | 3 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/compound.rb | 2 |
4 files changed, 17 insertions, 2 deletions
diff --git a/lib/arel/algebra/relations.rb b/lib/arel/algebra/relations.rb index 951b69d970..9481554667 100644 --- a/lib/arel/algebra/relations.rb +++ b/lib/arel/algebra/relations.rb @@ -7,6 +7,7 @@ require 'arel/algebra/relations/writes' require 'arel/algebra/relations/operations/alias' require 'arel/algebra/relations/operations/from' require 'arel/algebra/relations/operations/group' +require 'arel/algebra/relations/operations/having' require 'arel/algebra/relations/operations/join' require 'arel/algebra/relations/operations/order' require 'arel/algebra/relations/operations/project' diff --git a/lib/arel/algebra/relations/operations/having.rb b/lib/arel/algebra/relations/operations/having.rb new file mode 100644 index 0000000000..cd16535609 --- /dev/null +++ b/lib/arel/algebra/relations/operations/having.rb @@ -0,0 +1,13 @@ +module Arel + class Having < Compound + attributes :relation, :havings + deriving :== + + def initialize(relation, *havings, &block) + @relation = relation + @havings = (havings + arguments_from_block(relation, &block)) \ + .collect { |g| g.bind(relation) } + end + end +end + diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index b24e7c24d0..e848c8aa1c 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -43,7 +43,7 @@ module Arel join(other_relation, OuterJoin) end - [:where, :project, :order, :take, :skip, :group, :from].each do |operation_name| + [:where, :project, :order, :take, :skip, :group, :from, :having].each do |operation_name| class_eval <<-OPERATION, __FILE__, __LINE__ def #{operation_name}(*arguments, &block) arguments.all?(&:blank?) && !block_given?? self : #{operation_name.to_s.classify}.new(self, *arguments, &block) @@ -131,6 +131,7 @@ module Arel def orders; [] end def inserts; [] end def groupings; [] end + def havings; [] end def joins(formatter = nil); nil end # FIXME def taken; nil end def skipped; nil end diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb index 9967472d88..1acf92fef8 100644 --- a/lib/arel/algebra/relations/utilities/compound.rb +++ b/lib/arel/algebra/relations/utilities/compound.rb @@ -5,7 +5,7 @@ module Arel :column_for, :engine, :sources, :locked, :to => :relation - [:attributes, :wheres, :groupings, :orders].each do |operation_name| + [:attributes, :wheres, :groupings, :orders, :havings].each do |operation_name| class_eval <<-OPERATION, __FILE__, __LINE__ def #{operation_name} @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) } |