capsule_crm 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/lib/capsule_crm/address.rb +4 -3
  4. data/lib/capsule_crm/associations/belongs_to.rb +2 -1
  5. data/lib/capsule_crm/associations/belongs_to_association.rb +19 -2
  6. data/lib/capsule_crm/associations/belongs_to_finder.rb +46 -0
  7. data/lib/capsule_crm/associations/has_many_association.rb +23 -9
  8. data/lib/capsule_crm/associations/has_many_proxy.rb +38 -4
  9. data/lib/capsule_crm/case.rb +33 -236
  10. data/lib/capsule_crm/configuration.rb +1 -1
  11. data/lib/capsule_crm/connection.rb +3 -0
  12. data/lib/capsule_crm/contactable.rb +1 -1
  13. data/lib/capsule_crm/country.rb +7 -13
  14. data/lib/capsule_crm/currency.rb +7 -13
  15. data/lib/capsule_crm/custom_field.rb +14 -54
  16. data/lib/capsule_crm/custom_field_definition.rb +36 -0
  17. data/lib/capsule_crm/email.rb +4 -3
  18. data/lib/capsule_crm/gettable.rb +11 -0
  19. data/lib/capsule_crm/hash_helper.rb +5 -0
  20. data/lib/capsule_crm/history.rb +37 -252
  21. data/lib/capsule_crm/milestone.rb +9 -9
  22. data/lib/capsule_crm/normalizer.rb +85 -0
  23. data/lib/capsule_crm/opportunity.rb +30 -271
  24. data/lib/capsule_crm/organization.rb +19 -197
  25. data/lib/capsule_crm/party.rb +13 -26
  26. data/lib/capsule_crm/persistence/configuration.rb +25 -0
  27. data/lib/capsule_crm/persistence/deletable.rb +14 -0
  28. data/lib/capsule_crm/persistence/persistable.rb +76 -0
  29. data/lib/capsule_crm/persistence.rb +3 -0
  30. data/lib/capsule_crm/person.rb +19 -194
  31. data/lib/capsule_crm/phone.rb +4 -3
  32. data/lib/capsule_crm/querying/configuration.rb +21 -0
  33. data/lib/capsule_crm/querying/find_all.rb +16 -0
  34. data/lib/capsule_crm/querying/find_one.rb +14 -0
  35. data/lib/capsule_crm/querying/findable.rb +14 -0
  36. data/lib/capsule_crm/querying.rb +4 -0
  37. data/lib/capsule_crm/serializable.rb +38 -0
  38. data/lib/capsule_crm/serializer.rb +54 -7
  39. data/lib/capsule_crm/task.rb +16 -96
  40. data/lib/capsule_crm/track.rb +5 -10
  41. data/lib/capsule_crm/user.rb +3 -15
  42. data/lib/capsule_crm/version.rb +1 -1
  43. data/lib/capsule_crm/website.rb +4 -2
  44. data/lib/capsule_crm.rb +15 -5
  45. data/spec/fabricators/case_fabricator.rb +1 -0
  46. data/spec/fabricators/history_fabricator.rb +1 -0
  47. data/spec/fabricators/opportunity_fabricator.rb +1 -0
  48. data/spec/lib/capsule_crm/associations/belongs_to_finder_spec.rb +48 -0
  49. data/spec/lib/capsule_crm/associations/belongs_to_spec.rb +34 -0
  50. data/spec/lib/capsule_crm/associations/has_many_proxy_spec.rb +54 -14
  51. data/spec/lib/capsule_crm/associations/has_many_spec.rb +15 -2
  52. data/spec/lib/capsule_crm/case_spec.rb +20 -330
  53. data/spec/lib/capsule_crm/country_spec.rb +1 -16
  54. data/spec/lib/capsule_crm/currency_spec.rb +1 -18
  55. data/spec/lib/capsule_crm/custom_field_definition_spec.rb +59 -0
  56. data/spec/lib/capsule_crm/custom_field_spec.rb +2 -27
  57. data/spec/lib/capsule_crm/history_spec.rb +14 -389
  58. data/spec/lib/capsule_crm/milestone_spec.rb +1 -23
  59. data/spec/lib/capsule_crm/normalizer_spec.rb +11 -0
  60. data/spec/lib/capsule_crm/opportunity_spec.rb +22 -341
  61. data/spec/lib/capsule_crm/organization_spec.rb +39 -60
  62. data/spec/lib/capsule_crm/party_spec.rb +37 -59
  63. data/spec/lib/capsule_crm/person_spec.rb +49 -247
  64. data/spec/lib/capsule_crm/serializer_spec.rb +25 -4
  65. data/spec/lib/capsule_crm/task_spec.rb +21 -250
  66. data/spec/lib/capsule_crm/track_spec.rb +1 -15
  67. data/spec/lib/capsule_crm/user_spec.rb +1 -14
  68. data/spec/support/{countries.json → all_countries.json} +0 -0
  69. data/spec/support/{currencies.json → all_currencies.json} +0 -0
  70. data/spec/support/{milestones.json → all_milestones.json} +0 -0
  71. data/spec/support/{tracks.json → all_tracks.json} +0 -0
  72. data/spec/support/custom_field_definitions.json +19 -0
  73. data/spec/support/helpers.rb +3 -2
  74. data/spec/support/no_cases.json +5 -0
  75. data/spec/support/no_countries.json +5 -0
  76. data/spec/support/no_currencies.json +5 -0
  77. data/spec/support/no_milestones.json +5 -0
  78. data/spec/support/no_opportunities.json +5 -0
  79. data/spec/support/no_tasks.json +5 -0
  80. data/spec/support/no_tracks.json +5 -0
  81. data/spec/support/no_users.json +5 -0
  82. data/spec/support/shared_examples/deletable.rb +15 -0
  83. data/spec/support/shared_examples/find_all.rb +34 -0
  84. data/spec/support/shared_examples/find_one.rb +23 -0
  85. data/spec/support/shared_examples/persistable.rb +318 -0
  86. data/spec/support/single_person.json +16 -0
  87. metadata +60 -15
  88. data/lib/capsule_crm/attributes.rb +0 -11
  89. data/lib/capsule_crm/capsule_jsonable.rb +0 -13
  90. data/lib/capsule_crm/collection.rb +0 -9
  91. data/spec/support/single_user.json +0 -25
@@ -7,8 +7,26 @@ module CapsuleCRM
7
7
  include ActiveModel::Validations
8
8
 
9
9
  include CapsuleCRM::Associations
10
- include CapsuleCRM::Attributes
11
- include CapsuleCRM::Collection
10
+ include CapsuleCRM::Querying::Findable
11
+ include CapsuleCRM::Persistence::Persistable
12
+ include CapsuleCRM::Persistence::Deletable
13
+ include CapsuleCRM::Serializable
14
+
15
+ serializable_config do |config|
16
+ config.excluded_keys = [:track_id]
17
+ end
18
+
19
+ queryable_config do |config|
20
+ config.plural = :opportunity
21
+ end
22
+
23
+ persistable_config do |config|
24
+ config.create = lambda do |opportunity|
25
+ path = "party/#{opportunity.party.try(:id)}/opportunity"
26
+ path += "?trackId=#{opportunity.track_id}" if opportunity.track_id
27
+ path
28
+ end
29
+ end
12
30
 
13
31
  attribute :id, Integer
14
32
  attribute :name, String
@@ -29,11 +47,13 @@ module CapsuleCRM
29
47
  validates :party, presence: true
30
48
  validates :milestone, presence: true
31
49
 
32
- has_many :tasks, class_name: 'CapsuleCRM::Task', source: :opportunity
50
+ has_many :tasks
51
+ has_many :histories
52
+ has_many :custom_fields, embedded: true
33
53
 
34
- belongs_to :party, class_name: 'CapsuleCRM::Party'
35
- belongs_to :milestone, class_name: 'CapsuleCRM::Milestone'
36
- belongs_to :track, class_name: 'CapsuleCRM::Track'
54
+ belongs_to :party
55
+ belongs_to :milestone
56
+ belongs_to :track
37
57
 
38
58
  def milestone=(milestone)
39
59
  if milestone.is_a?(String)
@@ -48,34 +68,6 @@ module CapsuleCRM
48
68
  raise NotImplementedError.new("There is no way to find opportunities by trackId in the Capsule API right now")
49
69
  end
50
70
 
51
- # Public: Get all opportunities from Capsule. The list can be restricted
52
- # and/or paginated with various query parameters sent through the options
53
- # hash.
54
- #
55
- # options - The Hash of allowed query parameters for Capsule (default: {}):
56
- # :milestone - The String milestone name
57
- # :lastmodified - The Date when the opportunity was last modified
58
- # :tag - The String tag to search for
59
- # :start - The Integer first record to be returned in pagination.
60
- # The results start with an index of 1
61
- # :limit - The Integer maximum number of matching records to be
62
- # returned
63
- #
64
- # Examples
65
- #
66
- # CapsuleCRM::Opportunity.all
67
- #
68
- # CapsuleCRM::Opportunity.all(start: 10, limit: 20)
69
- #
70
- # Returns a ResultsProxy of opportunities
71
- def self.all(options = {})
72
- init_collection(
73
- CapsuleCRM::Connection.get(
74
- '/api/opportunity', options
75
- )['opportunities']['opportunity']
76
- )
77
- end
78
-
79
71
  # Public: Get all deleted opportunities since the specified date
80
72
  #
81
73
  # since - The Date to start checking for deleted opportunities
@@ -86,244 +78,11 @@ module CapsuleCRM
86
78
  #
87
79
  # Returns a ResultsProxy of opportunities
88
80
  def self.deleted(since)
89
- init_collection(
90
- CapsuleCRM::Connection.get(
91
- '/api/opportunity/deleted', since: since
92
- )['deletedOpportunities']['deletedOpportunity']
81
+ CapsuleCRM::Normalizer.new(
82
+ self, root: 'deletedOpportunity', collection_root: 'deletedOpportunities'
83
+ ).normalize_collection(
84
+ CapsuleCRM::Connection.get('/api/opportunity/deleted', since: since)
93
85
  )
94
86
  end
95
-
96
- # Public: Find an opportunity by id
97
- #
98
- # id - The Integer ID
99
- #
100
- # Examples
101
- #
102
- # CapsuleCRM::Opportunity.find(id)
103
- #
104
- # Returns a CapsuleCRM::Opportunity
105
- def self.find(id)
106
- new CapsuleCRM::Connection.get("/api/opportunity/#{id}")['opportunity']
107
- end
108
-
109
- # Public: Create a new opportunity in capsulecrm
110
- #
111
- # attributes - The Hash of opportunity attributes (default: {}):
112
- # :name - The String opportunity name
113
- # :description - The String opportunity description
114
- # :currency - The String currency code
115
- # :value - The Float opportunity (financial) value
116
- # :duration_basis - The String duration basis
117
- # :duration - The Integer duration (for opportunities
118
- # with a repeating (not FIXED) duratin basis
119
- # :party_id - The Integer party id
120
- # :milestone_id - The Integer milestone id
121
- # :expected_close_date - The DateTime when the opportunity
122
- # is expected to be closed
123
- # :actual_close_date - The DateTime when the opportunity
124
- # was actually closed
125
- # :probability - The Float probability that this
126
- # opportunity will be won
127
- #
128
- # Examples
129
- #
130
- # CapsuleCRM::opportunity.create(name: 'Test', milestone_id: 1)
131
- #
132
- # Returns a CapsuleCRM::opportunity
133
- def self.create(attributes = {})
134
- new(attributes).tap(&:save)
135
- end
136
-
137
- # Public: Create a new opportunity in capsulecrm and raise a
138
- # CapsuleCRM::Errors::InvalidRecord error if not possible
139
- #
140
- # attributes - The Hash of opportunity attributes (default: {}):
141
- # :name - The String opportunity name
142
- # :description - The String opportunity description
143
- # :currency - The String currency code
144
- # :value - The Float opportunity (financial) value
145
- # :duration_basis - The String duration basis
146
- # :duration - The Integer duration (for opportunities
147
- # with a repeating (not FIXED) duratin basis
148
- # :party_id - The Integer party id
149
- # :milestone_id - The Integer milestone id
150
- # :expected_close_date - The DateTime when the opportunity
151
- # is expected to be closed
152
- # :actual_close_date - The DateTime when the opportunity
153
- # was actually closed
154
- # :probability - The Float probability that this
155
- # opportunity will be won
156
- #
157
- # Examples
158
- #
159
- # CapsuleCRM::opportunity.create!(name: 'Test', milestone_id: 1)
160
- #
161
- # Returns a CapsuleCRM
162
- def self.create!(attributes = {})
163
- new(attributes).tap(&:save!)
164
- end
165
-
166
- # Public: If the opportunity already exists in capsule then update them,
167
- # otherwise create a new opportunity
168
- #
169
- # Examples
170
- #
171
- # opportunity = CapsuleCRM::Opportunity.new(name: 'Test', milestone_id: 1)
172
- # opportunity.save
173
- #
174
- # opportunity = CapsuleCRM::Opportunity.find(1)
175
- # opportunity.name = 'Another Test'
176
- # opportunity.save
177
- #
178
- # Returns a CapsuleCRM::opportunity
179
- def save
180
- if valid?
181
- new_record? ? create_record : update_record
182
- else
183
- false
184
- end
185
- end
186
-
187
- # Public: If the opportunity already exists in capsule then update them,
188
- # otherwise create a new opportunity. If the opportunity is not valid then a
189
- # CapsuleCRM::Errors::RecordInvalid exception is raised
190
- #
191
- # Examples
192
- #
193
- # opportunity = CapsuleCRM::Opportunity.new(name: 'Test, milestone_id: 1)
194
- # opportunity.save
195
- #
196
- # opportunity = CapsuleCRM::Opportunity.find(1)
197
- # opportunity.name = 'Another test'
198
- # opportunity.save
199
- #
200
- # Returns a CapsuleCRM::opportunity
201
- def save!
202
- if valid?
203
- new_record? ? create_record : update_record
204
- else
205
- raise CapsuleCRM::Errors::RecordInvalid.new(self)
206
- end
207
- end
208
-
209
- # Public: Determine whether this CapsuleCRM::opportunity is a new record or not
210
- #
211
- # Returns a Boolean
212
- def new_record?
213
- !id
214
- end
215
-
216
- # Public: Determine whether or not this CapsuleCRM::opportunity has already been
217
- # persisted to capsulecrm
218
- #
219
- # Returns a Boolean
220
- def persisted?
221
- !new_record?
222
- end
223
-
224
- # Public: Update the opportunity in capsule
225
- #
226
- # attributes - The Hash of opportunity attributes (default: {}):
227
- # :name - The String opportunity name
228
- # :description - The String opportunity description
229
- # :currency - The String currency code
230
- # :value - The Float opportunity (financial) value
231
- # :duration_basis - The String duration basis
232
- # :duration - The Integer duration (for opportunities
233
- # with a repeating (not FIXED) duratin basis
234
- # :party_id - The Integer party id
235
- # :milestone_id - The Integer milestone id
236
- # :expected_close_date - The DateTime when the opportunity
237
- # is expected to be closed
238
- # :actual_close_date - The DateTime when the opportunity
239
- # was actually closed
240
- # :probability - The Float probability that this
241
- # opportunity will be won
242
- # Examples
243
- #
244
- # opportunity = CapsuleCRM::Opportunity.find(1)
245
- # opportunity.update_attributes name: 'A New Name'
246
- #
247
- # Returns a CapsuleCRM::opportunity
248
- def update_attributes(attributes = {})
249
- self.attributes = attributes
250
- save
251
- end
252
-
253
- # Public: Update the opportunity in capsule. If the person is not valid then a
254
- # CapsuleCRM::Errors::RecordInvalid exception will be raised
255
- #
256
- # attributes - The Hash of opportunity attributes (default: {}):
257
- # :name - The String opportunity name
258
- # :description - The String opportunity description
259
- # :currency - The String currency code
260
- # :value - The Float opportunity (financial) value
261
- # :duration_basis - The String duration basis
262
- # :duration - The Integer duration (for opportunities
263
- # with a repeating (not FIXED) duratin basis
264
- # :party_id - The Integer party id
265
- # :milestone_id - The Integer milestone id
266
- # :expected_close_date - The DateTime when the opportunity
267
- # is expected to be closed
268
- # :actual_close_date - The DateTime when the opportunity
269
- # was actually closed
270
- # :probability - The Float probability that this
271
- # opportunity will be won
272
- #
273
- # Examples
274
- #
275
- # opportunity = CapsuleCRM::Opportunity.find(1)
276
- # opportunity.update_attributes! name: 'A New Name'
277
- # => CapsuleCRM::Opportunity
278
- #
279
- # opportunity.update_attributes! name: nil
280
- # => CapsuleCRM::Errors::RecordInvalid
281
- #
282
- # Returns a CapsuleCRM::opportunity
283
- def update_attributes!(attributes = {})
284
- self.attributes = attributes
285
- save!
286
- end
287
-
288
- # Public: Build a hash of attributes and camelize the keys for capsule
289
- #
290
- # Examples
291
- #
292
- # opportunity.to_capsule_json
293
- #
294
- # Returns a Hash
295
- def to_capsule_json
296
- serializer.serialize
297
- end
298
-
299
- # Public: Delete the opportunity in capsule
300
- #
301
- # Examples
302
- #
303
- # opportunity.destroy
304
- #
305
- # Return the CapsuleCRM::Opportunity
306
- def destroy
307
- self.id = nil if CapsuleCRM::Connection.delete("/api/opportunity/#{id}")
308
- self
309
- end
310
-
311
- private
312
-
313
- def serializer
314
- @serializer ||= CapsuleCRM::Serializer.new(self, excluded_keys: ['track_id'])
315
- end
316
-
317
- def create_record
318
- path = "/api/party/#{party_id}/opportunity"
319
- path += "?trackId=#{track_id}" if track_id
320
- self.attributes = CapsuleCRM::Connection.post(path, to_capsule_json)
321
- self
322
- end
323
-
324
- def update_record
325
- CapsuleCRM::Connection.put("/api/opportunity/#{id}", attributes)
326
- self
327
- end
328
87
  end
329
88
  end
@@ -7,8 +7,25 @@ module CapsuleCRM
7
7
  extend ActiveModel::Conversion
8
8
  include ActiveModel::Validations
9
9
 
10
- include CapsuleCRM::Collection
11
10
  include CapsuleCRM::Contactable
11
+ include CapsuleCRM::Persistence::Persistable
12
+ include CapsuleCRM::Persistence::Deletable
13
+ include CapsuleCRM::Querying::Configuration
14
+ include CapsuleCRM::Serializable
15
+
16
+ queryable_config do |config|
17
+ config.singular = :party
18
+ end
19
+
20
+ serializable_config do |config|
21
+ config.root = :organisation
22
+ config.additional_methods = [:contacts]
23
+ end
24
+
25
+ persistable_config do |config|
26
+ config.create = lambda { |org| "organisation" }
27
+ config.destroy = lambda { |org| "party/#{org.id}" }
28
+ end
12
29
 
13
30
  attribute :id, Integer
14
31
  attribute :name, String
@@ -16,9 +33,7 @@ module CapsuleCRM
16
33
 
17
34
  validates :name, presence: true
18
35
 
19
- has_many :people, class_name: 'CapsuleCRM::Person', source: :organization
20
- has_many :custom_fields, class_name: 'CapsuleCRM::CustomField',
21
- source: :party
36
+ has_many :people
22
37
 
23
38
  # Public: Get all people from Capsule. The list can be restricted
24
39
  # and/or paginated with various query parameters sent through the options
@@ -45,198 +60,5 @@ module CapsuleCRM
45
60
  CapsuleCRM::Party.all(options).
46
61
  delete_if { |item| !item.is_a?(CapsuleCRM::Organization) }
47
62
  end
48
-
49
- # Public: Get an organization by ID
50
- #
51
- # id - The Integer organization ID
52
- #
53
- # Examples
54
- #
55
- # CapsuleCRM::Organization.find(1)
56
- #
57
- # Returns a CapsuleCRM::Organization
58
- def self.find(id)
59
- new CapsuleCRM::Connection.get("/api/party/#{id}")['organisation']
60
- end
61
-
62
- # Public: Create a new organization in capsulecrm
63
- #
64
- # attributes - The Hash of organization attributes (default: {}):
65
- # :name - The String organization name
66
- # :about - The String information about the organization
67
- #
68
- # Examples
69
- #
70
- # CapsuleCRM::Organization.create(name: 'Google Inc')
71
- #
72
- # Returns a CapsuleCRM::Organization
73
- def self.create(attributes = {})
74
- new(attributes).tap(&:save)
75
- end
76
-
77
- # Public: Create a new organization in capsulecrm and raise a
78
- # CapsuleCRM::Errors::InvalidRecord error if not possible
79
- #
80
- # attributes - The Hash of organization attributes (default: {}):
81
- # :name - The String organization name
82
- # :about - The String information about the organization
83
- #
84
- # Examples
85
- #
86
- # CapsuleCRM::Organization.create!(name: 'Google Inc')
87
- #
88
- # Returns a CapsuleCRM
89
- def self.create!(attributes = {})
90
- new(attributes).tap(&:save!)
91
- end
92
-
93
- # Public: If the organization already exists in capsule then update them,
94
- # otherwise create a new organization
95
- #
96
- # Examples
97
- #
98
- # organization = CapsuleCRM::Organization.new(name: 'Google Inc')
99
- # organization.save
100
- #
101
- # organization = CapsuleCRM::Organization.find(1)
102
- # organization.name = 'Apple'
103
- # organization.save
104
- #
105
- # Returns a CapsuleCRM::Organization
106
- def save
107
- if valid?
108
- new_record? ? create_record : update_record
109
- else
110
- false
111
- end
112
- end
113
-
114
- # Public: If the organization already exists in capsule then update them,
115
- # otherwise create a new organization. If the organization is not valid
116
- # then a CapsuleCRM::Errors::RecordInvalid exception is raised
117
- #
118
- # Examples
119
- #
120
- # organization = CapsuleCRM::Organization.new(name: 'Google Inc')
121
- # organization.save!
122
- #
123
- # organization = CapsuleCRM::Organization.find(1)
124
- # organization.name = 'Apple'
125
- # organization.save!
126
- #
127
- # organization = CapsuleCRM::Organization.new
128
- # organization.save!
129
- # => CapsuleCRM::Errors::InvalidRecord
130
- #
131
- # Returns a CapsuleCRM::Organization
132
- def save!
133
- if valid?
134
- new_record? ? create_record : update_record
135
- else
136
- raise CapsuleCRM::Errors::RecordInvalid.new(self)
137
- end
138
- end
139
-
140
- # Public: Update the organization in capsule
141
- #
142
- # attributes - The Hash of organization attributes (default: {}):
143
- # :name - The String organization name
144
- # :about - The String information about the organization
145
- #
146
- # Examples
147
- #
148
- # organization = CapsuleCRM::Organization.find(1)
149
- # organization.update_attributes name: 'Google Inc'
150
- # => true
151
- #
152
- # organization.update_attributes {}
153
- # => false
154
- #
155
- # Returns a CapsuleCRM::Organization
156
- def update_attributes(attributes = {})
157
- self.attributes = attributes
158
- save
159
- end
160
-
161
- # Public: Update the organization in capsule. If the organization is not
162
- # valid then a CapsuleCRM::Errors::RecordInvalid exception will be raised
163
- #
164
- # attributes - The Hash of organization attributes (default: {}):
165
- # :name - The String organization name
166
- # :about - The String information about the organization
167
- #
168
- # Examples
169
- #
170
- # organization = CapsuleCRM::Organization.find(1)
171
- # organization.update_attributes! name: 'Microsoft'
172
- # => true
173
- #
174
- # organization = CapsuleCRM::Organization.find(1)
175
- # organization.update_attributes!
176
- # => CapsuleCRM::Errors::RecordInvalid
177
- #
178
- # Returns a CapsuleCRM::Organization
179
- def update_attributes!(attributes = {})
180
- self.attributes = attributes
181
- save!
182
- end
183
-
184
- # Public: Determine whether this CapsuleCRM::Organization is a new record
185
- # or not
186
- #
187
- # Returns a Boolean
188
- def new_record?
189
- !id
190
- end
191
-
192
- # Public: Determine whether or not this CapsuleCRM::Organization has
193
- # already been persisted to capsulecrm
194
- #
195
- # Returns a Boolean
196
- def persisted?
197
- !new_record?
198
- end
199
-
200
- # Public: Build a hash of attributes and merge in the attributes for the
201
- # contact information
202
- #
203
- # Examples
204
- #
205
- # organization.to_capsule_json
206
- #
207
- # Returns a Hash
208
- def to_capsule_json
209
- serializer.serialize
210
- end
211
-
212
- # Public: Delete the organization in capsule
213
- #
214
- # Examples
215
- #
216
- # organization.destroy
217
- #
218
- # Return the CapsuleCRM::Organization
219
- def destroy
220
- self.id = nil if CapsuleCRM::Connection.delete("/api/party/#{id}")
221
- self
222
- end
223
-
224
- private
225
-
226
- def serializer
227
- @serializer ||= CapsuleCRM::Serializer.
228
- new(self, root: :organisation, additional_methods: [:contacts])
229
- end
230
-
231
- def create_record
232
- self.attributes = CapsuleCRM::Connection.post(
233
- '/api/organisation', to_capsule_json
234
- )
235
- end
236
-
237
- def update_record
238
- CapsuleCRM::Connection.put("/api/organisation/#{id}", to_capsule_json)
239
- self
240
- end
241
63
  end
242
64
  end
@@ -1,40 +1,27 @@
1
1
  class CapsuleCRM::Party
2
2
  include Virtus
3
3
 
4
- include CapsuleCRM::Attributes
5
- include CapsuleCRM::Taggable
6
4
  include CapsuleCRM::Associations
5
+ include CapsuleCRM::Querying::Findable
6
+ include CapsuleCRM::Serializable
7
+ include CapsuleCRM::Taggable
7
8
 
8
- has_many :histories, class_name: 'CapsuleCRM::History', source: :party
9
- has_many :tasks, class_name: 'CapsuleCRM::Task', source: :party
10
-
11
- def self.all(options = {})
12
- init_collection(
13
- CapsuleCRM::Connection.get('/api/party', options)['parties']
14
- )
9
+ serializable_config do |config|
10
+ config.root = [:organisation, :person]
15
11
  end
16
12
 
17
- def self.find(id)
18
- attributes = CapsuleCRM::Connection.get("/api/party/#{id}")
19
- party_classes[attributes.keys.first].constantize.new(
20
- attributes[attributes.keys.first]
21
- )
13
+ queryable_config do |config|
14
+ config.plural = :party
22
15
  end
23
16
 
24
- private
17
+ has_many :histories
18
+ has_many :tasks
19
+ has_many :custom_fields, embedded: true
20
+ has_many :cases
25
21
 
26
- def self.init_collection(collection)
27
- CapsuleCRM::ResultsProxy.new(
28
- collection.map do |key, value|
29
- next unless %w(organisation person).include?(key)
30
- [collection[key]].flatten.map do |attrs|
31
- party_classes[key].constantize.new(attrs)
32
- end.flatten
33
- end.flatten
34
- )
35
- end
22
+ private
36
23
 
37
- def self.party_classes
24
+ def self.child_classes
38
25
  { person: 'CapsuleCRM::Person', organisation: 'CapsuleCRM::Organization' }.
39
26
  stringify_keys
40
27
  end
@@ -0,0 +1,25 @@
1
+ module CapsuleCRM
2
+ module Persistence
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+
6
+ def self.included(base)
7
+ base.send :class_attribute, :connection_options
8
+
9
+ klass_name = base.to_s.demodulize.downcase
10
+
11
+ base.connection_options = OpenStruct.new(
12
+ create: lambda { |object| "#{klass_name.pluralize}" },
13
+ update: lambda { |object| "#{klass_name.singularize}/#{object.id}" },
14
+ destroy: lambda { |object| "#{klass_name.singularize}/#{object.id}" }
15
+ )
16
+ end
17
+
18
+ module ClassMethods
19
+ def persistable_config
20
+ yield(connection_options)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ module CapsuleCRM
2
+ module Persistence
3
+ module Deletable
4
+ def destroy
5
+ self.id = nil if CapsuleCRM::Connection.delete(build_destroy_path)
6
+ self
7
+ end
8
+
9
+ def build_destroy_path
10
+ "/api/#{self.class.connection_options.destroy.call(self)}"
11
+ end
12
+ end
13
+ end
14
+ end