Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
55a7051a
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
55a7051a
编写于
2月 26, 2019
作者:
D
David Heinemeier Hansson
提交者:
GitHub
2月 26, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add negative scopes for all enum values (#35381)
Add negative scopes for all enum values
上级
3ee0dabb
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
25 addition
and
0 deletion
+25
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+14
-0
activerecord/lib/active_record/enum.rb
activerecord/lib/active_record/enum.rb
+6
-0
activerecord/test/cases/enum_test.rb
activerecord/test/cases/enum_test.rb
+5
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
55a7051a
*
Add negative scopes for all enum values.
Example:
class Post < ActiveRecord::Base
enum status: %i[ drafted active trashed ]
end
Post.not_drafted # => where.not(status: :drafted)
Post.not_active # => where.not(status: :active)
Post.not_trashed # => where.not(status: :trashed)
*DHH*
*
Fix different
`count`
calculation when using
`size`
with manual
`select`
with DISTINCT.
Fixes #35214.
...
...
activerecord/lib/active_record/enum.rb
浏览文件 @
55a7051a
...
...
@@ -31,7 +31,9 @@ module ActiveRecord
# as well. With the above example:
#
# Conversation.active
# Conversation.not_active
# Conversation.archived
# Conversation.not_archived
#
# Of course, you can also query them directly if the scopes don't fit your
# needs:
...
...
@@ -196,9 +198,13 @@ def enum(definitions)
define_method
(
"
#{
value_method_name
}
!"
)
{
update!
(
attr
=>
value
)
}
# scope :active, -> { where(status: 0) }
# scope :not_active, -> { where.not(status: 0) }
if
enum_scopes
!=
false
klass
.
send
(
:detect_enum_conflict!
,
name
,
value_method_name
,
true
)
klass
.
scope
value_method_name
,
->
{
where
(
attr
=>
value
)
}
klass
.
send
(
:detect_enum_conflict!
,
name
,
"not_
#{
value_method_name
}
"
,
true
)
klass
.
scope
"not_
#{
value_method_name
}
"
,
->
{
where
.
not
(
attr
=>
value
)
}
end
end
end
...
...
activerecord/test/cases/enum_test.rb
浏览文件 @
55a7051a
...
...
@@ -43,6 +43,11 @@ class EnumTest < ActiveRecord::TestCase
assert_equal
books
(
:ddd
),
Book
.
forgotten
.
first
assert_equal
books
(
:rfr
),
authors
(
:david
).
unpublished_books
.
first
end
test
"find via negative scope"
do
assert
Book
.
not_published
.
exclude?
(
@book
)
assert
Book
.
not_proposed
.
include?
(
@book
)
end
test
"find via where with values"
do
published
,
written
=
Book
.
statuses
[
:published
],
Book
.
statuses
[
:written
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录