aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-05-19 18:13:23 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-05-19 18:13:23 -0700
commit5a5501cde76bbba69bcea27d3d0efeaffa3e3bf5 (patch)
treeba04e17c1d2797fdd2f293a420e40f54271f83a3 /lib
parent78b5b5783776f57a47e480db4bd83b15db988909 (diff)
downloadrails-5a5501cde76bbba69bcea27d3d0efeaffa3e3bf5.tar.gz
rails-5a5501cde76bbba69bcea27d3d0efeaffa3e3bf5.tar.bz2
rails-5a5501cde76bbba69bcea27d3d0efeaffa3e3bf5.zip
drying up some of the code
Diffstat (limited to 'lib')
-rw-r--r--lib/arel/relations/relation.rb29
-rw-r--r--lib/arel/relations/utilities/compound.rb26
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