Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
7a036ebd
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,发现更多精彩内容 >>
提交
7a036ebd
编写于
12月 11, 2013
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert the whole refactoring in the association builder classes.
This is to get activerecord-deprecated_finders work again
上级
5853c64a
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
64 addition
and
47 deletion
+64
-47
activerecord/lib/active_record/associations/builder/association.rb
...ord/lib/active_record/associations/builder/association.rb
+28
-28
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+4
-2
activerecord/lib/active_record/associations/builder/collection_association.rb
...ive_record/associations/builder/collection_association.rb
+18
-6
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
...ve_record/associations/builder/has_and_belongs_to_many.rb
+5
-5
activerecord/lib/active_record/associations/builder/has_many.rb
...record/lib/active_record/associations/builder/has_many.rb
+2
-2
activerecord/lib/active_record/associations/builder/has_one.rb
...erecord/lib/active_record/associations/builder/has_one.rb
+4
-2
activerecord/lib/active_record/associations/builder/singular_association.rb
...ctive_record/associations/builder/singular_association.rb
+1
-1
activerecord/test/cases/associations/extension_test.rb
activerecord/test/cases/associations/extension_test.rb
+2
-1
未找到文件。
activerecord/lib/active_record/associations/builder/association.rb
浏览文件 @
7a036ebd
...
...
@@ -15,23 +15,26 @@ module ActiveRecord::Associations::Builder
class
Association
#:nodoc:
class
<<
self
attr_accessor
:extensions
# TODO: This class accessor is needed to make activerecord-deprecated_finders work.
# We can move it to a constant in 5.0.
attr_accessor
:valid_options
end
self
.
extensions
=
[]
# TODO: This class accessor is needed to make activerecord-deprecated_finders work.
# We can move it to a constant in 5.0.
cattr_accessor
:valid_options
,
instance_accessor:
false
self
.
valid_options
=
[
:class_name
,
:class
,
:foreign_key
,
:validate
]
attr_reader
:name
,
:scope
,
:options
def
self
.
build
(
model
,
name
,
scope
,
options
,
&
block
)
extension
=
define_extensions
model
,
name
,
&
block
reflection
=
create_reflection
model
,
name
,
scope
,
options
,
extension
builder
=
create_builder
model
,
name
,
scope
,
options
,
&
block
reflection
=
builder
.
build
(
model
)
define_accessors
model
,
reflection
define_callbacks
model
,
reflection
builder
.
define_extensions
model
reflection
end
def
self
.
create_
reflection
(
model
,
name
,
scope
,
options
,
extension
=
nil
)
def
self
.
create_
builder
(
model
,
name
,
scope
,
options
,
&
block
)
raise
ArgumentError
,
"association names must be a Symbol"
unless
name
.
kind_of?
(
Symbol
)
if
scope
.
is_a?
(
Hash
)
...
...
@@ -39,44 +42,39 @@ def self.create_reflection(model, name, scope, options, extension = nil)
scope
=
nil
end
validate_options
(
options
)
scope
=
build_scope
(
scope
,
extension
)
ActiveRecord
::
Reflection
.
create
(
macro
,
name
,
scope
,
options
,
model
)
new
(
model
,
name
,
scope
,
options
,
&
block
)
end
def
self
.
build_scope
(
scope
,
extension
)
new_scope
=
scope
def
initialize
(
model
,
name
,
scope
,
options
)
# TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders.
@name
=
name
@scope
=
scope
@options
=
options
if
scope
&&
scope
.
arity
==
0
new_scope
=
proc
{
instance_exec
(
&
scope
)
}
end
validate_options
if
extension
new_scope
=
wrap_scope
new_scope
,
extension
if
scope
&&
scope
.
arity
==
0
@scope
=
proc
{
instance_exec
(
&
scope
)
}
end
new_scope
end
def
self
.
wrap_scope
(
scope
,
extension
)
scope
def
build
(
model
)
ActiveRecord
::
Reflection
.
create
(
macro
,
name
,
scope
,
options
,
model
)
end
def
self
.
macro
def
macro
raise
NotImplementedError
end
def
self
.
build_valid_options
(
options
)
self
.
valid_options
+
Association
.
extensions
.
flat_map
(
&
:valid_options
)
def
valid_options
Association
.
valid_options
+
Association
.
extensions
.
flat_map
(
&
:valid_options
)
end
def
self
.
validate_options
(
options
)
options
.
assert_valid_keys
(
build_valid_options
(
options
)
)
def
validate_options
options
.
assert_valid_keys
(
valid_options
)
end
def
self
.
define_extensions
(
model
,
name
)
def
define_extensions
(
model
)
end
def
self
.
define_callbacks
(
model
,
reflection
)
...
...
@@ -119,6 +117,8 @@ def self.valid_dependent_options
raise
NotImplementedError
end
private
def
self
.
add_before_destroy_callbacks
(
model
,
reflection
)
unless
valid_dependent_options
.
include?
reflection
.
options
[
:dependent
]
raise
ArgumentError
,
"The :dependent option must be one of
#{
valid_dependent_options
}
, but is :
#{
reflection
.
options
[
:dependent
]
}
"
...
...
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
7a036ebd
module
ActiveRecord::Associations::Builder
class
BelongsTo
<
SingularAssociation
#:nodoc:
def
self
.
macro
def
macro
:belongs_to
end
def
self
.
build_valid_options
(
options
)
def
valid_options
super
+
[
:foreign_type
,
:polymorphic
,
:touch
,
:counter_cache
]
end
...
...
@@ -23,6 +23,8 @@ def self.define_accessors(mixin, reflection)
add_counter_cache_methods
mixin
end
private
def
self
.
add_counter_cache_methods
(
mixin
)
return
if
mixin
.
method_defined?
:belongs_to_counter_cache_after_create
...
...
activerecord/lib/active_record/associations/builder/collection_association.rb
浏览文件 @
7a036ebd
...
...
@@ -7,11 +7,22 @@ class CollectionAssociation < Association #:nodoc:
CALLBACKS
=
[
:before_add
,
:after_add
,
:before_remove
,
:after_remove
]
def
self
.
build_valid_options
(
options
)
def
valid_options
super
+
[
:table_name
,
:before_add
,
:after_add
,
:before_remove
,
:after_remove
,
:extend
]
end
attr_reader
:block_extension
def
initialize
(
model
,
name
,
scope
,
options
)
super
@mod
=
nil
if
block_given?
@mod
=
Module
.
new
(
&
Proc
.
new
)
@scope
=
wrap_scope
@scope
,
@mod
end
end
def
self
.
define_callbacks
(
model
,
reflection
)
super
name
=
reflection
.
name
...
...
@@ -21,11 +32,10 @@ def self.define_callbacks(model, reflection)
}
end
def
self
.
define_extensions
(
model
,
name
)
if
block_given?
def
define_extensions
(
model
)
if
@mod
extension_module_name
=
"
#{
model
.
name
.
demodulize
}#{
name
.
to_s
.
camelize
}
AssociationExtension"
extension
=
Module
.
new
(
&
Proc
.
new
)
model
.
parent
.
const_set
(
extension_module_name
,
extension
)
model
.
parent
.
const_set
(
extension_module_name
,
@mod
)
end
end
...
...
@@ -68,7 +78,9 @@ def #{name.to_s.singularize}_ids=(ids)
CODE
end
def
self
.
wrap_scope
(
scope
,
mod
)
private
def
wrap_scope
(
scope
,
mod
)
if
scope
proc
{
|
owner
|
instance_exec
(
owner
,
&
scope
).
extending
(
mod
)
}
else
...
...
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
浏览文件 @
7a036ebd
...
...
@@ -84,11 +84,11 @@ def middle_reflection(join_model)
middle_name
=
[
lhs_model
.
name
.
downcase
.
pluralize
,
association_name
].
join
(
'_'
).
gsub
(
/::/
,
'_'
).
to_sym
middle_options
=
middle_options
join_model
HasMany
.
create_reflection
(
lhs_model
,
middle_name
,
nil
,
middle_options
)
hm_builder
=
HasMany
.
create_builder
(
lhs_model
,
middle_name
,
nil
,
middle_options
)
hm_builder
.
build
lhs_model
end
private
...
...
activerecord/lib/active_record/associations/builder/has_many.rb
浏览文件 @
7a036ebd
module
ActiveRecord::Associations::Builder
class
HasMany
<
CollectionAssociation
#:nodoc:
def
self
.
macro
def
macro
:has_many
end
def
self
.
build_valid_options
(
options
)
def
valid_options
super
+
[
:primary_key
,
:dependent
,
:as
,
:through
,
:source
,
:source_type
,
:inverse_of
,
:counter_cache
]
end
...
...
activerecord/lib/active_record/associations/builder/has_one.rb
浏览文件 @
7a036ebd
module
ActiveRecord::Associations::Builder
class
HasOne
<
SingularAssociation
#:nodoc:
def
self
.
macro
def
macro
:has_one
end
def
self
.
build_valid_options
(
options
)
def
valid_options
valid
=
super
+
[
:order
,
:as
]
valid
+=
[
:through
,
:source
,
:source_type
]
if
options
[
:through
]
valid
...
...
@@ -14,6 +14,8 @@ def self.valid_dependent_options
[
:destroy
,
:delete
,
:nullify
,
:restrict_with_error
,
:restrict_with_exception
]
end
private
def
self
.
add_before_destroy_callbacks
(
model
,
reflection
)
super
unless
reflection
.
options
[
:through
]
end
...
...
activerecord/lib/active_record/associations/builder/singular_association.rb
浏览文件 @
7a036ebd
...
...
@@ -2,7 +2,7 @@
module
ActiveRecord::Associations::Builder
class
SingularAssociation
<
Association
#:nodoc:
def
self
.
build_valid_options
(
options
)
def
valid_options
super
+
[
:remote
,
:dependent
,
:primary_key
,
:inverse_of
]
end
...
...
activerecord/test/cases/associations/extension_test.rb
浏览文件 @
7a036ebd
...
...
@@ -75,6 +75,7 @@ def test_proxy_association_after_scoped
private
def
extend!
(
model
)
ActiveRecord
::
Associations
::
Builder
::
HasMany
.
define_extensions
(
model
,
:association_name
)
{
}
builder
=
ActiveRecord
::
Associations
::
Builder
::
HasMany
.
new
(
model
,
:association_name
,
nil
,
{})
{
}
builder
.
define_extensions
(
model
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录