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
|
# 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
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
13
14
15
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 13
def method_missing(name, *args, &block)
target.send(name, *args, &block)
end
|
Instance Method Details
#build(attributes = {}) ⇒ Object
17
18
19
20
21
22
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 17
def build(attributes = {})
target_klass.new(attributes).tap do |item|
item.send("#{source}=", parent)
target << item
end
end
|
#create(attributes = {}) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 24
def create(attributes = {})
record = build(attributes).tap do |r|
record_not_saved(r) unless parent.persisted?
end
embedded? ? save : record.save
end
|
#create!(attributes = {}) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 31
def create!(attributes = {})
record = build(attributes).tap do |r|
record_not_saved(r) unless parent.persisted?
end
embedded? ? save! : record.save!
end
|
#save ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 47
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
38
39
40
41
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 38
def tap
yield self
self
end
|
#to_capsule_json(root = nil) ⇒ Object
43
44
45
|
# File 'lib/capsule_crm/associations/has_many_proxy.rb', line 43
def to_capsule_json(root = nil)
{ root => serializer.serialize_collection(target_klass, target) }
end
|