Class: ActiveMocker::HasMany
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Queries
#all, #average, #count, #delete_all, #find, #find_by, #find_by!, #find_or_create_by, #find_or_initialize_by, #limit, #maximum, #minimum, #none, #order, #reverse_order, #sum, #update, #update_all, #where
Methods inherited from Relation
#from_limit?, #inspect
Methods inherited from Collection
#<<, #==, #blank?, #each, #hash, #to_a, #to_ary
Constructor Details
#initialize(collection, options = {}) ⇒ HasMany
Returns a new instance of HasMany.
10
11
12
13
14
15
16
17
18
|
# File 'lib/active_mocker/mock/has_many.rb', line 10
def initialize(collection, options={})
@relation_class = options[:relation_class]
@foreign_key = options[:foreign_key]
@foreign_id = options[:foreign_id]
@source = options[:source]
self.class.include "#{@relation_class.name}::Scopes".constantize
super(collection)
set_foreign_key
end
|
Instance Attribute Details
#foreign_id ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
27
28
29
|
# File 'lib/active_mocker/mock/has_many.rb', line 27
def foreign_id
@foreign_id
end
|
#foreign_key ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
27
28
29
|
# File 'lib/active_mocker/mock/has_many.rb', line 27
def foreign_key
@foreign_key
end
|
#relation_class ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
27
28
29
|
# File 'lib/active_mocker/mock/has_many.rb', line 27
def relation_class
@relation_class
end
|
#source ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
27
28
29
|
# File 'lib/active_mocker/mock/has_many.rb', line 27
def source
@source
end
|
Class Method Details
.new(collection, options = {}) ⇒ Object
5
6
7
8
|
# File 'lib/active_mocker/mock/has_many.rb', line 5
def self.new(collection, options = {})
return Relation.new(collection) if options[:relation_class].nil?
super(collection, options)
end
|
Instance Method Details
#build(options = {}, &block) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/active_mocker/mock/has_many.rb', line 29
def build(options={}, &block)
new_record = relation_class.new(init_options.merge!(options), &block)
def new_record._belongs_to(collection)
@belongs_to_collection = collection
end
new_record._belongs_to(self)
def new_record.save
@belongs_to_collection << self
super
end
new_record
end
|
#create(options = {}, &block) ⇒ Object
Also known as:
create!
48
49
50
51
52
|
# File 'lib/active_mocker/mock/has_many.rb', line 48
def create(options={}, &block)
created_record = relation_class.create(init_options.merge!(options), &block)
collection << created_record
created_record
end
|
#init_options ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
57
58
59
|
# File 'lib/active_mocker/mock/has_many.rb', line 57
def init_options
{foreign_key => foreign_id}
end
|
#set_foreign_key ⇒ Object
20
21
22
23
24
|
# File 'lib/active_mocker/mock/has_many.rb', line 20
def set_foreign_key
collection.each do |item|
item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=")
end
end
|