Permalink
Browse files

Adding subscribed flag to ringer to allow ringer unsubscrib and resub…

…scribe to the movement.
  • Loading branch information...
Willa
Willa committed Nov 7, 2012
1 parent d02ae83 commit ec99204b6e2474819315250d104129b2bd6053b9
@@ -5,8 +5,6 @@ def self.included(base)
include DataMapper::Resource
property :phone_number, DataMapper::Property::String, key: true
-
-
validates_uniqueness_of :phone_number
def phone_number=(number)
@@ -17,7 +15,6 @@ def self.from(number)
norm_number = Phoner::Phone.parse(number).to_s
self.first(phone_number: norm_number)
end
-
end
end
end
@@ -46,7 +43,7 @@ def ring(ringer)
class AssignedUnsubscribeVoiceNumber < AssignedVoiceNumber
def ring(ringer)
- #ringer rang an unsubscribe number
+ ringer.unsubscribe
end
end
@@ -74,6 +71,7 @@ def self.from(type, number)
def self.handle(type, request)
number = from(type, request.to)
ringer = Ringer.from(request.from)
+ ringer.subscribe
case number
when AssignedVoiceNumber
@@ -13,7 +13,7 @@ def tags=(tags)
end
def accept?(ringer)
- tag_filter.accept?(ringer)
+ tag_filter.accept?(ringer) && ringer.subscribed?
end
def send_message(params)
@@ -26,6 +26,5 @@ def send_message(params)
false
end
end
-
end
end
View
@@ -14,6 +14,7 @@ class Ringer
property :id, Serial
property :phone_number, String, unique: true
property :created_at, DateTime
+ property :subscribed, Boolean, default: true
has n, :ringer_taggings, constraint: :destroy
has n, :tags, through: :ringer_taggings, constraint: :skip
@@ -39,5 +40,21 @@ def add_tags
tags.concat(RegionTags.tags_for(number))
save
end
+
+ def unsubscribe
+ update(subscribed: false)
+ end
+
+ def subscribe
+ update(subscribed: true)
+ end
+
+ def subscribed?
+ subscribed
+ end
+
+ def unsubscribed?
+ !subscribed
+ end
end
end
@@ -69,4 +69,30 @@
end
end
+describe Crowdring::AssignedUnsubscribeVoiceNumber do
+ before(:each) do
+ DataMapper.auto_migrate!
+ Crowdring::AssignedUnsubscribeVoiceNumber.create(phone_number: '+18002222222')
+ end
+
+ it 'should unsubscribe a ringer when calls' do
+ Crowdring::Ringer.create(phone_number: '+18000000000')
+ request = double('request', from: '+18000000000', to:'+18002222222')
+ Crowdring::AssignedPhoneNumber.handle(:voice, request)
+
+ Crowdring::Ringer.first.unsubscribed?.should be_true
+ end
+
+ it 'should re-subscribe a ringer when calls after unsubscribing' do
+ @campaign = Crowdring::Campaign.create(title: 'test')
+ @campaign.voice_numbers.new(phone_number: '+18001111111', description: 'desc')
+ @campaign.sms_number = Crowdring::AssignedSMSNumber.new(phone_number: '+18002222222')
+ @campaign.save
+ Crowdring::Ringer.create(phone_number: '+18000000000', subscribed: false)
+ request = double('request', from: '+18000000000', to:'+18001111111')
+ Crowdring::AssignedPhoneNumber.handle(:voice, request)
+
+ Crowdring::Ringer.first.subscribed?.should be_true
+ end
+end

0 comments on commit ec99204

Please sign in to comment.