Skip to content

Commit

Permalink
Working on serializer improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed Jan 10, 2014
1 parent 632daa0 commit a45cb69
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 96 deletions.
3 changes: 2 additions & 1 deletion lib/capsule_crm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
require 'capsule_crm/collection'
require 'capsule_crm/associations'
require 'capsule_crm/address'
require 'capsule_crm/case'
require 'capsule_crm/connection'
require 'capsule_crm/serializer'
require 'capsule_crm/serializable'

require 'capsule_crm/attachment'
require 'capsule_crm/case'
require 'capsule_crm/country'
require 'capsule_crm/currency'
require 'capsule_crm/email'
Expand Down
16 changes: 6 additions & 10 deletions lib/capsule_crm/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ class Case
include ActiveModel::Conversion
include ActiveModel::Validations

include CapsuleCRM::Collection
include CapsuleCRM::Associations
include CapsuleCRM::Collection
include CapsuleCRM::Serializable
include CapsuleCRM::Taggable

self.serializable_options = {
root: :kase, excluded_keys: [:track_id]
}

attribute :id, Integer
attribute :name, String
attribute :description, String
Expand Down Expand Up @@ -236,21 +241,12 @@ def persisted?
!new_record?
end

def to_capsule_json
serializer.serialize
end

def self._for_track(track)
raise NotImplementedError.new("There is no way to find cases by trackId in the Capsule API right now")
end

private

def serializer
@serializer ||= CapsuleCRM::Serializer.
new(self, excluded_keys: ['track_id'], root: :kaze)
end

def create_record
path = "/api/party/#{party_id}/kase"
path += "?trackId=#{track_id}" if track_id
Expand Down
7 changes: 3 additions & 4 deletions lib/capsule_crm/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class CustomField
include CapsuleCRM::Associations
include CapsuleCRM::Attributes
include CapsuleCRM::Collection
include CapsuleCRM::Serializable

self.serializable_options = { root: 'customField' }

attribute :id, Integer
attribute :label, String
Expand Down Expand Up @@ -68,10 +71,6 @@ def to_capsule_json

private

def serializer
@serializer ||= CapsuleCRM::Serializer.new(self, root: 'customField')
end

def update_record
CapsuleCRM::Connection.post(
"/api/party/#{party.id}/customfields", to_capsule_json
Expand Down
18 changes: 3 additions & 15 deletions lib/capsule_crm/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class History
include CapsuleCRM::Associations
include CapsuleCRM::Attributes
include CapsuleCRM::Collection
include CapsuleCRM::Serializable

self.serializable_options = { root: :historyItem }

attribute :id, Integer
attribute :type, String
Expand Down Expand Up @@ -263,23 +266,8 @@ def persisted?
!new_record?
end

# Public: Generate the attributes hash to send to capsule
#
# Examples
#
# CapsuleCRM::History.find(1).to_capsule_json
#
# Returns a Hash of attributes
def to_capsule_json
serializer.serialize
end

private

def serializer
@serializer ||= CapsuleCRM::Serializer.new(self, root: :historyItem)
end

def belongs_to_required?
party.blank? && kase.blank? && opportunity.blank?
end
Expand Down
18 changes: 3 additions & 15 deletions lib/capsule_crm/opportunity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Opportunity
include CapsuleCRM::Associations
include CapsuleCRM::Attributes
include CapsuleCRM::Collection
include CapsuleCRM::Serializable

self.serializable_options = { excluded_keys: [:track_id] }

attribute :id, Integer
attribute :name, String
Expand Down Expand Up @@ -284,18 +287,7 @@ def update_attributes!(attributes = {})
self.attributes = attributes
save!
end

# Public: Build a hash of attributes and camelize the keys for capsule
#
# Examples
#
# opportunity.to_capsule_json
#
# Returns a Hash
def to_capsule_json
serializer.serialize
end

# Public: Delete the opportunity in capsule
#
# Examples
Expand All @@ -310,10 +302,6 @@ def destroy

private

def serializer
@serializer ||= CapsuleCRM::Serializer.new(self, excluded_keys: ['track_id'])
end

def create_record
path = "/api/party/#{party_id}/opportunity"
path += "?trackId=#{track_id}" if track_id
Expand Down
22 changes: 5 additions & 17 deletions lib/capsule_crm/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Organization < CapsuleCRM::Party

include CapsuleCRM::Collection
include CapsuleCRM::Contactable
include CapsuleCRM::Serializable

self.serializable_options = {
root: :organisation, additional_methods: [:contacts]
}

attribute :id, Integer
attribute :name, String
Expand Down Expand Up @@ -196,19 +201,7 @@ def new_record?
def persisted?
!new_record?
end

# Public: Build a hash of attributes and merge in the attributes for the
# contact information
#
# Examples
#
# organization.to_capsule_json
#
# Returns a Hash
def to_capsule_json
serializer.serialize
end

# Public: Delete the organization in capsule
#
# Examples
Expand All @@ -223,11 +216,6 @@ def destroy

private

def serializer
@serializer ||= CapsuleCRM::Serializer.
new(self, root: :organisation, additional_methods: [:contacts])
end

def create_record
self.attributes = CapsuleCRM::Connection.post(
'/api/organisation', to_capsule_json
Expand Down
22 changes: 5 additions & 17 deletions lib/capsule_crm/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ module CapsuleCRM
class Person < CapsuleCRM::Party
include CapsuleCRM::Collection
include CapsuleCRM::Contactable
include CapsuleCRM::Serializable

extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations

self.serializable_options = {
additional_methods: [:contacts]
}

attribute :id, Integer
attribute :title
attribute :first_name
Expand Down Expand Up @@ -210,25 +215,8 @@ def persisted?
!new_record?
end

# Public: Build a hash of attributes and merge in the attributes for the
# contact information
#
# Examples
#
# person.to_capsule_json
#
# Returns a Hash
def to_capsule_json
serializer.serialize
end

private

def serializer
@serializer ||= CapsuleCRM::Serializer.
new(self, additional_methods: [:contacts])
end

def create_record
self.attributes = CapsuleCRM::Connection.post(
'/api/person', to_capsule_json
Expand Down
20 changes: 20 additions & 0 deletions lib/capsule_crm/serializable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module CapsuleCRM
module Serializable
extend ActiveSupport::Concern

included do
class_attribute :serializable_options
self.serializable_options = {}
end

def to_capsule_json
serializer.serialize
end

def serializer
@serializer ||= CapsuleCRM::Serializer.new(
self, self.serializable_options
)
end
end
end
17 changes: 1 addition & 16 deletions lib/capsule_crm/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Task
include CapsuleCRM::Associations
include CapsuleCRM::Attributes
include CapsuleCRM::Collection
include CapsuleCRM::Serializable

attribute :id, Integer
attribute :due_date, Date
Expand Down Expand Up @@ -129,24 +130,8 @@ def persisted?
!new_record?
end

def to_capsule_json
serializer.serialize
end

private

def serializer
@serializer ||= CapsuleCRM::Serializer.new(self)
end

def capsule_attributes
{ description: description, category: category }.tap do |attrs|
attrs.merge!(owner: owner.username) if owner
attrs.merge!(due_date: due_date.to_s(:db)) if due_date
attrs.merge!(due_date_time: due_date_time.to_s(:db)) if due_date_time
end
end

def create_record
self.attributes = CapsuleCRM::Connection.post(
create_url, to_capsule_json
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/capsule_crm/case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,13 @@
it { should_not be_persisted }
end
end

describe '#to_capsule_json' do
let(:kase) { CapsuleCRM::Case.new }
subject { kase.to_capsule_json }

it 'should have a root of "kase"' do
expect(subject.keys.first).to eql('kase')
end
end
end
1 change: 0 additions & 1 deletion spec/lib/capsule_crm/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
end
let(:email_json) { subject['contacts']['email'].first }
let(:address_json) { subject['contacts']['address'].first }

subject { organization.to_capsule_json['organisation'] }

it { should have_key('name') }
Expand Down

0 comments on commit a45cb69

Please sign in to comment.