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, #first_or_create, #first_or_create!, #first_or_initialize, #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.
11
12
13
14
15
16
17
18
19
|
# File 'lib/active_mocker/mock/has_many.rb', line 11
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.
28
29
30
|
# File 'lib/active_mocker/mock/has_many.rb', line 28
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.
28
29
30
|
# File 'lib/active_mocker/mock/has_many.rb', line 28
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.
28
29
30
|
# File 'lib/active_mocker/mock/has_many.rb', line 28
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.
28
29
30
|
# File 'lib/active_mocker/mock/has_many.rb', line 28
def source
@source
end
|
Class Method Details
.new(collection, options = {}) ⇒ Object
6
7
8
9
|
# File 'lib/active_mocker/mock/has_many.rb', line 6
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/active_mocker/mock/has_many.rb', line 30
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!
49
50
51
52
53
|
# File 'lib/active_mocker/mock/has_many.rb', line 49
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.
58
59
60
|
# File 'lib/active_mocker/mock/has_many.rb', line 58
def init_options
{ foreign_key => foreign_id }
end
|
#set_foreign_key ⇒ Object
21
22
23
24
25
|
# File 'lib/active_mocker/mock/has_many.rb', line 21
def set_foreign_key
collection.each do |item|
item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=")
end
end
|