Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
4faaa811
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4faaa811
编写于
6月 05, 2012
作者:
M
Marc-Andre Lafortune
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
+ ActiveRecord::Base#destroy!
上级
2186540f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
34 addition
and
0 deletion
+34
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+5
-0
activerecord/lib/active_record/errors.rb
activerecord/lib/active_record/errors.rb
+4
-0
activerecord/lib/active_record/persistence.rb
activerecord/lib/active_record/persistence.rb
+16
-0
activerecord/test/cases/callbacks_test.rb
activerecord/test/cases/callbacks_test.rb
+2
-0
activerecord/test/cases/persistence_test.rb
activerecord/test/cases/persistence_test.rb
+7
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
4faaa811
## Rails 4.0.0 (unreleased) ##
*
Added
`#destroy!`
which acts like
`#destroy`
but will raise an
`ActiveRecord::RecordNotDestroyed`
exception instead of returning
`false`
.
*Marc-André Lafortune*
*
Allow blocks for
`count`
with
`ActiveRecord::Relation`
, to work similar as
`Array#count`
:
...
...
activerecord/lib/active_record/errors.rb
浏览文件 @
4faaa811
...
...
@@ -53,6 +53,10 @@ class RecordNotFound < ActiveRecordError
class
RecordNotSaved
<
ActiveRecordError
end
# Raised by ActiveRecord::Base.destroy! when a call to destroy would return false.
class
RecordNotDestroyed
<
ActiveRecordError
end
# Raised when SQL statement cannot be executed by the database (for example, it's often the case for
# MySQL when Ruby driver used is too old).
class
StatementInvalid
<
ActiveRecordError
...
...
activerecord/lib/active_record/persistence.rb
浏览文件 @
4faaa811
...
...
@@ -122,6 +122,11 @@ def delete
# Deletes the record in the database and freezes this instance to reflect
# that no changes should be made (since they can't be persisted).
#
# There's a series of callbacks associated with <tt>destroy</tt>. If
# the <tt>before_destroy</tt> callback return +false+ the action is cancelled
# and <tt>destroy</tt> returns +false+. See
# ActiveRecord::Callbacks for further details.
def
destroy
raise
ReadOnlyRecord
if
readonly?
destroy_associations
...
...
@@ -130,6 +135,17 @@ def destroy
freeze
end
# Deletes the record in the database and freezes this instance to reflect
# that no changes should be made (since they can't be persisted).
#
# There's a series of callbacks associated with <tt>destroy!</tt>. If
# the <tt>before_destroy</tt> callback return +false+ the action is cancelled
# and <tt>destroy!</tt> raises ActiveRecord::RecordNotDestroyed. See
# ActiveRecord::Callbacks for further details.
def
destroy!
destroy
||
raise
(
ActiveRecord
::
RecordNotDestroyed
)
end
# Returns an instance of the specified +klass+ with the attributes of the
# current record. This is mostly useful in relation to single-table
# inheritance structures where you want a subclass to appear as the
...
...
activerecord/test/cases/callbacks_test.rb
浏览文件 @
4faaa811
...
...
@@ -426,11 +426,13 @@ def test_before_update_returning_false
def
test_before_destroy_returning_false
david
=
ImmutableDeveloper
.
find
(
1
)
assert
!
david
.
destroy
assert_raise
(
ActiveRecord
::
RecordNotDestroyed
)
{
david
.
destroy!
}
assert_not_nil
ImmutableDeveloper
.
find_by_id
(
1
)
someone
=
CallbackCancellationDeveloper
.
find
(
1
)
someone
.
cancel_before_destroy
=
true
assert
!
someone
.
destroy
assert_raise
(
ActiveRecord
::
RecordNotDestroyed
)
{
someone
.
destroy!
}
assert
!
someone
.
after_destroy_called
end
...
...
activerecord/test/cases/persistence_test.rb
浏览文件 @
4faaa811
...
...
@@ -305,6 +305,13 @@ def test_destroy
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
find
(
topic
.
id
)
}
end
def
test_destroy!
topic
=
Topic
.
find
(
1
)
assert_equal
topic
,
topic
.
destroy!
,
'topic.destroy! did not return self'
assert
topic
.
frozen?
,
'topic not frozen after destroy!'
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
find
(
topic
.
id
)
}
end
def
test_record_not_found_exception
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
Topic
.
find
(
99999
)
}
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录