Permalink
Browse files

Add new finder methods to association collection.

  • Loading branch information...
1 parent f374150 commit 83f24afd44ecd6fb6e38d5e8a6830dfca4bf5ffe @lifo lifo committed Dec 26, 2009
@@ -1,5 +1,15 @@
*Edge*
+* Add new finder methods to association collection. [Pratik Naik]
+
+ class User < ActiveRecord::Base
+ has_many :items
+ end
+
+ user = User.first
+ user.items.where(:items => {:colour => 'red'})
+ user.items.select('items.id')
+
* Add relation.reload to force reloading the records. [Pratik Naik]
topics = Topic.scoped
@@ -1382,9 +1382,9 @@ def collection_reader_method(reflection, association_proxy_class)
if reflection.through_reflection && reflection.source_reflection.belongs_to?
through = reflection.through_reflection
primary_key = reflection.source_reflection.primary_key_name
- send(through.name).all(:select => "DISTINCT #{through.quoted_table_name}.#{primary_key}").map!(&:"#{primary_key}")
+ send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map!(&:"#{primary_key}")
else
- send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map!(&:id)
+ send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map!(&:id)
end
end
end
@@ -20,7 +20,22 @@ def initialize(owner, reflection)
super
construct_sql
end
-
+
+ delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :to => :scoped
+
+ def select(select = nil, &block)
+ if block_given?
+ load_target
+ @target.select(&block)
+ else
+ scoped.select(select)
+ end
+ end
+
+ def scoped
+ with_scope(construct_scope) { @reflection.klass.scoped }
+ end
+
def find(*args)
options = args.extract_options!
@@ -383,7 +398,7 @@ def load_target
loaded if target
target
end
-
+
def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
if block_given?
@@ -221,9 +221,9 @@ def associated_records_to_validate_or_save(association, new_record, autosave)
if new_record
association
elsif association.loaded?
- autosave ? association : association.select { |record| record.new_record? }
+ autosave ? association : association.find_all { |record| record.new_record? }
else
- autosave ? association.target : association.target.select { |record| record.new_record? }
+ autosave ? association.target : association.target.find_all { |record| record.new_record? }
end
end
@@ -140,23 +140,23 @@ def test_replace_association
def test_replace_order_is_preserved
posts(:welcome).people.clear
posts(:welcome).people = [people(:david), people(:michael)]
- assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
+ assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.order('id').map(&:person_id)
# Test the inverse order in case the first success was a coincidence
posts(:welcome).people.clear
posts(:welcome).people = [people(:michael), people(:david)]
- assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
+ assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.order('id').map(&:person_id)
end
def test_replace_by_id_order_is_preserved
posts(:welcome).people.clear
posts(:welcome).person_ids = [people(:david).id, people(:michael).id]
- assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
+ assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.order('id').map(&:person_id)
# Test the inverse order in case the first success was a coincidence
posts(:welcome).people.clear
posts(:welcome).person_ids = [people(:michael).id, people(:david).id]
- assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
+ assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.order('id').map(&:person_id)
end
def test_associate_with_create
@@ -5,7 +5,7 @@ class EachTest < ActiveRecord::TestCase
fixtures :posts
def setup
- @posts = Post.all(:order => "id asc")
+ @posts = Post.order("id asc")
@total = Post.count
end
@@ -345,8 +345,8 @@ def test_chaining_should_use_latest_conditions_when_creating
def test_chaining_should_use_latest_conditions_when_searching
# Normal hash conditions
- assert_equal Topic.all(:conditions => {:approved => true}).to_a, Topic.rejected.approved.all.to_a
- assert_equal Topic.all(:conditions => {:approved => false}).to_a, Topic.approved.rejected.all.to_a
+ assert_equal Topic.where(:approved => true).to_a, Topic.rejected.approved.all.to_a
+ assert_equal Topic.where(:approved => false).to_a, Topic.approved.rejected.all.to_a
# Nested hash conditions with same keys
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.all.to_a

0 comments on commit 83f24af

Please sign in to comment.