Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
04fe959b
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 搜索 >>
未验证
提交
04fe959b
编写于
7月 14, 2020
作者:
R
Ryuta Kamizono
提交者:
GitHub
7月 14, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39820 from kamipo/enum_default
Allow default to be configured for Enum
上级
ce81714f
2d98240c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
35 addition
and
4 deletion
+35
-4
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+12
-0
activerecord/lib/active_record/attributes.rb
activerecord/lib/active_record/attributes.rb
+8
-3
activerecord/lib/active_record/enum.rb
activerecord/lib/active_record/enum.rb
+6
-1
activerecord/test/cases/enum_test.rb
activerecord/test/cases/enum_test.rb
+9
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
04fe959b
*
Allow default to be configured for Enum.
```ruby
class Book < ActiveRecord::Base
enum status: [:proposed, :written, :published], _default: :published
end
Book.new.status # => "published"
```
*Ryuta Kamizono*
*
Support
`where`
with comparison operators (
`>`
,
`>=`
,
`<`
, and
`<=`
).
```ruby
...
...
activerecord/lib/active_record/attributes.rb
浏览文件 @
04fe959b
...
...
@@ -205,13 +205,13 @@ module ClassMethods
# tracking is performed. The methods +changed?+ and +changed_in_place?+
# will be called from ActiveModel::Dirty. See the documentation for those
# methods in ActiveModel::Type::Value for more details.
def
attribute
(
name
,
cast_type
=
Type
::
Value
.
new
,
**
options
)
def
attribute
(
name
,
cast_type
=
nil
,
**
options
,
&
block
)
name
=
name
.
to_s
reload_schema_from_cache
self
.
attributes_to_define_after_schema_loads
=
attributes_to_define_after_schema_loads
.
merge
(
name
=>
[
cast_type
,
options
]
name
=>
[
cast_type
||
block
,
options
]
)
end
...
...
@@ -246,9 +246,14 @@ def define_attribute(
def
load_schema!
# :nodoc:
super
attributes_to_define_after_schema_loads
.
each
do
|
name
,
(
type
,
options
)
|
if
type
.
is_a?
(
Symbol
)
case
type
when
Symbol
adapter_name
=
ActiveRecord
::
Type
.
adapter_name_from
(
self
)
type
=
ActiveRecord
::
Type
.
lookup
(
type
,
**
options
.
except
(
:default
),
adapter:
adapter_name
)
when
Proc
type
=
type
[
type_for_attribute
(
name
)]
else
type
||=
Type
::
Value
.
new
end
define_attribute
(
name
,
type
,
**
options
.
slice
(
:default
))
...
...
activerecord/lib/active_record/enum.rb
浏览文件 @
04fe959b
...
...
@@ -159,9 +159,14 @@ def assert_valid_value(value)
def
enum
(
definitions
)
klass
=
self
enum_prefix
=
definitions
.
delete
(
:_prefix
)
enum_suffix
=
definitions
.
delete
(
:_suffix
)
enum_scopes
=
definitions
.
delete
(
:_scopes
)
default
=
{}
default
[
:default
]
=
definitions
.
delete
(
:_default
)
if
definitions
.
key?
(
:_default
)
definitions
.
each
do
|
name
,
values
|
assert_valid_enum_definition_values
(
values
)
# statuses = { }
...
...
@@ -177,7 +182,7 @@ def enum(definitions)
detect_enum_conflict!
(
name
,
"
#{
name
}
="
)
attr
=
attribute_alias?
(
name
)
?
attribute_alias
(
name
)
:
name
decorate_attribute_type
(
attr
,
:enum
)
do
|
subtype
|
attribute
(
attr
,
**
default
)
do
|
subtype
|
EnumType
.
new
(
attr
,
enum_values
,
subtype
)
end
...
...
activerecord/test/cases/enum_test.rb
浏览文件 @
04fe959b
...
...
@@ -583,6 +583,15 @@ def self.name; "Book"; end
assert_equal
:integer
,
Book
.
type_for_attribute
(
"status"
).
type
end
test
"overloaded default"
do
klass
=
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"books"
enum
status:
[
:proposed
,
:written
,
:published
],
_default: :published
end
assert_equal
"published"
,
klass
.
new
.
status
end
test
"scopes can be disabled"
do
klass
=
Class
.
new
(
ActiveRecord
::
Base
)
do
self
.
table_name
=
"books"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录