capsule_crm 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1ae5fc726cd57ca7e7026cc85e30bab83221431
4
- data.tar.gz: 98cbdf209e74f7ff4a0d8b5cb8cf6554773432ed
3
+ metadata.gz: 895602f68388ac5ca0d367ee33568425ea6b9b94
4
+ data.tar.gz: 426048ab8c437b47607fe449af53455ee22a6c14
5
5
  SHA512:
6
- metadata.gz: 77e2bb19b4a89d0a41fbc823243448d3d341863c9bd0b5c1918e281c5f6c458f1c97b14c85ee427715a8cdb092224be9522bfdd81c1469da2b3b43a90d7777c9
7
- data.tar.gz: 2151b5e5d3c3e8ecd2305f8c89e0ac3983779f705819193d9572f7b24fcb361c183c9ce1284218a8d627b0d162c6e6fb8fcb5a54a30c039f745400d69fa585f6
6
+ metadata.gz: 2c0c4236c700a3014c6ed84ed1122d4784047016dfc69b2f8431cac9b23bea80832f17858562e1adf86c0eaf8c3367e0fa21d365187197acce034c91c171400f
7
+ data.tar.gz: bab4f5d64be4d3bf1f75be0de9d98a1a294307b1fae5af38ee70bee1f26d2ba50c64e70508808d523713c61acb3b94e5e5cdff57c3949a745584aea8c18f4465
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ - Refactored associations to allow classes to reflect on themselves in order to
6
+ allow serialization to happen in one place
7
+ - Created a serializer to deal with all converting all objects into capsule crm
8
+ compliant hashes
9
+ - Created CapsuleCRM::Normalizer to deal with converting capsule crm hashes into
10
+ ActiveModel compliant hashes
11
+ - Extracted querying logic into modules
12
+ - Extracted persistence logic into modules
13
+ - Extracted deleting logic into a module
14
+ - Added CapsuleCRM::Associations::BelongsToFinder to deal with querying the
15
+ belongs to side of associations
16
+ - Added CapsuleCRM::CustomFieldDefinitions
17
+
3
18
  ## 1.2.0
4
19
 
5
20
  - Refactor associations. Now all classes know about their associations and
@@ -1,11 +1,12 @@
1
1
  module CapsuleCRM
2
2
  class Address
3
3
  include Virtus
4
-
4
+ include CapsuleCRM::Serializable
5
5
  extend ActiveModel::Naming
6
- include ActiveModel::Serializers::JSON
7
6
 
8
- include CapsuleCRM::CapsuleJsonable
7
+ serializable_config do |config|
8
+ config.include_root = false
9
+ end
9
10
 
10
11
  attribute :type
11
12
  attribute :street
@@ -1,4 +1,5 @@
1
1
  require_relative 'belongs_to_association'
2
+ require_relative 'belongs_to_finder'
2
3
 
3
4
  module CapsuleCRM
4
5
  module Associations
@@ -41,7 +42,7 @@ module CapsuleCRM
41
42
 
42
43
  (class << self; self; end).instance_eval do
43
44
  define_method "_for_#{association_name}" do |id|
44
- raise NotImplementedError.new("_for_#{association_name} needs to be implemented")
45
+ CapsuleCRM::Associations::BelongsToFinder.new(association).call(id)
45
46
  end
46
47
  end
47
48
 
@@ -25,13 +25,21 @@ module CapsuleCRM
25
25
  @options = options
26
26
  end
27
27
 
28
+ def inverse
29
+ @inverse ||=
30
+ target_klass.has_many_associations.find do |name, association|
31
+ association.source == association_name &&
32
+ association.target_klass == defined_on
33
+ end.try(:last) if target_klass.respond_to?(:has_many_associations)
34
+ end
35
+
28
36
  # Public: Build the foreign key column name. If a foreign key name was
29
37
  # supplied in the options during initialization, then that is returned.
30
38
  # Otherwise it is inferred from the association name
31
39
  #
32
40
  # Returns a String foreign key name
33
41
  def foreign_key
34
- @foreign_key ||= options[:foreign_key] || "#{association_name}_id"
42
+ @foreign_key ||= options[:foreign_key] || infer_foreign_key
35
43
  end
36
44
 
37
45
  # Public: Find the parent object of the supplied object
@@ -72,8 +80,17 @@ module CapsuleCRM
72
80
 
73
81
  private
74
82
 
83
+ def infer_foreign_key
84
+ "#{association_name}_id"
85
+ end
86
+
75
87
  def target_klass
76
- @target_klass ||= options[:class_name].constantize
88
+ @target_klass ||=
89
+ (options[:class_name] || infer_target_klass).constantize
90
+ end
91
+
92
+ def infer_target_klass
93
+ "CapsuleCRM::#{association_name.to_s.camelize}"
77
94
  end
78
95
  end
79
96
  end
@@ -0,0 +1,46 @@
1
+ module CapsuleCRM
2
+ module Associations
3
+ class BelongsToFinder
4
+ attr_reader :association
5
+ attr_writer :singular, :plural, :normalizer
6
+
7
+ def initialize(association)
8
+ @association = association
9
+ end
10
+
11
+ def call(id)
12
+ id ? find(id) : []
13
+ end
14
+
15
+ private
16
+
17
+ def inverse
18
+ @inverse ||= association.inverse
19
+ end
20
+
21
+ def singular
22
+ @singular ||= inverse.defined_on.queryable_options.singular
23
+ end
24
+
25
+ def plural
26
+ @plural ||= association.defined_on.queryable_options.plural
27
+ end
28
+
29
+ def find(id)
30
+ normalizer.normalize_collection get(path(id)) if inverse
31
+ end
32
+
33
+ def get(path_string)
34
+ CapsuleCRM::Connection.get(path_string)
35
+ end
36
+
37
+ def path(id)
38
+ "/api/#{singular}/#{id}/#{plural}"
39
+ end
40
+
41
+ def normalizer
42
+ @normalizer ||= CapsuleCRM::Normalizer.new(association.defined_on)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -38,7 +38,8 @@ module CapsuleCRM
38
38
  # Returns a CapsuleCRM::Associations::HasManyProxy
39
39
  def proxy(parent, collection = nil)
40
40
  CapsuleCRM::Associations::HasManyProxy.new(
41
- parent, target_klass, build_target(parent, collection), source
41
+ parent, target_klass, build_target(parent, collection), source,
42
+ embedded
42
43
  )
43
44
  end
44
45
 
@@ -49,8 +50,29 @@ module CapsuleCRM
49
50
  :has_many
50
51
  end
51
52
 
53
+ def embedded
54
+ @embedded ||= !!options[:embedded]
55
+ end
56
+
57
+ def source
58
+ @source ||= options[:source] || infer_source
59
+ end
60
+
61
+ def target_klass
62
+ @target_klass ||=
63
+ (options[:class_name] || infer_target_klass).constantize
64
+ end
65
+
52
66
  private
53
67
 
68
+ def infer_source
69
+ defined_on.to_s.demodulize.downcase.singularize.to_sym
70
+ end
71
+
72
+ def infer_target_klass
73
+ "CapsuleCRM::#{association_name.to_s.singularize.camelize}"
74
+ end
75
+
54
76
  def build_target(parent, collection)
55
77
  collection.nil? ? target(parent) : collection_to_array(collection)
56
78
  end
@@ -63,18 +85,10 @@ module CapsuleCRM
63
85
  end
64
86
  end
65
87
 
66
- def target_klass
67
- @target_klass ||= options[:class_name].constantize
68
- end
69
-
70
88
  def target(parent)
71
89
  target_klass.
72
90
  send("_for_#{parent.class.to_s.demodulize.downcase}", parent.id)
73
91
  end
74
-
75
- def source
76
- @source ||= options[:source]
77
- end
78
92
  end
79
93
  end
80
94
  end
@@ -2,11 +2,12 @@ module CapsuleCRM
2
2
  module Associations
3
3
  class HasManyProxy < BasicObject
4
4
 
5
- def initialize(parent, target_klass, target, source)
5
+ def initialize(parent, target_klass, target, source, embedded)
6
6
  @target = target
7
7
  @parent = parent
8
8
  @target_klass = target_klass
9
9
  @source = source
10
+ @embedded = embedded
10
11
  end
11
12
 
12
13
  def method_missing(name, *args, &block)
@@ -21,9 +22,17 @@ module CapsuleCRM
21
22
  end
22
23
 
23
24
  def create(attributes = {})
24
- build(attributes).tap do |record|
25
- record_not_saved(record) unless parent.persisted?
26
- end.save
25
+ record = build(attributes).tap do |r|
26
+ record_not_saved(r) unless parent.persisted?
27
+ end
28
+ embedded? ? save : record.save
29
+ end
30
+
31
+ def create!(attributes = {})
32
+ record = build(attributes).tap do |r|
33
+ record_not_saved(r) unless parent.persisted?
34
+ end
35
+ embedded? ? save! : record.save!
27
36
  end
28
37
 
29
38
  def tap
@@ -31,8 +40,33 @@ module CapsuleCRM
31
40
  self
32
41
  end
33
42
 
43
+ def to_capsule_json(root = nil)
44
+ { root => serializer.serialize_collection(target_klass, target) }
45
+ end
46
+
34
47
  private
35
48
 
49
+ def serializer
50
+ @serializer ||= ::CapsuleCRM::Serializer
51
+ end
52
+
53
+ def save
54
+ json = to_capsule_json(target_klass.serializable_options.collection_root)
55
+ path = [
56
+ '/api', parent.class.queryable_options.singular, parent.id,
57
+ target_klass.queryable_options.plural
58
+ ].join('/')
59
+ ::CapsuleCRM::Connection.put(path, json)
60
+ end
61
+
62
+ def save!
63
+ save
64
+ end
65
+
66
+ def embedded?
67
+ @embedded
68
+ end
69
+
36
70
  def record_not_saved(record)
37
71
  raise ::CapsuleCRM::Errors::RecordNotSaved.new(record)
38
72
  end
@@ -6,10 +6,34 @@ module CapsuleCRM
6
6
  include ActiveModel::Conversion
7
7
  include ActiveModel::Validations
8
8
 
9
- include CapsuleCRM::Collection
10
9
  include CapsuleCRM::Associations
10
+ include CapsuleCRM::Persistence::Persistable
11
+ include CapsuleCRM::Persistence::Deletable
12
+ include CapsuleCRM::Querying::Findable
13
+ include CapsuleCRM::Serializable
11
14
  include CapsuleCRM::Taggable
12
15
 
16
+ serializable_config do |config|
17
+ config.collection_root = :kases
18
+ config.root = :kase
19
+ config.excluded_keys = [:track_id]
20
+ end
21
+
22
+ queryable_config do |config|
23
+ config.plural = :kase
24
+ config.singular = :kase
25
+ end
26
+
27
+ persistable_config do |config|
28
+ config.create = lambda do |kase|
29
+ path = "party/#{kase.party.try(:id)}/kase"
30
+ path += "?trackId=#{kase.track.id}" if kase.track
31
+ path
32
+ end
33
+ config.update = lambda { |kase| "kase/#{kase.id}" }
34
+ config.destroy = lambda { |kase| "kase/#{kase.id}" }
35
+ end
36
+
13
37
  attribute :id, Integer
14
38
  attribute :name, String
15
39
  attribute :description, String
@@ -21,247 +45,20 @@ module CapsuleCRM
21
45
  validates :name, presence: true
22
46
  validates :party, presence: true
23
47
 
24
- belongs_to :party, class_name: 'CapsuleCRM::Party'
25
- belongs_to :track, class_name: 'CapsuleCRM::Track'
26
-
27
- has_many :tasks, class_name: 'CapsuleCRM::Task', source: :case
28
-
29
- # Public: Search and retrieve all cases in CapsuleCRM
30
- #
31
- # options - the Hash of query options (default: {}):
32
- # :tag - the String tag to search for
33
- # :lastmodified - the Date after which the case was last
34
- # modified by
35
- # :start - the Integer index of the first record to
36
- # return.
37
- # :limit - the Integer number of records to return
38
- #
39
- # Examples
40
- #
41
- # CapsuleCRM::Case.all(tag: 'Some Tag')
42
- #
43
- # Returns a CapsuleCRM::ResultsProxy of CapsuleCRM::Case objects
44
- def self.all(options = {})
45
- init_collection(
46
- CapsuleCRM::Connection.get('/api/kase', options)['kases']['kase']
47
- )
48
- end
49
-
50
- # Public: Search for a single case by ID
51
- #
52
- # id - the Integer ID of the record to search for
53
- #
54
- # Examples
55
- #
56
- # CapsuleCRM::Case.find(1)
57
- #
58
- # Returns a CapsuleCRM::Case object
59
- def self.find(id)
60
- new CapsuleCRM::Connection.get("/api/kase/#{id}")['kase']
61
- end
62
-
63
- # Public: Create a CapsuleCRM::Case
64
- #
65
- # attributes - the Hash of attributes (default: {}):
66
- # :name - the String name of the case (required)
67
- # :description - the String description
68
- # :status - the String status (OPEN or CLOSED), default
69
- # is OPEN
70
- # :owner - the String username of the owner
71
- # :close_date - the Date when the case was/will be closed.
72
- # Ignored if the status is set to CLOSED
73
- #
74
- # Examples
75
- #
76
- # CapsuleCRM::Case.create name: "Matt's test case"
77
- #
78
- # Returns a CapsuleCRM::Case
79
- def self.create(attributes = {})
80
- new(attributes).tap(&:save)
81
- end
82
-
83
- # Public: Create a CapsuleCRM::Case or raise a error
84
- #
85
- # attributes - the Hash of attributes (default: {}):
86
- # :name - the String name of the case (required)
87
- # :description - the String description
88
- # :status - the String status (OPEN or CLOSED), default
89
- # is OPEN
90
- # :owner - the String username of the owner
91
- # :close_date - the Date when the case was/will be closed.
92
- # Ignored if the status is set to CLOSED
93
- #
94
- # Examples
95
- #
96
- # CapsuleCRM::Case.create! name: "Matt's test case"
97
- # => CapsuleCRM::Case object
98
- #
99
- # CapsuleCRM::Case.create! name: nil
100
- # => CapsuleCRM::Errors::RecordInvalid
101
- #
102
- # Returns a CapsuleCRM::Case
103
- def self.create!(attributes = {})
104
- new(attributes).tap(&:save!)
105
- end
106
-
107
- # Public: Updates an existing CapsuleCRM::Case with the supplied attributes
108
- #
109
- # attributes - the Hash of attributes (default: {}):
110
- # :name - the String name of the case (required)
111
- # :description - the String description
112
- # :status - the String status (OPEN or CLOSED), default
113
- # is OPEN
114
- # :owner - the String username of the owner
115
- # :close_date - the Date when the case was/will be closed.
116
- # Ignored if the status is set to CLOSED
117
- #
118
- # Examples
119
- #
120
- # kase = CapsuleCRM::Case.find(1)
121
- # kase.update_attributes name: 'A new name'
122
- #
123
- # Returns a CapsuleCRM::Case
124
- def update_attributes(attributes = {})
125
- self.attributes = attributes
126
- save
127
- end
128
-
129
- # Public: Updates an existing CapsuleCRM::Case with the supplied attributes
130
- # or raises an error
131
- #
132
- # attributes - the Hash of attributes (default: {}):
133
- # :name - the String name of the case (required)
134
- # :description - the String description
135
- # :status - the String status (OPEN or CLOSED), default
136
- # is OPEN
137
- # :owner - the String username of the owner
138
- # :close_date - the Date when the case was/will be closed.
139
- # Ignored if the status is set to CLOSED
140
- #
141
- # Examples
142
- #
143
- # kase = CapsuleCRM::Case.find(1)
144
- # kase.update_attributes! name: 'A new name'
145
- # => CapsuleCRM::Case object
146
- #
147
- # kase.update_attributes! name: nil
148
- # => CapsuleCRM::Errors::RecordInvalid
149
- #
150
- # Returns a CapsuleCRM::Case
151
- def update_attributes!(attributes = {})
152
- self.attributes = attributes
153
- save!
154
- end
155
-
156
- # Public: Saves this CapsuleCRM::Case
157
- #
158
- # Examples:
159
- #
160
- # kase = CapsuleCRM::Case.new(
161
- # name: 'Case Name', party: person_or_organization
162
- # )
163
- # kase.save
164
- # => CapsuleCRM::Case object
165
- #
166
- # Returns the CapsuleCRM::Case or false
167
- def save
168
- if valid?
169
- new_record? ? create_record : update_record
170
- else
171
- false
172
- end
173
- end
174
-
175
- # Public: Saves this CapsuleCRM::Case or raises an error
176
- #
177
- # Examples
178
- #
179
- # kase = CapsuleCRM::Case.find(1)
180
- # kase.name = 'Changed name'
181
- # kase.save!
182
- # => CapsuleCRM::Case object
183
- #
184
- # kase.name = nil
185
- # kase.save!
186
- # => CapsuleCRM::Errors::RecordInvalid
187
- #
188
- # Returns a CapsuleCRM::Case
189
- def save!
190
- if valid?
191
- new_record? ? create_record : update_record
192
- else
193
- raise CapsuleCRM::Errors::RecordInvalid.new(self)
194
- end
195
- end
48
+ belongs_to :party
49
+ belongs_to :track
196
50
 
197
- # Public: Deletes this CapsuleCRM::Case from CapsuleCRM
198
- #
199
- # Examples
200
- #
201
- # CapsuleCRM::Case.find(1).destroy
202
- #
203
- # Returns the CapsuleCRM::Case object
204
- def destroy
205
- self.id = nil if CapsuleCRM::Connection.delete("/api/kase/#{id}")
206
- self
207
- end
208
-
209
- # Public: Is this case a new record?
210
- #
211
- # Examples
212
- #
213
- # CapsuleCRM::Case.find(1).new_record?
214
- # => false
215
- #
216
- # CapsuleCRM::Case.new.new_record?
217
- # => true
218
- #
219
- # Returns a Boolean
220
- def new_record?
221
- !id
222
- end
51
+ has_many :tasks
52
+ has_many :histories
53
+ has_many :custom_fields, embedded: true
223
54
 
224
- # Public: Is this case persisted to CapsuleCRM?
225
- #
226
- # Examples
227
- #
228
- # CapsuleCRM::Case.find(1).persisted?
229
- # => true
230
- #
231
- # CapsuleCRM::Case.new.persisted?
232
- # => false
233
- #
234
- # Returns a Boolean
235
- def persisted?
236
- !new_record?
237
- end
238
-
239
- def to_capsule_json
240
- serializer.serialize
55
+ class << self
56
+ alias :_for_organization :_for_party
57
+ alias :_for_person :_for_party
241
58
  end
242
59
 
243
60
  def self._for_track(track)
244
61
  raise NotImplementedError.new("There is no way to find cases by trackId in the Capsule API right now")
245
62
  end
246
-
247
- private
248
-
249
- def serializer
250
- @serializer ||= CapsuleCRM::Serializer.
251
- new(self, excluded_keys: ['track_id'], root: :kaze)
252
- end
253
-
254
- def create_record
255
- path = "/api/party/#{party_id}/kase"
256
- path += "?trackId=#{track_id}" if track_id
257
- self.attributes = CapsuleCRM::Connection.post(
258
- path, to_capsule_json
259
- )
260
- self
261
- end
262
-
263
- def update_record
264
- CapsuleCRM::Connection.put("/api/kase/#{id}", to_capsule_json)
265
- end
266
63
  end
267
64
  end
@@ -1,6 +1,6 @@
1
1
  class CapsuleCRM::Configuration
2
2
 
3
- attr_accessor :api_token, :logger, :subdomain
3
+ attr_accessor :api_token, :logger, :subdomain, :perform_logging
4
4
 
5
5
  def logger
6
6
  @logger || Logger.new(STDOUT)
@@ -27,8 +27,11 @@ module CapsuleCRM
27
27
  private
28
28
 
29
29
  def self.request(method, path, params)
30
+ CapsuleCRM.log "CapsuleCRM: #{method.upcase} #{path} with #{params}"
30
31
  faraday.send(method, path, params) do |req|
31
32
  req.headers.update default_request_headers
33
+ end.tap do |response|
34
+ CapsuleCRM.log "CapsuleCRM Response: #{response.body}"
32
35
  end
33
36
  end
34
37
 
@@ -17,7 +17,7 @@ module CapsuleCRM
17
17
  if contacts.is_a?(Hash)
18
18
  contacts = CapsuleCRM::Contacts.new(contacts.symbolize_keys)
19
19
  end
20
- @contacts = contacts
20
+ @contacts = contacts unless contacts.blank?
21
21
  end
22
22
 
23
23
  def contacts
@@ -2,20 +2,14 @@ module CapsuleCRM
2
2
  class Country
3
3
  include Virtus
4
4
 
5
- attribute :name
5
+ include CapsuleCRM::Serializable
6
+ include CapsuleCRM::Querying::Configuration
7
+ include CapsuleCRM::Querying::FindAll
6
8
 
7
- # Public: Retrieve a list of countries from Capsule
8
- #
9
- # Examples
10
- #
11
- # CapsuleCRM::Country.all
12
- #
13
- # Returns an Array of CapsuleCRM::Country objects
14
- def self.all
15
- CapsuleCRM::Connection.
16
- get('/api/countries')['countries']['country'].map do |country_name|
17
- new name: country_name
18
- end
9
+ serializable_config do |config|
10
+ config.attribute_to_assign = :name
19
11
  end
12
+
13
+ attribute :name
20
14
  end
21
15
  end
@@ -6,20 +6,14 @@ module CapsuleCRM
6
6
  include ActiveModel::Conversion
7
7
  include ActiveModel::Validations
8
8
 
9
- attribute :code, String
9
+ include CapsuleCRM::Querying::Configuration
10
+ include CapsuleCRM::Querying::FindAll
11
+ include CapsuleCRM::Serializable
10
12
 
11
- # Public: Retrieve a list of all currencies in Capsule
12
- #
13
- # Examples
14
- #
15
- # CapsuleCRM::Currency.all
16
- #
17
- # Returns an Array of CapsuleCRM::Currency objects
18
- def self.all
19
- CapsuleCRM::Connection.
20
- get('/api/currencies')['currencies']['currency'].map do |currency_code|
21
- new code: currency_code
22
- end
13
+ serializable_config do |config|
14
+ config.attribute_to_assign = :code
23
15
  end
16
+
17
+ attribute :code, String
24
18
  end
25
19
  end