Skip to content

Commit

Permalink
Added update callback and subscribe methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SFEley committed Jul 29, 2009
1 parent 057993d commit 1b1dab8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
30 changes: 29 additions & 1 deletion lib/acts_as_icontact/rails/callbacks.rb
Expand Up @@ -22,8 +22,36 @@ def icontact_after_create
end
end
self.save
@icontact_in_progress = false # Very primitive loop prevention
end

end

def icontact_after_update
unless @icontact_in_progress # Avoid callback loops
c = find_contact_by_identity
self.class.icontact_mappings.each do |rails, iContact|
if (value = self.send(rails))
ic = (iContact.to_s + '=').to_sym # Blah. This feels like it should be easier.
c.send(ic, value)
end
end
c.save
# No need to update the record this time; iContact field changes don't have side effects
end
end

private
def find_contact_by_identity
im = self.class.icontact_identity_map
if (im[1] == :contactId)
ActsAsIcontact::Contact.find(self.send(im[0]))
elsif (im[0] == :id)
ActsAsIcontact::Contact.find(im[1] => id)
else
ActsAsIcontact::Contact.find(:email => self.send(im[0]))
end
rescue ActsAsIcontact::QueryError
nil
end

end
Expand Down
1 change: 1 addition & 0 deletions lib/acts_as_icontact/rails/macro.rb
Expand Up @@ -20,6 +20,7 @@ def acts_as_icontact(options = {})
# If we haven't flaked out so far, we must be doing okay. Make magic happen.
include ActsAsIcontact::Rails::Callbacks
after_create :icontact_after_create
after_update :icontact_after_update
end

end
Expand Down
7 changes: 7 additions & 0 deletions lib/acts_as_icontact/resources/contact.rb
Expand Up @@ -21,5 +21,12 @@ def lists
@lists ||= ActsAsIcontact::Subscription.lists(:contactId => id)
end

# Creates a new subscription for the contact to the specified list
def subscribe(list)
l = ActsAsIcontact::List.find(list)
s = ActsAsIcontact::Subscription.new(:contactId => id, :listId => l.id)
s.save
end

end
end
7 changes: 7 additions & 0 deletions lib/acts_as_icontact/resources/list.rb
Expand Up @@ -30,5 +30,12 @@ def subscribers
@subscribers ||= ActsAsIcontact::Subscription.contacts(:listId => id)
end

# Creates a new subscription for the specified list by the contact
def subscribe(contact)
c = ActsAsIcontact::Contact.find(contact)
s = ActsAsIcontact::Subscription.new(:contactId => c.id, :listId => id)
s.save
end

end
end
31 changes: 24 additions & 7 deletions spec/rails_spec/callbacks_spec.rb
Expand Up @@ -5,15 +5,32 @@
@person = @class.new(:firstName => "John", :surname => "Smith", :email => "john@example.org")
end

it "will create a new contact after record creation" do
# ActsAsIcontact::Contact.any_instance.expects(:save).returns(true)
@person.save
context "for creation" do
it "creates a new contact" do
conn = mock('Class Connection')
conn.expects(:post).with(regexp_matches(/Smith/)).returns('{"contacts":{}}')
ActsAsIcontact::Contact.expects(:connection).returns(conn)
@person.save
end

it "updates the Person with the results of the contact creation" do
@person.save
@person.icontact_id.should == 333444
end
end

it "updates the Person with the results of the contact save" do
@person.save
@person.icontact_id.should == 333444
context "for update" do
before(:each) do
@person.save
@person.surname = "Nielsen Hayden"
end

it "updates the contact with the new fields" do
conn = mock('Instance Connection')
conn.expects(:post).with(regexp_matches(/Nielsen Hayden/)).returns('{"contact":{}}')
ActsAsIcontact::Contact.any_instance.expects(:connection).returns(conn)
@person.save
end
end

end
end
7 changes: 7 additions & 0 deletions spec/resources/contact_spec.rb
Expand Up @@ -22,6 +22,13 @@
@john.lists.first.should == ActsAsIcontact::List.find(444444)
end

it "can subscribe oneself to a list" do
conn = mock('Class Connection')
conn.expects(:post).with(regexp_matches(/444444/) && regexp_matches(/333333/)).returns('{"subscriptions":{}}')
ActsAsIcontact::Subscription.expects(:connection).returns(conn)
@john.subscribe(444444)
end

it "knows its history"
end

Expand Down
8 changes: 8 additions & 0 deletions spec/resources/list_spec.rb
Expand Up @@ -40,5 +40,13 @@
it "knows its welcome message" do
@list.welcomeMessage.should == ActsAsIcontact::Message.find(555555)
end

it "can be subscribed to by a subscriber" do
conn = mock('Class Connection')
conn.expects(:post).with(regexp_matches(/444444/) && regexp_matches(/333333/)).returns('{"subscriptions":{}}')
ActsAsIcontact::Subscription.expects(:connection).returns(conn)
@list.subscribe(333333)
end

end
end

0 comments on commit 1b1dab8

Please sign in to comment.