Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added expire, expired?, in_the_future? and unexpired scope to the mod…

…el; Added expiry methods to models that include subly
  • Loading branch information...
commit 87b68ab49face64274dfb918ad3f24b3c1693e27 1 parent 41be286
@fuelxc fuelxc authored
View
11 lib/subly.rb
@@ -50,6 +50,17 @@ def has_subscription?(name)
def has_active_subscription?(name)
self.sublies.active.by_name(name).count > 0
end
+
+ def cancel_active_subscriptions(name)
+ self.sublies.active.by_name(name).collect(&:expire_now)
+ end
+
+ def cancel_all_subscriptions(name)
+ self.sublies.unexpired.by_name(name).each do |sub|
+ #if active, deactivate, else destroy as and end_time greater than start is not valid
+ sub.active? ? sub.expire_now : sub.destroy
+ end
+ end
end
end
View
19 lib/subly/model.rb
@@ -21,6 +21,11 @@ def self.expired
scoped(:conditions => ['starts_at <= ? AND ends_at <= ?', now, now])
end
+ def self.unexpired
+ now = Time.zone.now
+ scoped(:conditions => ['ends_at > ? OR ends_at IS NULL', now])
+ end
+
def self.for_subscriber(sub)
raise ArgumentError('wrong number of arguments (0 for 1)') if sub.blank?
scoped(:conditions => ['subscriber_type = ? AND subscriber_id = ?',sub.class.to_s, sub.id])
@@ -43,6 +48,20 @@ def active?
!!(starts_at <= now && (ends_at.nil? || ends_at > now))
end
+ def expired?
+ now = Time.now
+ !(ends_at.nil? || ends_at > now)
+ end
+
+ def in_the_future?
+ now = Time.now
+ !!(starts_at > now && (ends_at.nil? || ends_at > now))
+ end
+
+ def expire_now
+ self.update_attribute(:ends_at, Time.now)
+ end
+
private
def ends_after_starts
if !starts_at.nil? && !ends_at.nil? && (ends_at < starts_at)
View
2  lib/subly/version.rb
@@ -1,3 +1,3 @@
module Subly
- VERSION = "0.2.0"
+ VERSION = "0.3.0"
end
View
4 spec/spec_helper.rb
@@ -6,7 +6,9 @@
require 'sqlite3'
#Time.zone.now doesn't work in test
-
+def stub_time_zone
+ Time.stub!(:zone).and_return(Time)
+end
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
View
19 spec/subly_model_spec.rb
@@ -21,8 +21,23 @@ class Thing < ActiveRecord::Base
lambda{Subly::Model.create!(:name => 'Foo', :subscriber_type => 'Foo', :subscriber_id => 1, :starts_at => Time.now + 1.minute, :ends_at => Time.now - 1.minute)}.should raise_error
end
- it "should be active if it stats in the past and ends in the future" do
- i = Item.create
+ it "should be expire immediately" do
+ s = Subly::Model.create(:name => 'Foo', :subscriber_type => 'Item', :subscriber_id => 1, :starts_at => Time.now - 5.minute)
+ s.active?.should be_true
+ s.expire_now.should be_true
+ s.reload
+ s.active?.should be_false
+ end
+
+ it "should be active if it starts in the past and ends in the future" do
Subly::Model.new(:name => 'Foo', :subscriber_type => 'Item', :subscriber_id => 1, :starts_at => Time.now - 1.minute, :ends_at => Time.now + 1.hour).active?.should be_true
end
+
+ it "should be expired if it ends in the past" do
+ Subly::Model.new(:name => 'Foo', :subscriber_type => 'Item', :subscriber_id => 1, :starts_at => Time.now - 5.minute, :ends_at => Time.now - 4.minutes).expired?.should be_true
+ end
+
+ it "should be in_the_future if it starts and ends in the future" do
+ Subly::Model.new(:name => 'Foo', :subscriber_type => 'Item', :subscriber_id => 1, :starts_at => Time.now + 5.minute, :ends_at => Time.now + 14.minutes).in_the_future?.should be_true
+ end
end
View
32 spec/subly_spec.rb
@@ -17,7 +17,7 @@ class Thing < ActiveRecord::Base
end
it "subscription should default to active" do
- Time.stub!(:zone).and_return(Time)
+ stub_time_zone
thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
thing_one.add_subscription('sub name').should be_true
thing_one.reload
@@ -52,12 +52,12 @@ class Thing < ActiveRecord::Base
end
it "is method should be false if it does not have an active sub" do
- Time.stub!(:zone).and_return(Time)
+ stub_time_zone
Item.new.is_subby?.should be_false
end
it "is method should be true if it has an active sub" do
- Time.stub!(:zone).and_return(Time)
+ stub_time_zone
item = Item.create(:name => 'foo')
item.add_subscription('subby')
item.is_subby?.should be_true
@@ -65,11 +65,35 @@ class Thing < ActiveRecord::Base
it "should convert duration to time" do
time = Time.parse('2001-01-01T010101+0000')
- Time.stub!(:zone).and_return(Time)
+ stub_time_zone
Time.stub!(:now).and_return(time)
item = Item.create(:name => 'foo')
item.add_subscription('subby', :duration => "1 year")
item.is_subby?.should be_true
item.sublies.last.ends_at.should == time + 1.year
end
+
+ it "should change the end date of all active subscriptions" do
+ stub_time_zone
+ thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
+ thing_one.add_subscription('sub name',:value => 'sub value').should be_true
+ thing_one.add_subscription('sub name',:value => 'sub value', :starts_at => Time.now + 1.month).should be_true
+ thing_one.reload
+ thing_one.cancel_active_subscriptions('sub name')
+ thing_one.sublies.count.should == 2
+ thing_one.sublies.unexpired.count.should == 1
+ thing_one.sublies.expired.count.should == 1
+ end
+
+ it "should cancel all subscriptions" do
+ stub_time_zone
+ thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
+ thing_one.add_subscription('sub name',:value => 'sub value').should be_true
+ thing_one.add_subscription('sub name',:value => 'sub value', :starts_at => Time.now + 1.month).should be_true
+ thing_one.reload
+ thing_one.cancel_all_subscriptions('sub name')
+ thing_one.sublies.count.should == 1
+ thing_one.sublies.unexpired.count.should == 0
+ thing_one.sublies.expired.count.should == 1
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.