subly 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/subly.rb +4 -5
- data/lib/subly/model.rb +24 -1
- data/lib/subly/version.rb +1 -1
- data/spec/spec_helper.rb +3 -1
- data/spec/subly_model_spec.rb +5 -0
- data/spec/subly_spec.rb +13 -3
- data/subly.gemspec +2 -0
- metadata +19 -8
data/lib/subly.rb
CHANGED
@@ -38,10 +38,9 @@ module Subly
|
|
38
38
|
|
39
39
|
module InstanceMethods
|
40
40
|
def add_subscription(name, args = {})
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
self.sublies.create(:name => name, :value => value, :starts_at => start_date, :ends_at => end_date)
|
41
|
+
args[:starts_at] ||= Time.now
|
42
|
+
args[:name] = name
|
43
|
+
self.sublies.create(args)
|
45
44
|
end
|
46
45
|
|
47
46
|
def has_subscription?(name)
|
@@ -54,4 +53,4 @@ module Subly
|
|
54
53
|
end
|
55
54
|
end
|
56
55
|
|
57
|
-
ActiveRecord::Base.send :extend, Subly
|
56
|
+
ActiveRecord::Base.send :extend, Subly
|
data/lib/subly/model.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Subly
|
2
2
|
class Model < ActiveRecord::Base
|
3
|
+
require 'chronic_duration'
|
3
4
|
set_table_name 'subly_models'
|
4
5
|
|
5
6
|
belongs_to :subscriber, :polymorphic => true
|
6
7
|
|
8
|
+
before_validation :convert_duration
|
9
|
+
|
7
10
|
validates_presence_of :subscriber_type
|
8
11
|
validates_presence_of :subscriber_id, :unless => Proc.new { |subly| subly.subscriber_type.nil? }
|
9
12
|
validate :ends_after_starts
|
@@ -27,11 +30,31 @@ module Subly
|
|
27
30
|
scoped(:conditions => {:name => name})
|
28
31
|
end
|
29
32
|
|
33
|
+
def duration=(duration)
|
34
|
+
@duration = duration
|
35
|
+
end
|
36
|
+
|
37
|
+
def duration
|
38
|
+
@duration
|
39
|
+
end
|
40
|
+
|
41
|
+
def active?
|
42
|
+
now = Time.now
|
43
|
+
!!(starts_at <= now && (ends_at.nil? || ends_at > now))
|
44
|
+
end
|
45
|
+
|
30
46
|
private
|
31
47
|
def ends_after_starts
|
32
48
|
if !starts_at.nil? && !ends_at.nil? && (ends_at < starts_at)
|
33
49
|
raise "ends_at must be after starts_at"
|
34
50
|
end
|
35
51
|
end
|
52
|
+
|
53
|
+
def convert_duration
|
54
|
+
unless duration.blank?
|
55
|
+
self.starts_at = Time.zone.now if self.starts_at.blank?
|
56
|
+
self.ends_at = self.starts_at + ::ChronicDuration.parse(self.duration.to_s).seconds
|
57
|
+
end
|
58
|
+
end
|
36
59
|
end
|
37
|
-
end
|
60
|
+
end
|
data/lib/subly/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -15,6 +15,8 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
|
15
15
|
#ActiveRecord::Schema.verbose = false
|
16
16
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
17
17
|
|
18
|
+
ActiveRecord::Base.logger = Logger.new('/dev/null')
|
19
|
+
|
18
20
|
ActiveRecord::Base.configurations = true
|
19
21
|
ActiveRecord::Schema.define(:version => 1) do
|
20
22
|
create_table :items do |t|
|
@@ -43,4 +45,4 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
43
45
|
add_index :subly_models, [:subscriber_type,:subscriber_id], :name => 'subscriber_idx'
|
44
46
|
add_index :subly_models, :starts_at, :name => 'starts_idx'
|
45
47
|
add_index :subly_models, :ends_at, :name => 'ends_idx'
|
46
|
-
end
|
48
|
+
end
|
data/spec/subly_model_spec.rb
CHANGED
@@ -20,4 +20,9 @@ describe Subly::Model do
|
|
20
20
|
it "subscription should not be created when ends before starts" do
|
21
21
|
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
|
22
22
|
end
|
23
|
+
|
24
|
+
it "should be active if it stats in the past and ends in the future" do
|
25
|
+
i = Item.create
|
26
|
+
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
|
27
|
+
end
|
23
28
|
end
|
data/spec/subly_spec.rb
CHANGED
@@ -11,7 +11,7 @@ end
|
|
11
11
|
describe Subly do
|
12
12
|
it "should add a subscription" do
|
13
13
|
thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
|
14
|
-
thing_one.add_subscription('sub name'
|
14
|
+
thing_one.add_subscription('sub name',:value => 'sub value').should be_true
|
15
15
|
thing_one.reload
|
16
16
|
thing_one.sublies.count.should == 1
|
17
17
|
end
|
@@ -26,7 +26,7 @@ describe Subly do
|
|
26
26
|
|
27
27
|
it "has_subscription should return true even for expired subscription" do
|
28
28
|
thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
|
29
|
-
thing_one.add_subscription('sub name', :
|
29
|
+
thing_one.add_subscription('sub name', :starts_at => Time.now - 1.day, :ends_at => Time.now - 2.hours).should be_true
|
30
30
|
thing_one.reload
|
31
31
|
thing_one.has_subscription?('sub name').should be_true
|
32
32
|
end
|
@@ -42,7 +42,7 @@ describe Subly do
|
|
42
42
|
it "should find models with even expired subscriptions" do
|
43
43
|
Thing.delete_all
|
44
44
|
thing_one = Thing.create(:name => 'Thing One', :description => 'foo')
|
45
|
-
thing_one.add_subscription('sub name', :
|
45
|
+
thing_one.add_subscription('sub name', :starts_at => Time.now - 1.day, :ends_at => Time.now - 2.hours).should be_true
|
46
46
|
Thing.with_subscription('sub name').should == [thing_one]
|
47
47
|
thing_one.destroy
|
48
48
|
end
|
@@ -62,4 +62,14 @@ describe Subly do
|
|
62
62
|
item.add_subscription('subby')
|
63
63
|
item.is_subby?.should be_true
|
64
64
|
end
|
65
|
+
|
66
|
+
it "should convert duration to time" do
|
67
|
+
time = Time.parse('2001-01-01T010101+0000')
|
68
|
+
Time.stub!(:zone).and_return(Time)
|
69
|
+
Time.stub!(:now).and_return(time)
|
70
|
+
item = Item.create(:name => 'foo')
|
71
|
+
item.add_subscription('subby', :duration => "1 year")
|
72
|
+
item.is_subby?.should be_true
|
73
|
+
item.sublies.last.ends_at.should == time + 1.year
|
74
|
+
end
|
65
75
|
end
|
data/subly.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-01-09 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156449020 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 2.3.12
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156449020
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rspec
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156448640 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156448640
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: sqlite3
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156448120 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,7 +44,18 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2156448120
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: chronic_duration
|
50
|
+
requirement: &2156447620 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :runtime
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2156447620
|
48
59
|
description: This was purpose built to extend models with subscriptions that were
|
49
60
|
controlled via SalesForce
|
50
61
|
email:
|