-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
7 changed files
with
119 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module ActsAsIcontact | ||
class Segment < Resource | ||
|
||
# Name and listId are required | ||
def self.required_on_create | ||
super << 'listId' << 'name' | ||
end | ||
|
||
# Name and listId are required | ||
def self.required_on_update | ||
super << 'name' | ||
end | ||
|
||
# Cannot pass listId when updating | ||
def self.never_on_update | ||
['listId'] | ||
end | ||
|
||
# Searches on segment name. | ||
def self.find_by_string(value) | ||
first(:name => value) | ||
end | ||
|
||
|
||
# Returns the list to which this segment is bound. | ||
def list | ||
@list ||= ActsAsIcontact::List.find(listId.to_i) if (listId.to_i) > 0 | ||
end | ||
|
||
# Returns a collection of SegmentCriteria resources for this segment. The usual iContact search options (limit, offset, search terms, etc.) can be passed. | ||
def criteria(options={}) | ||
@criteria ||= ActsAsIcontact::SegmentCriteria.scoped_find(self, options) | ||
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,36 @@ | ||
module ActsAsIcontact | ||
# The list of criteria attached to every Segment. Because of this intrinsic association, the usual #find methods don't | ||
# work; this collection _must_ be obtained using the individual segment's #criteria method. | ||
# Unlike other subresources, SegmentCriteria can be created, modified, and saved. | ||
class SegmentCriteria < Subresource | ||
|
||
alias_method :segment, :parent | ||
|
||
# fieldName, operator, and values are required | ||
def self.required_on_create | ||
super + %w(fieldName operator values) | ||
end | ||
|
||
# Looks like 'criteria' is just a bit too strange for ActiveSupport to know singulars and plurals... | ||
def self.resource_name # :nodoc: | ||
"criterion" | ||
end | ||
|
||
def self.collection_name # :nodoc: | ||
"criteria" | ||
end | ||
|
||
# Uses criterionId as its ID. | ||
def id | ||
properties["criterionId"] | ||
end | ||
|
||
|
||
# operator must be one: eq, lt, lte, gt, gte, bet, notcontains, contains | ||
def validate_on_save(fields) | ||
operators = %w(eq lt lte gt gte bet notcontains contains) | ||
raise ActsAsIcontact::ValidationError, "operator must be one of: " + operators.join(', ') unless operators.include?(fields["operator"]) | ||
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
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,31 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe ActsAsIcontact::Segment do | ||
|
||
|
||
it "requires a listId" do | ||
s = ActsAsIcontact::Segment.new | ||
lambda{s.save}.should raise_error(ActsAsIcontact::ValidationError, /listId/) | ||
end | ||
|
||
it "requires a name" do | ||
s = ActsAsIcontact::Segment.new | ||
lambda{s.save}.should raise_error(ActsAsIcontact::ValidationError, /name/) | ||
end | ||
|
||
context "associations" do | ||
# We have _one_ really good segment set up here | ||
before(:each) do | ||
@segment = ActsAsIcontact::Segment.find("People Named John") | ||
end | ||
|
||
it "knows its list" do | ||
@segment.list.name.should == "First Test" | ||
end | ||
|
||
it "knows its criteria" do | ||
@segment.criteria.first.fieldName.should == "firstName" | ||
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