Class: CapsuleCRM::Associations::HasManyProxy
- Inherits:
-
BasicObject
- Defined in:
- lib/capsule_crm/associations/has_many_proxy.rb
Instance Method Summary
collapse
Constructor Details
#initialize(parent, target_klass, target, source, embedded) ⇒ HasManyProxy
Returns a new instance of HasManyProxy.
5
6
7
8
9
10
11
12
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 5
def initialize(parent, target_klass, target, source, embedded)
@target = target
@parent = parent
@target_klass = target_klass
@source = source
@embedded = embedded
self
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
14
15
16
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 14
def method_missing(name, *args, &block)
target.send(name, *args, &block)
end
|
Instance Method Details
#build(attributes = {}) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 18
def build(attributes = {})
target_klass.new(attributes).tap do |item|
item.send("#{source}=", parent)
target << item
end
end
|
#create(attributes = {}) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 25
def create(attributes = {})
record = build(attributes).tap do |r|
record_not_saved(r) unless parent.persisted?
end
record if embedded? ? save : record.save
end
|
#create!(attributes = {}) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 32
def create!(attributes = {})
record = build(attributes).tap do |r|
record_not_saved(r) unless parent.persisted?
end
record if embedded? ? save! : record.save!
end
|
#save ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 48
def save
json = to_capsule_json(target_klass.serializable_options.collection_root)
path = [
'/api', parent.class.queryable_options.singular, parent.id,
target_klass.queryable_options.plural
].join('/')
::CapsuleCRM::Connection.put(path, json)
end
|
#tap {|_self| ... } ⇒ Object
39
40
41
42
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 39
def tap
yield self
self
end
|
#to_capsule_json(root = nil) ⇒ Object
44
45
46
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 44
def to_capsule_json(root = nil)
{ root => serializer.serialize_collection(target_klass, target) }
end
|