Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1b5c775e
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,发现更多精彩内容 >>
提交
1b5c775e
编写于
5月 18, 2012
作者:
F
Francesco Rodriguez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move docs from CollectionAssociation to CollectionProxy
上级
db77b16e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
143 addition
and
114 deletion
+143
-114
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+1
-113
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+142
-1
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
1b5c775e
...
...
@@ -24,7 +24,7 @@ module Associations
#
# If you need to work on all current children, new and existing records,
# +load_target+ and the +loaded+ flag are your friends.
class
CollectionAssociation
<
Association
class
CollectionAssociation
<
Association
#:nodoc:
# Implements the reader method, e.g. foo.items for Foo.has_many :items
def
reader
(
force_reload
=
false
)
...
...
@@ -127,16 +127,6 @@ def create!(attributes = {}, options = {}, &block)
# Add +records+ to this association. Returns +self+ so method calls may
# be chained. Since << flattens its argument list and inserts each record,
# +push+ and +concat+ behave identically.
#
# class Person < ActiveRecord::Base
# pets :has_many
# end
#
# person.pets << Person.new(name: 'Nemo')
# person.pets.concat(Person.new(name: 'Droopy'))
# person.pets.push(Person.new(name: 'Ren'))
#
# person.pets # => [#<Pet name: "Nemo">, #<Pet name: "Droopy">, #<Pet name: "Ren">]
def
concat
(
*
records
)
load_target
if
owner
.
new_record?
...
...
@@ -181,17 +171,6 @@ def delete_all_on_destroy
# Destroy all the records from this association.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.size # => 3
#
# person.pets.destroy_all
#
# person.pets.size # => 0
# person.pets # => []
#
# See destroy for more info.
def
destroy_all
destroy
(
load_target
).
tap
do
...
...
@@ -302,51 +281,12 @@ def length
# <tt>collection.size.zero?</tt>. If the collection has not been already
# loaded and you are going to fetch the records anyway it is better to
# check <tt>collection.length.zero?</tt>.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count # => 1
# person.pets.empty? # => false
#
# person.pets.delete_all
# person.pets.count # => 0
# person.pets.empty? # => true
def
empty?
size
.
zero?
end
# Returns true if the collections is not empty.
# Equivalent to +!collection.empty?+.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count # => 0
# person.pets.any? # => false
#
# person.pets << Pet.new(name: 'Snoop')
# person.pets.count # => 0
# person.pets.any? # => true
#
# Also, you can pass a block to define a criteria. The behaviour
# is the same, it returns true if the collection based on the
# criteria is not empty.
#
# person.pets
# # => [#<Pet name: "Snoop", group: "dogs">]
#
# person.pets.any? do |pet|
# pet.group == 'cats'
# end
# # => false
#
# person.pets.any? do |pet|
# pet.group == 'dogs'
# end
# # => true
def
any?
if
block_given?
load_target
.
any?
{
|*
block_args
|
yield
(
*
block_args
)
}
...
...
@@ -357,38 +297,6 @@ def any?
# Returns true if the collection has more than 1 record.
# Equivalent to +collection.size > 1+.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count #=> 1
# person.pets.many? #=> false
#
# person.pets << Pet.new(name: 'Snoopy')
# person.pets.count #=> 2
# person.pets.many? #=> true
#
# Also, you can pass a block to define a criteria. The
# behaviour is the same, it returns true if the collection
# based on the criteria has more than 1 record.
#
# person.pets
# # => [
# # #<Pet name: "GorbyPuff", group: "cats">,
# # #<Pet name: "Wy", group: "cats">,
# # #<Pet name: "Snoop", group: "dogs">
# # ]
#
# person.pets.many? do |pet|
# pet.group == 'dogs'
# end
# # => false
#
# person.pets.many? do |pet|
# pet.group == 'cats'
# end
# # => true
def
many?
if
block_given?
load_target
.
many?
{
|*
block_args
|
yield
(
*
block_args
)
}
...
...
@@ -406,26 +314,6 @@ def uniq(collection = load_target)
# Replace this collection with +other_array+. This will perform a diff
# and delete/add only records that have changed.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets
# # => [#<Pet id: 1, name: "Wy", group: "cats", person_id: 1>]
#
# other_pets = [Pet.new(name: 'GorbyPuff', group: 'celebrities']
#
# person.pets.replace(other_pets)
#
# person.pets
# # => [#<Pet id: 2, name: "GorbyPuff", group: "celebrities", person_id: 1>]
#
# If the supplied array has an incorrect association type, it raises
# an ActiveRecord::AssociationTypeMismatch error:
#
# person.pets.replace(["doo", "ggie", "gaga"])
# # => ActiveRecord::AssociationTypeMismatch: Pet expected, got String
def
replace
(
other_array
)
other_array
.
each
{
|
val
|
raise_on_type_mismatch
(
val
)
}
original_target
=
load_target
.
dup
...
...
activerecord/lib/active_record/associations/collection_proxy.rb
浏览文件 @
1b5c775e
...
...
@@ -33,9 +33,150 @@ module Associations
#
# is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records.
class
CollectionProxy
<
Relation
# :nodoc:
class
CollectionProxy
<
Relation
delegate
:target
,
:load_target
,
:loaded?
,
:to
=>
:@association
##
# :method: concat
# Add +records+ to this association. Returns +self+ so method calls may
# be chained. Since << flattens its argument list and inserts each record,
# +push+ and +concat+ behave identically.
#
# class Person < ActiveRecord::Base
# pets :has_many
# end
#
# person.pets << Person.new(name: 'Nemo')
# person.pets.concat(Person.new(name: 'Droopy'))
# person.pets.push(Person.new(name: 'Ren'))
#
# person.pets # => [#<Pet name: "Nemo">, #<Pet name: "Droopy">, #<Pet name: "Ren">]
##
# :method: replace
# Replace this collection with +other_array+. This will perform a diff
# and delete/add only records that have changed.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets
# # => [#<Pet id: 1, name: "Wy", group: "cats", person_id: 1>]
#
# other_pets = [Pet.new(name: 'GorbyPuff', group: 'celebrities']
#
# person.pets.replace(other_pets)
#
# person.pets
# # => [#<Pet id: 2, name: "GorbyPuff", group: "celebrities", person_id: 1>]
#
# If the supplied array has an incorrect association type, it raises
# an ActiveRecord::AssociationTypeMismatch error:
#
# person.pets.replace(["doo", "ggie", "gaga"])
# # => ActiveRecord::AssociationTypeMismatch: Pet expected, got String
##
# :method: destroy_all
# Destroy all the records from this association.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.size # => 3
#
# person.pets.destroy_all
#
# person.pets.size # => 0
# person.pets # => []
##
# :method: empty?
# Returns true if the collection is empty.
# Equivalent to +size.zero?+.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count # => 1
# person.pets.empty? # => false
#
# person.pets.delete_all
# person.pets.count # => 0
# person.pets.empty? # => true
##
# :method: any?
# Returns true if the collections is not empty.
# Equivalent to +!collection.empty?+.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count # => 0
# person.pets.any? # => false
#
# person.pets << Pet.new(name: 'Snoop')
# person.pets.count # => 0
# person.pets.any? # => true
#
# Also, you can pass a block to define a criteria. The behaviour
# is the same, it returns true if the collection based on the
# criteria is not empty.
#
# person.pets
# # => [#<Pet name: "Snoop", group: "dogs">]
#
# person.pets.any? do |pet|
# pet.group == 'cats'
# end
# # => false
#
# person.pets.any? do |pet|
# pet.group == 'dogs'
# end
# # => true
##
# :method: many?
# Returns true if the collection has more than 1 record.
# Equivalent to +collection.size > 1+.
#
# class Person < ActiveRecord::Base
# has_many :pets
# end
#
# person.pets.count #=> 1
# person.pets.many? #=> false
#
# person.pets << Pet.new(name: 'Snoopy')
# person.pets.count #=> 2
# person.pets.many? #=> true
#
# Also, you can pass a block to define a criteria. The
# behaviour is the same, it returns true if the collection
# based on the criteria has more than 1 record.
#
# person.pets
# # => [
# # #<Pet name: "GorbyPuff", group: "cats">,
# # #<Pet name: "Wy", group: "cats">,
# # #<Pet name: "Snoop", group: "dogs">
# # ]
#
# person.pets.many? do |pet|
# pet.group == 'dogs'
# end
# # => false
#
# person.pets.many? do |pet|
# pet.group == 'cats'
# end
# # => true
delegate
:select
,
:find
,
:first
,
:last
,
:build
,
:create
,
:create!
,
:concat
,
:replace
,
:delete_all
,
:destroy_all
,
:delete
,
:destroy
,
:uniq
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录