aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-03 19:42:29 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-03 19:43:29 +0530
commitaf5e1b4cc6fd3ae5f5e175751373a5e60934385b (patch)
tree3c044df83a27526741ba33084c991ad013f37eb0 /activerecord
parent22bfd8b09804db28e05e598e062d58fd2ab36485 (diff)
downloadrails-af5e1b4cc6fd3ae5f5e175751373a5e60934385b.tar.gz
rails-af5e1b4cc6fd3ae5f5e175751373a5e60934385b.tar.bz2
rails-af5e1b4cc6fd3ae5f5e175751373a5e60934385b.zip
Add Relation#except
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG5
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb22
-rw-r--r--activerecord/test/cases/relations_test.rb12
3 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 4846774deb..0cfd8cdc87 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*Edge*
+* Add Relation#except. [Pratik Naik]
+
+ one_red_item = Item.where(:colour => 'red').limit(1)
+ all_items = one_red_item.except(:where, :limit)
+
* Add Relation#delete_all. [Pratik Naik]
Item.where(:colour => 'red').delete_all
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 7b15279253..b9e3457aac 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -45,5 +45,27 @@ module ActiveRecord
alias :& :merge
+ def except(*skips)
+ result = Relation.new(@klass, table)
+ result.table = table
+
+ [:eager_load, :preload, :includes].each do |load_method|
+ result = result.send(load_method, send(:"#{load_method}_associations"))
+ end
+
+ result.readonly = self.readonly unless skips.include?(:readonly)
+
+ result = result.joins(@relation.joins(@relation)) unless skips.include?(:joins)
+ result = result.group(@relation.groupings) unless skips.include?(:group)
+ result = result.limit(@relation.taken) unless skips.include?(:limit)
+ result = result.offset(@relation.skipped) unless skips.include?(:offset)
+ result = result.select(@relation.send(:select_clauses)) unless skips.include?(:select)
+ result = result.from(@relation.sources) unless skips.include?(:from)
+ result = result.order(order_clause) unless skips.include?(:order)
+ result = result.where(*@relation.wheres) unless skips.include?(:where)
+
+ result
+ end
+
end
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 7046420ebd..bcecf74737 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -539,4 +539,16 @@ class RelationTest < ActiveRecord::TestCase
assert ! hen.new_record?
assert_equal 'hen', hen.name
end
+
+ def test_except
+ relation = Post.where(:author_id => 1).order('id ASC').limit(1)
+ assert_equal [posts(:welcome)], relation.all
+
+ author_posts = relation.except(:order, :limit)
+ assert_equal Post.where(:author_id => 1).all, author_posts.all
+
+ all_posts = relation.except(:where, :order, :limit)
+ assert_equal Post.all, all_posts.all
+ end
+
end