Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3fe83d1d
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 搜索 >>
提交
3fe83d1d
编写于
4月 05, 2019
作者:
A
Abhay Nikam
提交者:
Ryuta Kamizono
4月 25, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adds touch option to has_one association
上级
aa5e6b59
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
81 addition
and
4 deletion
+81
-4
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/associations/builder/has_one.rb
...erecord/lib/active_record/associations/builder/has_one.rb
+34
-2
activerecord/test/cases/associations/has_one_associations_test.rb
...cord/test/cases/associations/has_one_associations_test.rb
+38
-1
activerecord/test/models/club.rb
activerecord/test/models/club.rb
+3
-1
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+2
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
3fe83d1d
*
Add
`touch`
option to
`has_one`
association.
*Abhay Nikam*
*
Deprecate
`where.not`
working as NOR and will be changed to NAND in Rails 6.1.
```ruby
...
...
activerecord/lib/active_record/associations/builder/has_one.rb
浏览文件 @
3fe83d1d
...
...
@@ -7,7 +7,7 @@ def self.macro
end
def
self
.
valid_options
(
options
)
valid
=
super
+
[
:as
]
valid
=
super
+
[
:as
,
:touch
]
valid
+=
[
:through
,
:source
,
:source_type
]
if
options
[
:through
]
valid
end
...
...
@@ -16,6 +16,11 @@ def self.valid_dependent_options
[
:destroy
,
:delete
,
:nullify
,
:restrict_with_error
,
:restrict_with_exception
]
end
def
self
.
define_callbacks
(
model
,
reflection
)
super
add_touch_callbacks
(
model
,
reflection
)
if
reflection
.
options
[
:touch
]
end
def
self
.
add_destroy_callbacks
(
model
,
reflection
)
super
unless
reflection
.
options
[
:through
]
end
...
...
@@ -27,6 +32,33 @@ def self.define_validations(model, reflection)
end
end
private_class_method
:macro
,
:valid_options
,
:valid_dependent_options
,
:add_destroy_callbacks
,
:define_validations
def
self
.
touch_record
(
o
,
name
,
touch
)
record
=
o
.
send
name
return
unless
record
&&
record
.
persisted?
if
touch
!=
true
record
.
touch
(
touch
)
else
record
.
touch
end
end
def
self
.
add_touch_callbacks
(
model
,
reflection
)
name
=
reflection
.
name
touch
=
reflection
.
options
[
:touch
]
callback
=
lambda
{
|
record
|
HasOne
.
touch_record
(
record
,
name
,
touch
)
}
model
.
after_create
callback
,
if: :saved_changes?
model
.
after_update
callback
,
if: :saved_changes?
model
.
after_destroy
callback
model
.
after_touch
callback
end
private_class_method
:macro
,
:valid_options
,
:valid_dependent_options
,
:add_destroy_callbacks
,
:define_callbacks
,
:define_validations
,
:add_touch_callbacks
end
end
activerecord/test/cases/associations/has_one_associations_test.rb
浏览文件 @
3fe83d1d
...
...
@@ -15,10 +15,13 @@
require
"models/drink_designer"
require
"models/chef"
require
"models/department"
require
"models/club"
require
"models/membership"
class
HasOneAssociationsTest
<
ActiveRecord
::
TestCase
self
.
use_transactional_tests
=
false
unless
supports_savepoints?
fixtures
:accounts
,
:companies
,
:developers
,
:projects
,
:developers_projects
,
:ships
,
:pirates
,
:authors
,
:author_addresses
fixtures
:accounts
,
:companies
,
:developers
,
:projects
,
:developers_projects
,
:ships
,
:pirates
,
:authors
,
:author_addresses
,
:memberships
,
:clubs
def
setup
Account
.
destroyed_account_ids
.
clear
...
...
@@ -706,6 +709,40 @@ def test_with_polymorphic_has_one_with_custom_columns_name
end
end
def
test_has_one_with_touch_option_on_create
assert_queries
(
3
)
{
Club
.
create
(
name:
"1000 Oaks"
,
membership_attributes:
{
favourite:
true
})
}
end
def
test_has_one_with_touch_option_on_update
new_club
=
Club
.
create
(
name:
"1000 Oaks"
)
new_club
.
create_membership
assert_queries
(
2
)
{
new_club
.
update
(
name:
"Effingut"
)
}
end
def
test_has_one_with_touch_option_on_touch
new_club
=
Club
.
create
(
name:
"1000 Oaks"
)
new_club
.
create_membership
assert_queries
(
1
)
{
new_club
.
touch
}
end
def
test_has_one_with_touch_option_on_destroy
new_club
=
Club
.
create
(
name:
"1000 Oaks"
)
new_club
.
create_membership
assert_queries
(
2
)
{
new_club
.
destroy
}
end
def
test_has_one_with_touch_option_on_empty_update
new_club
=
Club
.
create
(
name:
"1000 Oaks"
)
new_club
.
create_membership
assert_no_queries
{
new_club
.
save
}
end
class
SpecialBook
<
ActiveRecord
::
Base
self
.
table_name
=
"books"
belongs_to
:author
,
class_name:
"SpecialAuthor"
...
...
activerecord/test/models/club.rb
浏览文件 @
3fe83d1d
# frozen_string_literal: true
class
Club
<
ActiveRecord
::
Base
has_one
:membership
has_one
:membership
,
touch:
true
has_many
:memberships
,
inverse_of:
false
has_many
:members
,
through: :memberships
has_one
:sponsor
...
...
@@ -12,6 +12,8 @@ class Club < ActiveRecord::Base
scope
:general
,
->
{
left_joins
(
:category
).
where
(
categories:
{
name:
"General"
}).
unscope
(
:limit
)
}
accepts_nested_attributes_for
:membership
private
def
private_method
...
...
activerecord/test/schema/schema.rb
浏览文件 @
3fe83d1d
...
...
@@ -524,6 +524,8 @@
t
.
integer
:club_id
,
:member_id
t
.
boolean
:favourite
,
default:
false
t
.
integer
:type
t
.
datetime
:created_at
t
.
datetime
:updated_at
end
create_table
:member_types
,
force:
true
do
|
t
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录