From 081ddb6f24d70794ee9e071d3ed5302f52b26c4d Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Mon, 24 Mar 2008 02:50:02 +0000 Subject: Merge the has_finder gem, renamed as 'named_scope'. Closes #11404 [nkallen] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9084 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 14 +++ activerecord/lib/active_record.rb | 2 + activerecord/lib/active_record/associations.rb | 2 +- .../associations/association_collection.rb | 8 +- .../associations/association_proxy.rb | 4 + .../associations/has_many_association.rb | 5 +- .../associations/has_many_through_association.rb | 4 +- activerecord/lib/active_record/named_scope.rb | 128 +++++++++++++++++++++ .../associations/cascaded_eager_loading_test.rb | 9 +- activerecord/test/cases/base_test.rb | 40 ++++--- activerecord/test/cases/finder_test.rb | 8 +- activerecord/test/cases/fixtures_test.rb | 2 +- activerecord/test/cases/lifecycle_test.rb | 6 +- activerecord/test/cases/named_scope_test.rb | 116 +++++++++++++++++++ activerecord/test/cases/validations_test.rb | 2 +- activerecord/test/fixtures/topics.yml | 24 +++- activerecord/test/models/author.rb | 2 + activerecord/test/models/comment.rb | 2 + activerecord/test/models/post.rb | 2 + activerecord/test/models/topic.rb | 28 +++++ 20 files changed, 369 insertions(+), 39 deletions(-) create mode 100644 activerecord/lib/active_record/named_scope.rb create mode 100644 activerecord/test/cases/named_scope_test.rb diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index b85cccc78e..96075d0d33 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,19 @@ *SVN* +* Merge the has_finder gem, renamed as 'named_scope'. #11404 [nkallen] + + class Article < ActiveRecord::Base + has_finder :published, :conditions => {:published => true} + has_finder :popular, :conditions => ... + end + + Article.published.paginate(:page => 1) + Article.published.popular.count + Article.popular.find(:first) + Article.popular.find(:all, :conditions => {...}) + + See http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries + * Add has_one :through support. #4756 [thechrisoshow] * Migrations: create_table supports primary_key_prefix_type. #10314 [student, thechrisoshow] diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 4d85c24584..cedcd8b0a8 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -37,6 +37,7 @@ unless defined? ActiveSupport end require 'active_record/base' +require 'active_record/named_scope' require 'active_record/observer' require 'active_record/query_cache' require 'active_record/validations' @@ -64,6 +65,7 @@ ActiveRecord::Base.class_eval do include ActiveRecord::Observing include ActiveRecord::Timestamp include ActiveRecord::Associations + include ActiveRecord::NamedScope include ActiveRecord::AssociationPreload include ActiveRecord::Aggregations include ActiveRecord::Transactions diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 068f2f9de2..e420a8ec57 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1133,7 +1133,7 @@ module ActiveRecord end end end - + def add_multiple_associated_save_callbacks(association_name) method_name = "validate_associated_records_for_#{association_name}".to_sym ivar = "@#{association_name}" diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index beebd72bba..d8b988d889 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -41,7 +41,7 @@ module ActiveRecord delete(@target) reset_target! end - + # Calculate sum using SQL, not Enumerable def sum(*args) if block_given? @@ -168,8 +168,10 @@ module ActiveRecord else super end - else - @reflection.klass.send(:with_scope, construct_scope) do + elsif @reflection.klass.scopes.include?(method) + @reflection.klass.scopes[method].call(self, *args) + else + with_scope(construct_scope) do if block_given? @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } else diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index adfc4610d5..421ddc15ee 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -119,6 +119,10 @@ module ActiveRecord ) end + def with_scope(*args, &block) + @reflection.klass.send :with_scope, *args, &block + end + private def method_missing(method, *args) if load_target diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 054a8991da..eedffa052b 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -167,7 +167,10 @@ module ActiveRecord def construct_scope create_scoping = {} set_belongs_to_association_for(create_scoping) - { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, :create => create_scoping } + { + :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, + :create => create_scoping + } end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 8c10ac641b..700a4bf89c 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -139,8 +139,10 @@ module ActiveRecord else super end + elsif @reflection.klass.scopes.include?(method) + @reflection.klass.scopes[method].call(self, *args) else - @reflection.klass.send(:with_scope, construct_scope) do + with_scope construct_scope do if block_given? @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } else diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb new file mode 100644 index 0000000000..120ec88f95 --- /dev/null +++ b/activerecord/lib/active_record/named_scope.rb @@ -0,0 +1,128 @@ +module ActiveRecord + module NamedScope + # All subclasses of ActiveRecord::Base have two named_scopes: + # * all, which is similar to a find(:all) query, and + # * scoped, which allows for the creation of anonymous scopes, on the fly: + # + # Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions) + # + # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing + # intermediate values (scopes) around as first-class objects is convenient. + def self.included(base) + base.class_eval do + extend ClassMethods + named_scope :all + named_scope :scoped, lambda { |scope| scope } + end + end + + module ClassMethods + def scopes + read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {}) + end + + # Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query, + # such as :conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions. + # + # class Shirt < ActiveRecord::Base + # named_scope :red, :conditions => {:color => 'red'} + # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true] + # end + # + # The above calls to named_scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, + # in effect, represents the query Shirt.find(:all, :conditions => {:color => 'red'}). + # + # Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object + # constructed by a has_many declaration. For instance, you can invoke Shirt.red.find(:first), Shirt.red.count, + # Shirt.red.find(:all, :conditions => {:size => 'small'}). Also, just + # as with the association objects, name scopes acts like an Array, implementing Enumerable; Shirt.red.each(&block), + # Shirt.red.first, and Shirt.red.inject(memo, &block) all behave as if Shirt.red really were an Array. + # + # These named scopes are composable. For instance, Shirt.red.dry_clean_only will produce all shirts that are both red and dry clean only. + # Nested finds and calculations also work with these compositions: Shirt.red.dry_clean_only.count returns the number of garments + # for which these criteria obtain. Similarly with Shirt.red.dry_clean_only.average(:thread_count). + # + # All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to + # has_many associations. If, + # + # class Person < ActiveRecord::Base + # has_many :shirts + # end + # + # then elton.shirts.red.dry_clean_only will return all of Elton's red, dry clean + # only shirts. + # + # Named scopes can also be procedural. + # + # class Shirt < ActiveRecord::Base + # named_scope :colored, lambda { |color| + # { :conditions => { :color => color } } + # } + # end + # + # In this example, Shirt.colored('puce') finds all puce shirts. + # + # Named scopes can also have extensions, just as with has_many declarations: + # + # class Shirt < ActiveRecord::Base + # named_scope :red, :conditions => {:color => 'red'} do + # def dom_id + # 'red_shirts' + # end + # end + # end + # + def named_scope(name, options = {}, &block) + scopes[name] = lambda do |parent_scope, *args| + Scope.new(parent_scope, case options + when Hash + options + when Proc + options.call(*args) + end, &block) + end + (class << self; self end).instance_eval do + define_method name do |*args| + scopes[name].call(self, *args) + end + end + end + end + + class Scope + attr_reader :proxy_scope, :proxy_options + [].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ } + delegate :scopes, :with_scope, :to => :proxy_scope + + def initialize(proxy_scope, options, &block) + [options[:extend]].flatten.each { |extension| extend extension } if options[:extend] + extend Module.new(&block) if block_given? + @proxy_scope, @proxy_options = proxy_scope, options.except(:extend) + end + + def reload + load_found; self + end + + protected + def proxy_found + @found || load_found + end + + private + def method_missing(method, *args, &block) + if scopes.include?(method) + scopes[method].call(self, *args) + else + with_scope :find => proxy_options do + proxy_scope.send(method, *args, &block) + end + end + end + + def load_found + @found = find(:all) + end + end + end +end \ No newline at end of file diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 6349a3a8b2..3631be76a0 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -61,16 +61,17 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase def test_eager_association_loading_with_has_many_sti topics = Topic.find(:all, :include => :replies, :order => 'topics.id') - assert_equal topics(:first, :second), topics + first, second, = topics(:first).replies.size, topics(:second).replies.size assert_no_queries do - assert_equal 1, topics[0].replies.size - assert_equal 0, topics[1].replies.size + assert_equal first, topics[0].replies.size + assert_equal second, topics[1].replies.size end end def test_eager_association_loading_with_belongs_to_sti replies = Reply.find(:all, :include => :topic, :order => 'topics.id') - assert_equal [topics(:second)], replies + assert replies.include?(topics(:second)) + assert !replies.include?(topics(:first)) assert_equal topics(:first), assert_no_queries { replies.first.topic } end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 91cfdee0cf..d8b107177f 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -477,7 +477,7 @@ class BasicsTest < ActiveRecord::TestCase def test_load topics = Topic.find(:all, :order => 'id') - assert_equal(2, topics.size) + assert_equal(4, topics.size) assert_equal(topics(:first).title, topics.first.title) end @@ -549,10 +549,11 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroy_all - assert_equal 2, Topic.count - - Topic.destroy_all "author_name = 'Mary'" - assert_equal 1, Topic.count + original_count = Topic.count + topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'") + + Topic.destroy_all mary + assert_equal original_count - topics_by_mary, Topic.count end def test_destroy_many @@ -562,8 +563,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_delete_many - Topic.delete([1, 2]) - assert_equal 0, Topic.count + original_count = Topic.count + Topic.delete(deleting = [1, 2]) + assert_equal original_count - deleting.size, Topic.count end def test_boolean_attributes @@ -588,21 +590,21 @@ class BasicsTest < ActiveRecord::TestCase end def test_update_all - assert_equal 2, Topic.update_all("content = 'bulk updated!'") + assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'") assert_equal "bulk updated!", Topic.find(1).content assert_equal "bulk updated!", Topic.find(2).content - assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!']) + assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!']) assert_equal "bulk updated again!", Topic.find(1).content assert_equal "bulk updated again!", Topic.find(2).content - assert_equal 2, Topic.update_all(['content = ?', nil]) + assert_equal Topic.count, Topic.update_all(['content = ?', nil]) assert_nil Topic.find(1).content end def test_update_all_with_hash assert_not_nil Topic.find(1).last_read - assert_equal 2, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil) + assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil) assert_equal "bulk updated with hash!", Topic.find(1).content assert_equal "bulk updated with hash!", Topic.find(2).content assert_nil Topic.find(1).last_read @@ -637,7 +639,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_delete_all - assert_equal 2, Topic.delete_all + assert Topic.count > 0 + + assert_equal Topic.count, Topic.delete_all end def test_update_by_condition @@ -804,17 +808,17 @@ class BasicsTest < ActiveRecord::TestCase def test_update_attributes! reply = Reply.find(2) - assert_equal "The Second Topic's of the day", reply.title + assert_equal "The Second Topic of the day", reply.title assert_equal "Have a nice day", reply.content - reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening") + reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening") reply.reload - assert_equal "The Second Topic's of the day updated", reply.title + assert_equal "The Second Topic of the day updated", reply.title assert_equal "Have a nice evening", reply.content - reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day") + reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day") reply.reload - assert_equal "The Second Topic's of the day", reply.title + assert_equal "The Second Topic of the day", reply.title assert_equal "Have a nice day", reply.content assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") } @@ -1770,7 +1774,7 @@ class BasicsTest < ActiveRecord::TestCase xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count) assert_equal "", xml.first(7) assert xml.include?(%()) - assert xml.include?(%(The Second Topic's of the day)) + assert xml.include?(%(The Second Topic of the day)) end def test_array_to_xml_including_has_many_association diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 49015133da..5da8603070 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -542,7 +542,7 @@ class FinderTest < ActiveRecord::TestCase end def test_find_all_by_array_attribute - assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic's of the day"]).size + assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size end def test_find_all_by_boolean_attribute @@ -551,7 +551,7 @@ class FinderTest < ActiveRecord::TestCase assert topics.include?(topics(:first)) topics = Topic.find_all_by_approved(true) - assert_equal 1, topics.size + assert_equal 3, topics.size assert topics.include?(topics(:second)) end @@ -563,8 +563,8 @@ class FinderTest < ActiveRecord::TestCase def test_find_all_by_nil_attribute topics = Topic.find_all_by_last_read nil - assert_equal 1, topics.size - assert_nil topics[0].last_read + assert_equal 3, topics.size + assert topics.collect(&:last_read).all?(&:nil?) end def test_find_by_nil_and_not_nil_attributes diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index dce04e63be..e30035e79e 100755 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -123,7 +123,7 @@ class FixturesTest < ActiveRecord::TestCase end def test_complete_instantiation - assert_equal 2, @topics.size + assert_equal 4, @topics.size assert_equal "The First Topic", @first.title end diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 0df7f4690d..ddcacacc3a 100755 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -68,9 +68,9 @@ class LifecycleTest < ActiveRecord::TestCase fixtures :topics, :developers def test_before_destroy - assert_equal 2, Topic.count - Topic.find(1).destroy - assert_equal 0, Topic.count + original_count = Topic.count + (topic_to_be_destroyed = Topic.find(1)).destroy + assert_equal original_count - (1 + topic_to_be_destroyed.replies.size), Topic.count end def test_after_save diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb new file mode 100644 index 0000000000..3d2cf8ba93 --- /dev/null +++ b/activerecord/test/cases/named_scope_test.rb @@ -0,0 +1,116 @@ +require "cases/helper" +require 'models/post' +require 'models/topic' +require 'models/comment' +require 'models/reply' +require 'models/author' + +class NamedScopeTest < ActiveRecord::TestCase + fixtures :posts, :authors, :topics + + def test_implements_enumerable + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all + assert_equal Topic.find(:all), Topic.all.collect + assert_equal Topic.find(:first), Topic.all.first + assert_equal Topic.find(:all), Topic.all.each { |i| i } + end + + def test_found_items_are_cached + Topic.columns + all_posts = Topic.all + + assert_queries(1) do + all_posts.collect + all_posts.collect + end + end + + def test_reload_expires_cache_of_found_items + all_posts = Topic.all + all_posts.inspect + + new_post = Topic.create! + assert !all_posts.include?(new_post) + assert all_posts.reload.include?(new_post) + end + + def test_delegates_finds_and_calculations_to_the_base_class + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all.find(:all) + assert_equal Topic.find(:first), Topic.all.find(:first) + assert_equal Topic.count, Topic.all.count + assert_equal Topic.average(:replies_count), Topic.all.average(:replies_count) + end + + def test_subclasses_inherit_scopes + assert Topic.scopes.include?(:all) + + assert Reply.scopes.include?(:all) + assert_equal Reply.find(:all), Reply.all + end + + def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified + assert !Topic.find(:all, :conditions => {:approved => true}).empty? + + assert_equal Topic.find(:all, :conditions => {:approved => true}), Topic.approved + assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count + end + + def test_scopes_are_composable + assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved + assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied + assert !(approved == replied) + assert !(approved & replied).empty? + + assert_equal approved & replied, Topic.approved.replied + end + + def test_procedural_scopes + topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on]) + topics_written_before_the_second = Topic.find(:all, :conditions => ['written_on < ?', topics(:second).written_on]) + assert_not_equal topics_written_before_the_second, topics_written_before_the_third + + assert_equal topics_written_before_the_third, Topic.written_before(topics(:third).written_on) + assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on) + end + + def test_extensions + assert_equal 1, Topic.anonymous_extension.one + assert_equal 2, Topic.named_extension.two + end + + def test_multiple_extensions + assert_equal 2, Topic.multiple_extensions.extension_two + assert_equal 1, Topic.multiple_extensions.extension_one + end + + def test_has_many_associations_have_access_to_named_scopes + assert_not_equal Post.containing_the_letter_a, authors(:david).posts + assert !Post.containing_the_letter_a.empty? + + assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a + end + + def test_has_many_through_associations_have_access_to_named_scopes + assert_not_equal Comment.containing_the_letter_e, authors(:david).posts + assert !Comment.containing_the_letter_e.empty? + + assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e + end + + def test_active_records_have_scope_named__all__ + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all + end + + def test_active_records_have_scope_named__scoped__ + assert !Topic.find(:all, scope = {:conditions => "content LIKE '%Have%'"}).empty? + + assert_equal Topic.find(:all, scope), Topic.scoped(scope) + end + +end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index f9a1b4620f..34d55dbcf6 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -428,7 +428,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_nil t2.errors.on(:title) assert t2.errors.on(:parent_id) - t2.parent_id = 3 + t2.parent_id = 4 assert t2.save, "Should now save t2 as unique" t2.parent_id = nil diff --git a/activerecord/test/fixtures/topics.yml b/activerecord/test/fixtures/topics.yml index e61d17c00c..1769152445 100644 --- a/activerecord/test/fixtures/topics.yml +++ b/activerecord/test/fixtures/topics.yml @@ -12,11 +12,31 @@ first: second: id: 2 - title: The Second Topic's of the day + title: The Second Topic of the day author_name: Mary - written_on: 2003-07-15t15:28:00.0099+01:00 + written_on: 2004-07-15t15:28:00.0099+01:00 content: Have a nice day approved: true replies_count: 0 parent_id: 1 type: Reply + +third: + id: 3 + title: The Third Topic of the day + author_name: Nick + written_on: 2005-07-15t15:28:00.0099+01:00 + content: I'm a troll + approved: true + replies_count: 1 + +fourth: + id: 4 + title: The Fourth Topic of the day + author_name: Carl + written_on: 2006-07-15t15:28:00.0099+01:00 + content: Why not? + approved: true + type: Reply + parent_id: 3 + diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 523d794fb3..79a121b4a4 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -3,6 +3,7 @@ class Author < ActiveRecord::Base has_many :posts_with_comments, :include => :comments, :class_name => "Post" has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" + has_many :posts_containing_the_letter_a, :class_name => "Post" has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension def testing_proxy_owner proxy_owner @@ -15,6 +16,7 @@ class Author < ActiveRecord::Base end end has_many :comments, :through => :posts + has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC' has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1 has_many :funky_comments, :through => :posts, :source => :comments diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 73ebdadf73..f7f07c103f 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ActiveRecord::Base + named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" + belongs_to :post, :counter_cache => true def self.what_are_you diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 7f9d53b34b..fcfe260d4a 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -1,4 +1,6 @@ class Post < ActiveRecord::Base + named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'" + belongs_to :author do def greeting "hello" diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 17e91e4b13..0ef7be97d3 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,4 +1,32 @@ class Topic < ActiveRecord::Base + named_scope :written_before, lambda { |time| + { :conditions => ['written_on < ?', time] } + } + named_scope :approved, :conditions => {:approved => true} + named_scope :replied, :conditions => ['replies_count > 0'] + named_scope :anonymous_extension do + def one + 1 + end + end + module NamedExtension + def two + 2 + end + end + module MultipleExtensionOne + def extension_one + 1 + end + end + module MultipleExtensionTwo + def extension_two + 2 + end + end + named_scope :named_extension, :extend => NamedExtension + named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" serialize :content -- cgit v1.2.3