Class: ActsAsIcontact::Subscription

Inherits:
Resource
  • Object
show all
Defined in:
lib/acts_as_icontact/resources/subscription.rb

Overview

Subscriptions are effectively a “has_many :through” mapping between Contacts and Lists. They are implemented that way, with a .lists property on Contact and a .subscribers property on List that both work through the Subscription class.

Constant Summary collapse

STATUSES =
%w(normal pending unsubscribed)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #connection, #error, #errors, find, find_by_id, first, #id, #method_missing, #new_record?, #save, #save!

Constructor Details

#initialize(properties = {}) ⇒ Subscription

Defaults status to “normal”



11
12
13
# File 'lib/acts_as_icontact/resources/subscription.rb', line 11

def initialize(properties = {})
  super({:status => "normal"}.merge(properties))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActsAsIcontact::Resource

Class Method Details

.all(options = {}, forwardTo = nil) ⇒ Object

Overrides “all” to pass an optional forwardTo parameter to ResourceCollection. This instructs ResourceCollection to look up and create a new object of the given type, rather than the base resource. (This is how the #lists and #contacts methods are implemented.)



34
35
36
37
38
39
# File 'lib/acts_as_icontact/resources/subscription.rb', line 34

def self.all(options={}, forwardTo = nil)
  query_options = default_options.merge(options)
  validate_options(query_options)
  result = query_collection(query_options)
  ResourceCollection.new(self, result, forwardTo)
end

.contacts(options = {}) ⇒ Object

Returns a collection of all contacts matching the query. Unfortunately, this has to be performed by looking up each one individually. We forward the Contact class to ResourceCollection to do that work.



43
44
45
# File 'lib/acts_as_icontact/resources/subscription.rb', line 43

def self.contacts(options={})
  all(options, ActsAsIcontact::Contact)
end

.find_by_string(value) ⇒ Object

Searches on the compound primary key (“listid_contactid”).



16
17
18
# File 'lib/acts_as_icontact/resources/subscription.rb', line 16

def self.find_by_string(value)
  find_by_id(value)
end

.lists(options = {}) ⇒ Object

Returns a collection of all lists matching the query. Unfortunately, this has to be performed by looking up each one individually. We forward the List class to ResourceCollection to do that work.



49
50
51
# File 'lib/acts_as_icontact/resources/subscription.rb', line 49

def self.lists(options={})
  all(options, ActsAsIcontact::List)
end

.never_on_updateObject

Never send listId or contactId on update



26
27
28
# File 'lib/acts_as_icontact/resources/subscription.rb', line 26

def self.never_on_update
  super + %w(listId contactId)
end

.required_on_createObject

Requires listId, contactId, and status when creating a record



21
22
23
# File 'lib/acts_as_icontact/resources/subscription.rb', line 21

def self.required_on_create
  super + %w(listId contactId status)
end

Instance Method Details

#confirmationMessageObject

The confirmation message for this subscription, if any.



69
70
71
# File 'lib/acts_as_icontact/resources/subscription.rb', line 69

def confirmationMessage
  ActsAsIcontact::Message.find(confirmationMessageId) if properties["confirmationMessageId"]
end

#contactObject

The contact that this subscription is associated with.



64
65
66
# File 'lib/acts_as_icontact/resources/subscription.rb', line 64

def contact
  ActsAsIcontact::Contact.find(contactId) if properties["contactId"]
end

#listObject

The list that this subscription is associated with.



59
60
61
# File 'lib/acts_as_icontact/resources/subscription.rb', line 59

def list
  ActsAsIcontact::List.find(listId) if properties["listId"]
end

#validate_on_save(fields) ⇒ Object

status must be one of: normal, pending, unsubscribed



54
55
56
# File 'lib/acts_as_icontact/resources/subscription.rb', line 54

def validate_on_save(fields)
  raise ActsAsIcontact::ValidationError, "Status must be one of: #{STATUSES.join(', ')}" unless STATUSES.include?(fields["status"])
end