Class: ActsAsIcontact::Subscription
- 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
-
.all(options = {}, forwardTo = nil) ⇒ Object
Overrides “all” to pass an optional forwardTo parameter to ResourceCollection.
-
.contacts(options = {}) ⇒ Object
Returns a collection of all contacts matching the query.
-
.find_by_string(value) ⇒ Object
Searches on the compound primary key (“listid_contactid”).
-
.lists(options = {}) ⇒ Object
Returns a collection of all lists matching the query.
-
.never_on_update ⇒ Object
Never send listId or contactId on update.
-
.required_on_create ⇒ Object
Requires listId, contactId, and status when creating a record.
Instance Method Summary collapse
-
#confirmationMessage ⇒ Object
The confirmation message for this subscription, if any.
-
#contact ⇒ Object
The contact that this subscription is associated with.
-
#initialize(properties = {}) ⇒ Subscription
constructor
Defaults status to “normal”.
-
#list ⇒ Object
The list that this subscription is associated with.
-
#validate_on_save(fields) ⇒ Object
status must be one of: normal, pending, unsubscribed.
Methods inherited from Resource
#==, #connection, #error, #errors, find, find_by_id, first, #id, #inspect, #method_missing, #new_record?, #property_names, #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(={}, forwardTo = nil) = .merge() () result = query_collection() ResourceCollection.new(self, result, :forwardTo => 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(={}) all(, 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(={}) all(, ActsAsIcontact::List) end |
.never_on_update ⇒ Object
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_create ⇒ Object
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
#confirmationMessage ⇒ Object
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 |
#contact ⇒ Object
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 |
#list ⇒ Object
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 |