diff options
Diffstat (limited to 'lib/arel')
-rw-r--r-- | lib/arel/relations/relation.rb | 29 | ||||
-rw-r--r-- | lib/arel/relations/utilities/compound.rb | 26 |
2 files changed, 16 insertions, 39 deletions
diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 6e0413232f..1e2ac9f2be 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -72,33 +72,18 @@ module Arel join(other_relation, "LEFT OUTER JOIN") end - def where(*predicates, &block) - predicates.all?(&:blank?) ? self : Where.new(self, *predicates) + [:where, :project, :order, :take, :skip, :group].each do |operation_name| + operation = <<-OPERATION + def #{operation_name}(*arguments, &block) + arguments.all?(&:blank?) && !block_given?? self : #{operation_name.to_s.classify}.new(self, *arguments, &block) + end + OPERATION + class_eval operation, __FILE__, __LINE__ end - def project(*attributes, &block) - attributes.all?(&:blank?) ? self : Project.new(self, *attributes) - end - def alias Alias.new(self) end - - def order(*attributes, &block) - attributes.all?(&:blank?) ? self : Order.new(self, *attributes) - end - - def take(taken = nil) - taken.blank?? self : Take.new(self, taken) - end - - def skip(skipped = nil) - skipped.blank?? self : Skip.new(self, skipped) - end - - def group(*groupings, &block) - groupings.all?(&:blank?) ? self : Group.new(self, *groupings) - end module Writable def insert(record) diff --git a/lib/arel/relations/utilities/compound.rb b/lib/arel/relations/utilities/compound.rb index a77099e0de..23a55d4b5b 100644 --- a/lib/arel/relations/utilities/compound.rb +++ b/lib/arel/relations/utilities/compound.rb @@ -2,25 +2,17 @@ module Arel class Compound < Relation attr_reader :relation hash_on :relation - delegate :joins, :wheres, :join?, :inserts, :taken, - :skipped, :name, :aggregation?, :column_for, - :engine, :table, :table_sql, + delegate :joins, :join?, :inserts, :taken, :skipped, :name, :aggregation?, + :column_for, :engine, :table, :table_sql, :to => :relation - def attributes - @attributes ||= relation.attributes.collect { |a| a.bind(self) } - end - - def wheres - @wheres ||= relation.wheres.collect { |w| w.bind(self) } - end - - def groupings - @groupings ||= relation.groupings.collect { |g| g.bind(self) } - end - - def orders - @orders ||= relation.orders.collect { |o| o.bind(self) } + [:attributes, :wheres, :groupings, :orders].each do |operation_name| + operation = <<-OPERATION + def #{operation_name} + @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) } + end + OPERATION + class_eval operation, __FILE__, __LINE__ end end end
\ No newline at end of file |