Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
09d2f168
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,发现更多精彩内容 >>
提交
09d2f168
编写于
7月 13, 2012
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
stop using class_attribute where methods/inheritance will suffice.
上级
c733b8e9
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
57 addition
and
55 deletion
+57
-55
activerecord/lib/active_record/associations/builder/association.rb
...ord/lib/active_record/associations/builder/association.rb
+18
-7
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+6
-2
activerecord/lib/active_record/associations/builder/collection_association.rb
...ive_record/associations/builder/collection_association.rb
+6
-4
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
...ve_record/associations/builder/has_and_belongs_to_many.rb
+6
-2
activerecord/lib/active_record/associations/builder/has_many.rb
...record/lib/active_record/associations/builder/has_many.rb
+6
-2
activerecord/lib/active_record/associations/builder/has_one.rb
...erecord/lib/active_record/associations/builder/has_one.rb
+8
-11
activerecord/lib/active_record/associations/builder/singular_association.rb
...ctive_record/associations/builder/singular_association.rb
+3
-1
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+4
-10
activerecord/test/cases/autosave_association_test.rb
activerecord/test/cases/autosave_association_test.rb
+0
-16
未找到文件。
activerecord/lib/active_record/associations/builder/association.rb
浏览文件 @
09d2f168
module
ActiveRecord::Associations::Builder
class
Association
#:nodoc:
class_attribute
:valid_options
self
.
valid_options
=
[
:class_name
,
:foreign_key
,
:select
,
:conditions
,
:include
,
:extend
,
:readonly
,
:validate
,
:references
]
class
<<
self
attr_accessor
:valid_options
end
# Set by subclasses
class_attribute
:macro
self
.
valid_options
=
[
:class_name
,
:foreign_key
,
:select
,
:conditions
,
:include
,
:extend
,
:readonly
,
:validate
,
:references
]
attr_reader
:model
,
:name
,
:scope
,
:options
,
:reflection
...
...
@@ -29,17 +29,28 @@ def mixin
@model
.
generated_feature_methods
end
include
Module
.
new
{
def
build
;
end
}
def
build
validate_options
reflection
=
model
.
create_reflection
(
self
.
class
.
macro
,
name
,
scope
,
options
,
model
)
define_accessors
reflection
@reflection
=
model
.
create_reflection
(
macro
,
name
,
scope
,
options
,
model
)
super
# provides an extension point
@reflection
end
def
macro
raise
NotImplementedError
end
def
valid_options
Association
.
valid_options
end
private
def
validate_options
options
.
assert_valid_keys
(
self
.
class
.
valid_options
)
options
.
assert_valid_keys
(
valid_options
)
end
def
define_accessors
...
...
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
09d2f168
...
...
@@ -2,9 +2,13 @@
module
ActiveRecord::Associations::Builder
class
BelongsTo
<
SingularAssociation
#:nodoc:
self
.
macro
=
:belongs_to
def
macro
:belongs_to
end
self
.
valid_options
+=
[
:foreign_type
,
:polymorphic
,
:touch
]
def
valid_options
super
+
[
:foreign_type
,
:polymorphic
,
:touch
]
end
def
constructable?
!
options
[
:polymorphic
]
...
...
activerecord/lib/active_record/associations/builder/collection_association.rb
浏览文件 @
09d2f168
...
...
@@ -2,10 +2,12 @@ module ActiveRecord::Associations::Builder
class
CollectionAssociation
<
Association
#:nodoc:
CALLBACKS
=
[
:before_add
,
:after_add
,
:before_remove
,
:after_remove
]
self
.
valid_options
+=
[
:table_name
,
:order
,
:group
,
:having
,
:limit
,
:offset
,
:uniq
,
:finder_sql
,
:counter_sql
,
:before_add
,
:after_add
,
:before_remove
,
:after_remove
]
def
valid_options
super
+
[
:table_name
,
:order
,
:group
,
:having
,
:limit
,
:offset
,
:uniq
,
:finder_sql
,
:counter_sql
,
:before_add
,
:after_add
,
:before_remove
,
:after_remove
]
end
attr_reader
:block_extension
...
...
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
浏览文件 @
09d2f168
module
ActiveRecord::Associations::Builder
class
HasAndBelongsToMany
<
CollectionAssociation
#:nodoc:
self
.
macro
=
:has_and_belongs_to_many
def
macro
:has_and_belongs_to_many
end
self
.
valid_options
+=
[
:join_table
,
:association_foreign_key
,
:delete_sql
,
:insert_sql
]
def
valid_options
super
+
[
:join_table
,
:association_foreign_key
,
:delete_sql
,
:insert_sql
]
end
def
build
reflection
=
super
...
...
activerecord/lib/active_record/associations/builder/has_many.rb
浏览文件 @
09d2f168
...
...
@@ -2,9 +2,13 @@
module
ActiveRecord::Associations::Builder
class
HasMany
<
CollectionAssociation
#:nodoc:
self
.
macro
=
:has_many
def
macro
:has_many
end
self
.
valid_options
+=
[
:primary_key
,
:dependent
,
:as
,
:through
,
:source
,
:source_type
,
:inverse_of
]
def
valid_options
super
+
[
:primary_key
,
:dependent
,
:as
,
:through
,
:source
,
:source_type
,
:inverse_of
]
end
def
build
reflection
=
super
...
...
activerecord/lib/active_record/associations/builder/has_one.rb
浏览文件 @
09d2f168
...
...
@@ -2,12 +2,15 @@
module
ActiveRecord::Associations::Builder
class
HasOne
<
SingularAssociation
#:nodoc:
self
.
macro
=
:has_one
self
.
valid_options
+=
[
:order
,
:as
]
def
macro
:has_one
end
class_attribute
:through_options
self
.
through_options
=
[
:through
,
:source
,
:source_type
]
def
valid_options
valid
=
super
+
[
:order
,
:as
]
valid
+=
[
:through
,
:source
,
:source_type
]
if
options
[
:through
]
valid
end
def
constructable?
!
options
[
:through
]
...
...
@@ -21,12 +24,6 @@ def build
private
def
validate_options
valid_options
=
self
.
class
.
valid_options
valid_options
+=
self
.
class
.
through_options
if
options
[
:through
]
options
.
assert_valid_keys
(
valid_options
)
end
def
configure_dependency
if
options
[
:dependent
]
unless
options
[
:dependent
].
in?
([
:destroy
,
:delete
,
:nullify
,
:restrict
])
...
...
activerecord/lib/active_record/associations/builder/singular_association.rb
浏览文件 @
09d2f168
module
ActiveRecord::Associations::Builder
class
SingularAssociation
<
Association
#:nodoc:
self
.
valid_options
+=
[
:remote
,
:dependent
,
:counter_cache
,
:primary_key
,
:inverse_of
]
def
valid_options
super
+
[
:remote
,
:dependent
,
:counter_cache
,
:primary_key
,
:inverse_of
]
end
def
constructable?
true
...
...
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
09d2f168
...
...
@@ -127,23 +127,17 @@ module ActiveRecord
module
AutosaveAssociation
extend
ActiveSupport
::
Concern
ASSOCIATION_TYPES
=
%w{ HasOne HasMany BelongsTo HasAndBelongsToMany }
module
AssociationBuilderExtension
#:nodoc:
def
self
.
included
(
base
)
base
.
valid_options
<<
:autosave
end
def
build
reflection
=
super
model
.
send
(
:add_autosave_association_callbacks
,
reflection
)
reflection
super
end
end
included
do
ASSOCIATION_TYPES
.
each
do
|
type
|
Associations
::
Builder
.
const_get
(
type
).
send
(
:include
,
AssociationBuilderExtension
)
Associations
::
Builder
::
Association
.
class_eval
do
self
.
valid_options
<<
:autosave
include
AssociationBuilderExtension
end
end
...
...
activerecord/test/cases/autosave_association_test.rb
浏览文件 @
09d2f168
...
...
@@ -20,22 +20,6 @@
require
'models/eye'
class
TestAutosaveAssociationsInGeneral
<
ActiveRecord
::
TestCase
def
test_autosave_should_be_a_valid_option_for_has_one
assert
ActiveRecord
::
Associations
::
Builder
::
HasOne
.
valid_options
.
include?
(
:autosave
)
end
def
test_autosave_should_be_a_valid_option_for_belongs_to
assert
ActiveRecord
::
Associations
::
Builder
::
BelongsTo
.
valid_options
.
include?
(
:autosave
)
end
def
test_autosave_should_be_a_valid_option_for_has_many
assert
ActiveRecord
::
Associations
::
Builder
::
HasMany
.
valid_options
.
include?
(
:autosave
)
end
def
test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
assert
ActiveRecord
::
Associations
::
Builder
::
HasAndBelongsToMany
.
valid_options
.
include?
(
:autosave
)
end
def
test_should_not_add_the_same_callbacks_multiple_times_for_has_one
assert_no_difference_when_adding_callbacks_twice_for
Pirate
,
:ship
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录