Delegate first!, last!, any? and many? to scoped

This commit is contained in:
Andrew White
2011-03-29 15:08:40 +01:00
parent e8d20b858d
commit a9dafbb28d
4 changed files with 54 additions and 2 deletions

View File

@@ -437,7 +437,8 @@ module ActiveRecord #:nodoc:
self._attr_readonly = []
class << self # Class methods
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :delete_all, :update, :update_all, :to => :scoped
delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
delegate :find_each, :find_in_batches, :to => :scoped
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :create_with, :to => :scoped
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :to => :scoped

View File

@@ -208,6 +208,14 @@ class FinderTest < ActiveRecord::TestCase
end
end
def test_model_class_responds_to_first_bang
assert_equal topics(:first), Topic.first!
assert_raises ActiveRecord::RecordNotFound do
Topic.delete_all
Topic.first!
end
end
def test_last_bang_present
assert_nothing_raised do
assert_equal topics(:second), Topic.where("title = 'The Second Topic of the day'").last!
@@ -220,6 +228,14 @@ class FinderTest < ActiveRecord::TestCase
end
end
def test_model_class_responds_to_last_bang
assert_equal topics(:fourth), Topic.last!
assert_raises ActiveRecord::RecordNotFound do
Topic.delete_all
Topic.last!
end
end
def test_unexisting_record_exception_handling
assert_raise(ActiveRecord::RecordNotFound) {
Topic.find(1).parent

View File

@@ -64,7 +64,7 @@ class NamedScopeTest < ActiveRecord::TestCase
assert Reply.scopes.include?(:base)
assert_equal Reply.find(:all), Reply.base
end
def test_classes_dont_inherit_subclasses_scopes
assert !ActiveRecord::Base.scopes.include?(:base)
end
@@ -246,6 +246,12 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_no_queries { assert topics.any? }
end
def test_model_class_should_respond_to_any
assert Topic.any?
Topic.delete_all
assert !Topic.any?
end
def test_many_should_not_load_results
topics = Topic.base
assert_queries(2) do
@@ -280,6 +286,15 @@ class NamedScopeTest < ActiveRecord::TestCase
assert Topic.base.many?
end
def test_model_class_should_respond_to_many
Topic.delete_all
assert !Topic.many?
Topic.create!
assert !Topic.many?
Topic.create!
assert Topic.many?
end
def test_should_build_on_top_of_scope
topic = Topic.approved.build({})
assert topic.approved

View File

@@ -962,6 +962,26 @@ Client.exists?
The above returns +false+ if the +clients+ table is empty and +true+ otherwise.
You can also use +any?+ and +many?+ to check for existence on a model or relation.
<ruby>
# via a model
Post.any?
Post.many?
# via a named scope
Post.recent.any?
Post.recent.many?
# via a relation
Post.where(:published => true).any?
Post.where(:published => true).many?
# via an association
Post.first.categories.any?
Post.first.categories.many?
</ruby>
h3. Calculations
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.