Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7222b2
commit b10d295
Showing
10 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module CapsuleCRM | ||
class Tag | ||
include Virtus | ||
|
||
extend ActiveModel::Naming | ||
include ActiveModel::Conversion | ||
include ActiveModel::Validations | ||
|
||
attribute :name | ||
|
||
validates :name, presence: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module CapsuleCRM | ||
module Taggable | ||
extend ActiveSupport::Concern | ||
|
||
def tags | ||
CapsuleCRM::Connection.get( | ||
"/api/#{api_singular_name}/#{id}/tag" | ||
)['tags']['tag'].map { |item| CapsuleCRM::Tag.new(item) } | ||
end | ||
|
||
def add_tag(tag_name) | ||
if id | ||
CapsuleCRM::Connection.post( | ||
"/api/#{api_singular_name}/#{id}/#{URI.encode(tag_name)}" | ||
) | ||
end | ||
end | ||
|
||
def api_singular_name | ||
self.class.to_s.demodulize.downcase.singularize | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'spec_helper' | ||
|
||
describe CapsuleCRM::Tag do | ||
|
||
it { should validate_presence_of(:name) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
|
||
class TaggableItem | ||
include CapsuleCRM::Taggable | ||
include Virtus | ||
|
||
attribute :id | ||
end | ||
|
||
describe CapsuleCRM::Taggable do | ||
before { configure } | ||
|
||
describe '#tags' do | ||
before do | ||
stub_request(:get, /\/api\/taggableitem\/1\/tag$/). | ||
to_return(body: File.read('spec/support/all_tags.json')) | ||
end | ||
|
||
let(:taggable_item) { TaggableItem.new(id: 1) } | ||
|
||
subject { taggable_item.tags } | ||
|
||
it { should be_a(Array) } | ||
|
||
it { subject.length.should eql(2) } | ||
|
||
it do | ||
subject.all? { |item| item.is_a?(CapsuleCRM::Tag) }.should be_true | ||
end | ||
|
||
it { subject.first.name.should eql('Customer') } | ||
|
||
it { subject.last.name.should eql('VIP') } | ||
end | ||
|
||
describe '#add_tag' do | ||
context 'when the taggable item has an id' do | ||
let(:taggable_item) { TaggableItem.new(id: 1) } | ||
|
||
before do | ||
loc = 'https://sample.capsulecrm.com/api/party/1000/tag/A%20Test%20Tag' | ||
stub_request(:post, /\/api\/taggableitem\/1\/A%20Test%20Tag$/). | ||
to_return(headers: { 'Location' => loc }) | ||
end | ||
|
||
subject { taggable_item.add_tag 'A Test Tag' } | ||
|
||
it { subject.should be_true } | ||
end | ||
|
||
context 'when the taggable item has no id' do | ||
let(:taggable_item) { TaggableItem.new } | ||
|
||
subject { taggable_item.add_tag 'A Test Tag' } | ||
|
||
it { subject.should be_nil } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"tags": { | ||
"tag": [ | ||
{ | ||
"name": "Customer" | ||
}, | ||
{ | ||
"name": "VIP" | ||
} | ||
] | ||
} | ||
} |