Skip to content

Commit

Permalink
Delegate some additional methods in querying.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenta committed Mar 29, 2016
1 parent 3c29836 commit 78ea4c5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,16 @@
* Delegate `empty?`, `none?` and `one?`. Now they can be invoked as model class methods.

Example:

# When no record is found on the table
Topic.empty? # => true
Topic.none? # => true

# When only one record is found on the table
Topic.one? # => true

*Kenta Shirai*

* The form builder now properly displays values when passing a proc form
default to the attributes API.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/querying.rb
@@ -1,6 +1,6 @@
module ActiveRecord
module Querying
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, to: :all
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :empty?, :none?, :one?, to: :all

This comment has been minimized.

Copy link
@januszm

januszm Apr 30, 2016

This might have quite big impact on 3rd party libs, empty? is used by blank? and the latter is used in validates_presence_of (validate_each to be precise)

delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!, to: :all
delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all
delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all
Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/scoping/named_scoping_test.rb
Expand Up @@ -544,4 +544,24 @@ def test_subclass_merges_scopes_properly
assert_equal 1, SpecialComment.where(body: 'go crazy').created.count
end

def test_model_class_should_respond_to_empty
assert !Topic.empty?
Topic.delete_all
assert Topic.empty?
end

def test_model_class_should_respond_to_none
assert !Topic.none?
Topic.delete_all
assert Topic.none?
end

def test_model_class_should_respond_to_one
assert !Topic.one?
Topic.delete_all
assert !Topic.one?
Topic.create!
assert Topic.one?
end

end

0 comments on commit 78ea4c5

Please sign in to comment.