Changeset 9084
- Timestamp:
- 03/24/08 02:50:02 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/associations.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/association_collection.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/associations/association_proxy.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_many_association.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_many_through_association.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/named_scope.rb (added)
- trunk/activerecord/test/cases/associations/cascaded_eager_loading_test.rb (modified) (2 diffs)
- trunk/activerecord/test/cases/base_test.rb (modified) (8 diffs)
- trunk/activerecord/test/cases/finder_test.rb (modified) (3 diffs)
- trunk/activerecord/test/cases/fixtures_test.rb (modified) (1 diff)
- trunk/activerecord/test/cases/lifecycle_test.rb (modified) (1 diff)
- trunk/activerecord/test/cases/named_scope_test.rb (added)
- trunk/activerecord/test/cases/validations_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/topics.yml (modified) (2 diffs)
- trunk/activerecord/test/models/author.rb (modified) (2 diffs)
- trunk/activerecord/test/models/comment.rb (modified) (1 diff)
- trunk/activerecord/test/models/post.rb (modified) (1 diff)
- trunk/activerecord/test/models/topic.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r9067 r9084 1 1 *SVN* 2 3 * Merge the has_finder gem, renamed as 'named_scope'. #11404 [nkallen] 4 5 class Article < ActiveRecord::Base 6 has_finder :published, :conditions => {:published => true} 7 has_finder :popular, :conditions => ... 8 end 9 10 Article.published.paginate(:page => 1) 11 Article.published.popular.count 12 Article.popular.find(:first) 13 Article.popular.find(:all, :conditions => {...}) 14 15 See http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries 2 16 3 17 * Add has_one :through support. #4756 [thechrisoshow] trunk/activerecord/lib/active_record.rb
r8672 r9084 38 38 39 39 require 'active_record/base' 40 require 'active_record/named_scope' 40 41 require 'active_record/observer' 41 42 require 'active_record/query_cache' … … 65 66 include ActiveRecord::Timestamp 66 67 include ActiveRecord::Associations 68 include ActiveRecord::NamedScope 67 69 include ActiveRecord::AssociationPreload 68 70 include ActiveRecord::Aggregations trunk/activerecord/lib/active_record/associations.rb
r9075 r9084 1134 1134 end 1135 1135 end 1136 1136 1137 1137 def add_multiple_associated_save_callbacks(association_name) 1138 1138 method_name = "validate_associated_records_for_#{association_name}".to_sym trunk/activerecord/lib/active_record/associations/association_collection.rb
r9068 r9084 42 42 reset_target! 43 43 end 44 44 45 45 # Calculate sum using SQL, not Enumerable 46 46 def sum(*args) … … 169 169 super 170 170 end 171 else 172 @reflection.klass.send(:with_scope, construct_scope) do 171 elsif @reflection.klass.scopes.include?(method) 172 @reflection.klass.scopes[method].call(self, *args) 173 else 174 with_scope(construct_scope) do 173 175 if block_given? 174 176 @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } trunk/activerecord/lib/active_record/associations/association_proxy.rb
r8957 r9084 120 120 end 121 121 122 def with_scope(*args, &block) 123 @reflection.klass.send :with_scope, *args, &block 124 end 125 122 126 private 123 127 def method_missing(method, *args) trunk/activerecord/lib/active_record/associations/has_many_association.rb
r8571 r9084 168 168 create_scoping = {} 169 169 set_belongs_to_association_for(create_scoping) 170 { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, :create => create_scoping } 170 { 171 :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, 172 :create => create_scoping 173 } 171 174 end 172 175 end trunk/activerecord/lib/active_record/associations/has_many_through_association.rb
r8989 r9084 140 140 super 141 141 end 142 else 143 @reflection.klass.send(:with_scope, construct_scope) do 142 elsif @reflection.klass.scopes.include?(method) 143 @reflection.klass.scopes[method].call(self, *args) 144 else 145 with_scope construct_scope do 144 146 if block_given? 145 147 @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } trunk/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
r8681 r9084 62 62 def test_eager_association_loading_with_has_many_sti 63 63 topics = Topic.find(:all, :include => :replies, :order => 'topics.id') 64 assert_equal topics(:first, :second), topics64 first, second, = topics(:first).replies.size, topics(:second).replies.size 65 65 assert_no_queries do 66 assert_equal 1, topics[0].replies.size67 assert_equal 0, topics[1].replies.size66 assert_equal first, topics[0].replies.size 67 assert_equal second, topics[1].replies.size 68 68 end 69 69 end … … 71 71 def test_eager_association_loading_with_belongs_to_sti 72 72 replies = Reply.find(:all, :include => :topic, :order => 'topics.id') 73 assert_equal [topics(:second)], replies 73 assert replies.include?(topics(:second)) 74 assert !replies.include?(topics(:first)) 74 75 assert_equal topics(:first), assert_no_queries { replies.first.topic } 75 76 end trunk/activerecord/test/cases/base_test.rb
r9012 r9084 478 478 def test_load 479 479 topics = Topic.find(:all, :order => 'id') 480 assert_equal( 2, topics.size)480 assert_equal(4, topics.size) 481 481 assert_equal(topics(:first).title, topics.first.title) 482 482 end … … 550 550 551 551 def test_destroy_all 552 assert_equal 2, Topic.count 553 554 Topic.destroy_all "author_name = 'Mary'" 555 assert_equal 1, Topic.count 552 original_count = Topic.count 553 topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'") 554 555 Topic.destroy_all mary 556 assert_equal original_count - topics_by_mary, Topic.count 556 557 end 557 558 … … 563 564 564 565 def test_delete_many 565 Topic.delete([1, 2]) 566 assert_equal 0, Topic.count 566 original_count = Topic.count 567 Topic.delete(deleting = [1, 2]) 568 assert_equal original_count - deleting.size, Topic.count 567 569 end 568 570 … … 589 591 590 592 def test_update_all 591 assert_equal 2, Topic.update_all("content = 'bulk updated!'")593 assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'") 592 594 assert_equal "bulk updated!", Topic.find(1).content 593 595 assert_equal "bulk updated!", Topic.find(2).content 594 596 595 assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!'])597 assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!']) 596 598 assert_equal "bulk updated again!", Topic.find(1).content 597 599 assert_equal "bulk updated again!", Topic.find(2).content 598 600 599 assert_equal 2, Topic.update_all(['content = ?', nil])601 assert_equal Topic.count, Topic.update_all(['content = ?', nil]) 600 602 assert_nil Topic.find(1).content 601 603 end … … 603 605 def test_update_all_with_hash 604 606 assert_not_nil Topic.find(1).last_read 605 assert_equal 2, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)607 assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil) 606 608 assert_equal "bulk updated with hash!", Topic.find(1).content 607 609 assert_equal "bulk updated with hash!", Topic.find(2).content … … 638 640 639 641 def test_delete_all 640 assert_equal 2, Topic.delete_all 642 assert Topic.count > 0 643 644 assert_equal Topic.count, Topic.delete_all 641 645 end 642 646 … … 805 809 def test_update_attributes! 806 810 reply = Reply.find(2) 807 assert_equal "The Second Topic 'sof the day", reply.title811 assert_equal "The Second Topic of the day", reply.title 808 812 assert_equal "Have a nice day", reply.content 809 813 810 reply.update_attributes!("title" => "The Second Topic 'sof the day updated", "content" => "Have a nice evening")814 reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening") 811 815 reply.reload 812 assert_equal "The Second Topic 'sof the day updated", reply.title816 assert_equal "The Second Topic of the day updated", reply.title 813 817 assert_equal "Have a nice evening", reply.content 814 818 815 reply.update_attributes!(:title => "The Second Topic 'sof the day", :content => "Have a nice day")819 reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day") 816 820 reply.reload 817 assert_equal "The Second Topic 'sof the day", reply.title821 assert_equal "The Second Topic of the day", reply.title 818 822 assert_equal "Have a nice day", reply.content 819 823 … … 1771 1775 assert_equal "<topic>", xml.first(7) 1772 1776 assert xml.include?(%(<replies type="array"><reply>)) 1773 assert xml.include?(%(<title>The Second Topic 'sof the day</title>))1777 assert xml.include?(%(<title>The Second Topic of the day</title>)) 1774 1778 end 1775 1779 trunk/activerecord/test/cases/finder_test.rb
r8936 r9084 543 543 544 544 def test_find_all_by_array_attribute 545 assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic 'sof the day"]).size545 assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size 546 546 end 547 547 … … 552 552 553 553 topics = Topic.find_all_by_approved(true) 554 assert_equal 1, topics.size554 assert_equal 3, topics.size 555 555 assert topics.include?(topics(:second)) 556 556 end … … 564 564 def test_find_all_by_nil_attribute 565 565 topics = Topic.find_all_by_last_read nil 566 assert_equal 1, topics.size567 assert _nil topics[0].last_read566 assert_equal 3, topics.size 567 assert topics.collect(&:last_read).all?(&:nil?) 568 568 end 569 569 trunk/activerecord/test/cases/fixtures_test.rb
r9050 r9084 124 124 125 125 def test_complete_instantiation 126 assert_equal 2, @topics.size126 assert_equal 4, @topics.size 127 127 assert_equal "The First Topic", @first.title 128 128 end trunk/activerecord/test/cases/lifecycle_test.rb
r8889 r9084 69 69 70 70 def test_before_destroy 71 assert_equal 2,Topic.count72 Topic.find(1).destroy73 assert_equal 0, Topic.count71 original_count = Topic.count 72 (topic_to_be_destroyed = Topic.find(1)).destroy 73 assert_equal original_count - (1 + topic_to_be_destroyed.replies.size), Topic.count 74 74 end 75 75 trunk/activerecord/test/cases/validations_test.rb
r8941 r9084 429 429 assert t2.errors.on(:parent_id) 430 430 431 t2.parent_id = 3431 t2.parent_id = 4 432 432 assert t2.save, "Should now save t2 as unique" 433 433 trunk/activerecord/test/fixtures/topics.yml
r4579 r9084 13 13 second: 14 14 id: 2 15 title: The Second Topic 'sof the day15 title: The Second Topic of the day 16 16 author_name: Mary 17 written_on: 200 3-07-15t15:28:00.0099+01:0017 written_on: 2004-07-15t15:28:00.0099+01:00 18 18 content: Have a nice day 19 19 approved: true … … 21 21 parent_id: 1 22 22 type: Reply 23 24 third: 25 id: 3 26 title: The Third Topic of the day 27 author_name: Nick 28 written_on: 2005-07-15t15:28:00.0099+01:00 29 content: I'm a troll 30 approved: true 31 replies_count: 1 32 33 fourth: 34 id: 4 35 title: The Fourth Topic of the day 36 author_name: Carl 37 written_on: 2006-07-15t15:28:00.0099+01:00 38 content: Why not? 39 approved: true 40 type: Reply 41 parent_id: 3 42 trunk/activerecord/test/models/author.rb
r8989 r9084 4 4 has_many :posts_with_categories, :include => :categories, :class_name => "Post" 5 5 has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" 6 has_many :posts_containing_the_letter_a, :class_name => "Post" 6 7 has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension 7 8 def testing_proxy_owner … … 16 17 end 17 18 has_many :comments, :through => :posts 19 has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments 18 20 has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC' 19 21 has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1 trunk/activerecord/test/models/comment.rb
r8735 r9084 1 1 class Comment < ActiveRecord::Base 2 named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" 3 2 4 belongs_to :post, :counter_cache => true 3 5 trunk/activerecord/test/models/post.rb
r8890 r9084 1 1 class Post < ActiveRecord::Base 2 named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'" 3 2 4 belongs_to :author do 3 5 def greeting trunk/activerecord/test/models/topic.rb
r8657 r9084 1 1 class Topic < ActiveRecord::Base 2 named_scope :written_before, lambda { |time| 3 { :conditions => ['written_on < ?', time] } 4 } 5 named_scope :approved, :conditions => {:approved => true} 6 named_scope :replied, :conditions => ['replies_count > 0'] 7 named_scope :anonymous_extension do 8 def one 9 1 10 end 11 end 12 module NamedExtension 13 def two 14 2 15 end 16 end 17 module MultipleExtensionOne 18 def extension_one 19 1 20 end 21 end 22 module MultipleExtensionTwo 23 def extension_two 24 2 25 end 26 end 27 named_scope :named_extension, :extend => NamedExtension 28 named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] 29 2 30 has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" 3 31 serialize :content