Skip to content

Commit

Permalink
Fixed contacts assignment. Child records are now saved using callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed Jan 17, 2014
1 parent aa7b91d commit 4411339
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
25 changes: 16 additions & 9 deletions lib/capsule_crm/contacts.rb
Expand Up @@ -34,8 +34,7 @@ def initialize(attributes = {})
#
# Returns an Array of CapsuleCRM::Address objects
def addresses=(addresses)
addresses = CapsuleCRM::Address.new(addresses) if addresses.is_a?(Hash)
@addresses = Array(addresses)
set_collection(CapsuleCRM::Address, addresses)
end

# Public: Gets the addresses for this contacts container
Expand All @@ -62,11 +61,10 @@ def addresses
#
# Returns an Array of CapsuleCRM::Email objects
def emails=(emails)
@emails = [emails].compact.flatten.map do |email|
email.is_a?(Hash) ? CapsuleCRM::Email.new(email) : email
end
set_collection(CapsuleCRM::Email, emails)
end


# Public: Gets the emails for this contacts container
#
# Examples
Expand All @@ -90,8 +88,7 @@ def emails
#
# Returns an Array of CapsuleCRM::Phone objects
def phones=(phones)
phones = CapsuleCRM::Phone.new(phones) if phones.is_a?(Hash)
@phones = Array(phones)
set_collection(CapsuleCRM::Phone, phones)
end

# Public: Gets the phones for this contacts container
Expand Down Expand Up @@ -119,8 +116,7 @@ def phones
#
# Returns an Array of CapsuleCRM::Website objects
def websites=(websites)
websites = CapsuleCRM::Website.new(websites) if websites.is_a?(Hash)
@websites = Array(websites)
set_collection(CapsuleCRM::Website, websites)
end

# Public: Gets the websites for this contacts container
Expand Down Expand Up @@ -149,5 +145,16 @@ def to_capsule_json
website: websites.map(&:to_capsule_json)
}.delete_if { |key, value| value.blank? }.stringify_keys
end

private

def set_collection(klass, collection)
objects = [collection].compact.flatten.map do |item|
item.is_a?(Hash) ? klass.new(item) : item
end
instance_variable_set(
:"@#{klass.to_s.downcase.demodulize.pluralize}", objects
)
end
end
end
4 changes: 2 additions & 2 deletions lib/capsule_crm/gettable.rb
Expand Up @@ -3,8 +3,8 @@ module Gettable
extend ActiveSupport::Concern

module ClassMethods
def get(path)
CapsuleCRM::Connection.get(path)
def get(path, options = {})
CapsuleCRM::Connection.get(path, options)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/capsule_crm/person.rb
Expand Up @@ -34,6 +34,8 @@ class Person < CapsuleCRM::Party

belongs_to :organization, foreign_key: :organisation_id

after_save :save_custom_fields

validates :id, numericality: { allow_blank: true }
validates :first_name, presence: { if: :first_name_required? }
validates :last_name, presence: { if: :last_name_required? }
Expand Down
35 changes: 32 additions & 3 deletions spec/lib/capsule_crm/contacts_spec.rb
Expand Up @@ -26,13 +26,14 @@
end

describe '#emails=' do
let(:contacts) { CapsuleCRM::Contacts.new }
before { contacts.emails = email_attributes }
subject { contacts.emails }

context 'when a hash is supplied' do
let(:contacts) { CapsuleCRM::Contacts.new }
let(:email_attributes) do
{ email_address: 'matt@gmail.com', type: 'Work' }
end
before { contacts.emails = email_attributes }
subject { contacts.emails }

it 'should create an email from the attributes' do
expect(subject.first).to be_a(CapsuleCRM::Email)
Expand All @@ -42,6 +43,34 @@
expect(subject).to be_a(Array)
end
end

context 'when an array is supplied' do
context 'when the array contains hashes' do
let(:email_attributes) do
[{ email_address: Faker::Internet.email, type: 'Work' }]
end

it 'should create an email from the attributes' do
expect(subject.first).to be_a(CapsuleCRM::Email)
end

it 'should be an array' do
expect(subject).to be_a(Array)
end
end

context 'when the array contains emails' do
let(:email) do
CapsuleCRM::Email.
new(email_address: Faker::Internet.email, type: 'Work')
end
let(:email_attributes) { [email] }

it 'should set the emails from the supplied array' do
expect(subject).to eql(email_attributes)
end
end
end
end

describe '#initialize' do
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/capsule_crm/custom_field_spec.rb
Expand Up @@ -50,6 +50,8 @@
end

it 'should have a root element of "customField"' do
p described_class.serializable_options
p subject
expect(subject.keys.first).to eql('customField')
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/support/shared_examples/persistable.rb
Expand Up @@ -42,14 +42,17 @@
subject { described_class.create attributes }

it "should save the embedded #{name}" do
p subject
p described_class.embedded_associations
p subject.custom_fields
expect(WebMock).to have_requested(
:put, "https://1234:@company.capsulecrm.com/api/#{singular}/#{subject.id}/#{plural}"
).with(subject.send(name).to_capsule_json(root: collection_root))
)
end
end
end

if described_class.attributes.map(&:name).include?(:track_id)
if described_class.attribute_set.map(&:name).include?(:track_id)
context "when the #{described_class} has a track" do
let(:track) { CapsuleCRM::Track.new(id: Random.rand(1..10)) }
subject do
Expand Down

0 comments on commit 4411339

Please sign in to comment.