mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #15791 from zev/add_model_to_recordnotfound_message
Update RecordNotFound exception cases to include a message with the
This commit is contained in:
commit
808070b065
2 changed files with 26 additions and 18 deletions
|
@ -103,7 +103,7 @@ module ActiveRecord
|
||||||
# Same as +take+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +take+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found. Note that <tt>take!</tt> accepts no arguments.
|
# is found. Note that <tt>take!</tt> accepts no arguments.
|
||||||
def take!
|
def take!
|
||||||
take or raise RecordNotFound
|
take or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the first record (or first N records if a parameter is supplied).
|
# Find the first record (or first N records if a parameter is supplied).
|
||||||
|
@ -138,7 +138,7 @@ module ActiveRecord
|
||||||
# Same as +first+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +first+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found. Note that <tt>first!</tt> accepts no arguments.
|
# is found. Note that <tt>first!</tt> accepts no arguments.
|
||||||
def first!
|
def first!
|
||||||
first or raise RecordNotFound
|
first or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the last record (or last N records if a parameter is supplied).
|
# Find the last record (or last N records if a parameter is supplied).
|
||||||
|
@ -171,7 +171,7 @@ module ActiveRecord
|
||||||
# Same as +last+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +last+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found. Note that <tt>last!</tt> accepts no arguments.
|
# is found. Note that <tt>last!</tt> accepts no arguments.
|
||||||
def last!
|
def last!
|
||||||
last or raise RecordNotFound
|
last or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the second record.
|
# Find the second record.
|
||||||
|
@ -187,7 +187,7 @@ module ActiveRecord
|
||||||
# Same as +second+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +second+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found.
|
# is found.
|
||||||
def second!
|
def second!
|
||||||
second or raise RecordNotFound
|
second or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the third record.
|
# Find the third record.
|
||||||
|
@ -203,7 +203,7 @@ module ActiveRecord
|
||||||
# Same as +third+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +third+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found.
|
# is found.
|
||||||
def third!
|
def third!
|
||||||
third or raise RecordNotFound
|
third or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the fourth record.
|
# Find the fourth record.
|
||||||
|
@ -219,7 +219,7 @@ module ActiveRecord
|
||||||
# Same as +fourth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +fourth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found.
|
# is found.
|
||||||
def fourth!
|
def fourth!
|
||||||
fourth or raise RecordNotFound
|
fourth or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the fifth record.
|
# Find the fifth record.
|
||||||
|
@ -235,7 +235,7 @@ module ActiveRecord
|
||||||
# Same as +fifth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +fifth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found.
|
# is found.
|
||||||
def fifth!
|
def fifth!
|
||||||
fifth or raise RecordNotFound
|
fifth or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find the forty-second record. Also known as accessing "the reddit".
|
# Find the forty-second record. Also known as accessing "the reddit".
|
||||||
|
@ -251,7 +251,7 @@ module ActiveRecord
|
||||||
# Same as +forty_two+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
# Same as +forty_two+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||||
# is found.
|
# is found.
|
||||||
def forty_two!
|
def forty_two!
|
||||||
forty_two or raise RecordNotFound
|
forty_two or raise RecordNotFound.new("Couldn't find #{@klass.name} with [#{arel.where_sql}]")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns +true+ if a record exists in the table that matches the +id+ or
|
# Returns +true+ if a record exists in the table that matches the +id+ or
|
||||||
|
|
|
@ -262,7 +262,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_take_bang_missing
|
def test_take_bang_missing
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.where("title = 'This title does not exist'").take!
|
Topic.where("title = 'This title does not exist'").take!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -282,7 +282,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_first_bang_missing
|
def test_first_bang_missing
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.where("title = 'This title does not exist'").first!
|
Topic.where("title = 'This title does not exist'").first!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -296,7 +296,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
def test_model_class_responds_to_first_bang
|
def test_model_class_responds_to_first_bang
|
||||||
assert Topic.first!
|
assert Topic.first!
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.first!
|
Topic.first!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -318,7 +318,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
def test_model_class_responds_to_second_bang
|
def test_model_class_responds_to_second_bang
|
||||||
assert Topic.second!
|
assert Topic.second!
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.second!
|
Topic.second!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -340,7 +340,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
def test_model_class_responds_to_third_bang
|
def test_model_class_responds_to_third_bang
|
||||||
assert Topic.third!
|
assert Topic.third!
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.third!
|
Topic.third!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -362,7 +362,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
def test_model_class_responds_to_fourth_bang
|
def test_model_class_responds_to_fourth_bang
|
||||||
assert Topic.fourth!
|
assert Topic.fourth!
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.fourth!
|
Topic.fourth!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -384,7 +384,7 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
def test_model_class_responds_to_fifth_bang
|
def test_model_class_responds_to_fifth_bang
|
||||||
assert Topic.fifth!
|
assert Topic.fifth!
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.fifth!
|
Topic.fifth!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -396,14 +396,14 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_last_bang_missing
|
def test_last_bang_missing
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.where("title = 'This title does not exist'").last!
|
Topic.where("title = 'This title does not exist'").last!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_model_class_responds_to_last_bang
|
def test_model_class_responds_to_last_bang
|
||||||
assert_equal topics(:fifth), Topic.last!
|
assert_equal topics(:fifth), Topic.last!
|
||||||
assert_raises ActiveRecord::RecordNotFound do
|
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
|
||||||
Topic.delete_all
|
Topic.delete_all
|
||||||
Topic.last!
|
Topic.last!
|
||||||
end
|
end
|
||||||
|
@ -802,7 +802,9 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
def test_find_by_one_attribute_bang
|
def test_find_by_one_attribute_bang
|
||||||
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
|
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
|
||||||
assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
|
assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
|
||||||
|
Topic.find_by_title!("The First Topic!")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_by_on_attribute_that_is_a_reserved_word
|
def test_find_by_on_attribute_that_is_a_reserved_word
|
||||||
|
@ -1113,4 +1115,10 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def assert_raises_with_message(exception_class, message, &block)
|
||||||
|
err = assert_raises(exception_class) { block.call }
|
||||||
|
assert_match message, err.message
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue