Skip to content

Commit

Permalink
Work out the custom field parent
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed Jan 11, 2014
1 parent a45cb69 commit 3422f6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
12 changes: 8 additions & 4 deletions lib/capsule_crm/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CustomField
validates :label, presence: true

belongs_to :party, class_name: 'CapsuleCRM::Party'
belongs_to :opportunity, class_name: 'CapsuleCRM::Opportunity'
belongs_to :kase, class_name: 'CapsuleCRM::Case'

def self._for_party(party_id)
init_collection(
Expand Down Expand Up @@ -65,12 +67,14 @@ def save!
def destroy
end

def to_capsule_json
{ 'customFields' => serializer.serialize }
end

private

def parent
self.class.belongs_to_associations.each do |association|
return self.send(assocation) unless self.send(association).blank?
end
end

def update_record
CapsuleCRM::Connection.post(
"/api/party/#{party.id}/customfields", to_capsule_json
Expand Down
19 changes: 18 additions & 1 deletion lib/capsule_crm/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@ def initialize(object, options = {})
end

def serialize
@serialized ||= { root => build_attributes_hash }.stringify_keys
@serialized ||=
if object.is_a?(Array)
serialize_collection
else
serialize_single
end
end

def root
@root ||= options[:root] ||
object.class.to_s.demodulize.downcase.singularize.camelize(:lower)
end

def collection_root
@collection_root ||= options[:collection_root] || root.pluralize
end

private

def serialize_single
{ root => build_attributes_hash }.stringify_keys
end

def serialize_collection
{ collection_root => serialize_single }.stringify_keys
end

def additional_methods
@additional_methods ||= options[:additional_methods] || []
end
Expand Down
6 changes: 1 addition & 5 deletions spec/lib/capsule_crm/custom_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@
end

it 'should have a root element of "customField"' do
expect(subject.keys.first).to eql('customFields')
end

it 'should have an element named "customField"' do
expect(subject['customFields'].keys.first).to eql('customField')
expect(subject.keys.first).to eql('customField')
end
end
end

0 comments on commit 3422f6e

Please sign in to comment.