publishing_logic 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/model_logic.rb +8 -8
- data/spec/lib/model_logic_spec.rb +29 -35
- metadata +3 -3
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
gem.summary = %Q{Publishing logic for ActiveRecord models}
|
9
9
|
gem.description = %Q{Publishing logic for ActiveRecord models}
|
10
10
|
gem.email = "enquiries@unboxedconsulting.com"
|
11
|
-
gem.homepage = "http://github.com/
|
11
|
+
gem.homepage = "http://github.com/unboxed/publishing_logic"
|
12
12
|
gem.authors = ["Unboxed Consulting"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
14
|
gem.add_development_dependency "cucumber", ">= 0"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/model_logic.rb
CHANGED
@@ -1,10 +1,5 @@
|
|
1
1
|
module PublishingLogic
|
2
2
|
module ModelLogic
|
3
|
-
PUBLISHED_CONDITIONS = <<EOT
|
4
|
-
publishing_enabled = true AND
|
5
|
-
(published_until is null or published_until > ?) AND
|
6
|
-
(published_at is null or published_at < ?)
|
7
|
-
EOT
|
8
3
|
|
9
4
|
def published?
|
10
5
|
return false if published_at && Time.now < published_at
|
@@ -15,7 +10,12 @@ EOT
|
|
15
10
|
def self.included(base)
|
16
11
|
base.extend(ClassMethods)
|
17
12
|
base.instance_eval do
|
18
|
-
named_scope :published, lambda {{ :conditions => [
|
13
|
+
named_scope :published, lambda {{ :conditions => ["#{base.table_name}.publishing_enabled = true AND \
|
14
|
+
(#{base.table_name}.published_until is null or #{base.table_name}.published_until > ?) AND \
|
15
|
+
(#{base.table_name}.published_at is null or #{base.table_name}.published_at < ?)",
|
16
|
+
Time.now.utc, Time.now.utc] }} do
|
17
|
+
|
18
|
+
# TODO Not using table name with the following methods so in danger of getting ambiguous column names
|
19
19
|
def newest
|
20
20
|
find(:first, :order => "published_at DESC")
|
21
21
|
end
|
@@ -29,8 +29,8 @@ EOT
|
|
29
29
|
# identical as well, then order by id. This is done to ensure there is a unique
|
30
30
|
# ordering of objects, ordering by newest and oldest should result in arrays that are
|
31
31
|
# the inverse of the other.
|
32
|
-
named_scope :by_date_oldest_first, :order =>
|
33
|
-
named_scope :by_date_newest_first, :order =>
|
32
|
+
named_scope :by_date_oldest_first, :order => "#{base.table_name}.published_at ASC, #{base.table_name}.created_at ASC, #{base.table_name}.id ASC"
|
33
|
+
named_scope :by_date_newest_first, :order => "#{base.table_name}.published_at DESC, #{base.table_name}.created_at DESC, #{base.table_name}.id DESC"
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -1,38 +1,32 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../../../../spec/factories"))
|
2
3
|
require 'model_logic'
|
3
4
|
|
4
|
-
class FunItem < ActiveRecord::Base
|
5
|
-
include PublishingLogic::ModelLogic
|
6
|
-
end
|
7
|
-
|
8
|
-
Factory.define :fun_item do |c|
|
9
|
-
end
|
10
|
-
|
11
5
|
describe PublishingLogic::ModelLogic do
|
12
6
|
def create_objects_with_different_published_at_dates
|
13
|
-
@object2 = Factory.create(:
|
14
|
-
@object1 = Factory.create(:
|
15
|
-
@object3 = Factory.create(:
|
7
|
+
@object2 = Factory.create(:programme, :publishing_enabled => true, :published_at => 2.days.ago)
|
8
|
+
@object1 = Factory.create(:programme, :publishing_enabled => true, :published_at => 1.days.ago)
|
9
|
+
@object3 = Factory.create(:programme, :publishing_enabled => true, :published_at => 3.days.ago)
|
16
10
|
end
|
17
11
|
|
18
12
|
describe "published?" do
|
19
13
|
describe "with publishing enabled" do
|
20
14
|
it "should be published by default" do
|
21
|
-
Factory.create(:
|
15
|
+
Factory.create(:programme,
|
22
16
|
:publishing_enabled => true,
|
23
17
|
:published_at => nil,
|
24
18
|
:published_until => nil).should be_published
|
25
19
|
end
|
26
20
|
|
27
21
|
it "should not be published if the published_at datetime is in the future" do
|
28
|
-
Factory.create(:
|
22
|
+
Factory.create(:programme,
|
29
23
|
:publishing_enabled => true,
|
30
24
|
:published_at => 5.seconds.from_now,
|
31
25
|
:published_until => nil).should_not be_published
|
32
26
|
end
|
33
27
|
|
34
28
|
it "should not be published if the published_until datetime is in the past" do
|
35
|
-
Factory.create(:
|
29
|
+
Factory.create(:programme,
|
36
30
|
:publishing_enabled => true,
|
37
31
|
:published_at => nil,
|
38
32
|
:published_until => 5.seconds.ago).should_not be_published
|
@@ -40,7 +34,7 @@ describe PublishingLogic::ModelLogic do
|
|
40
34
|
end
|
41
35
|
describe "with publishing disabled" do
|
42
36
|
it "should not be published" do
|
43
|
-
Factory.create(:
|
37
|
+
Factory.create(:programme,
|
44
38
|
:publishing_enabled => false,
|
45
39
|
:published_at => 1.days.ago,
|
46
40
|
:published_until => 10.days.from_now).should_not be_published
|
@@ -50,13 +44,13 @@ describe PublishingLogic::ModelLogic do
|
|
50
44
|
|
51
45
|
describe "published named scope" do
|
52
46
|
it "should include published objects" do
|
53
|
-
|
54
|
-
|
47
|
+
programme = Factory.create(:programme, :publishing_enabled => true)
|
48
|
+
Programme.published.should == [programme]
|
55
49
|
end
|
56
50
|
|
57
51
|
it "should not include any unpublished objects" do
|
58
|
-
|
59
|
-
|
52
|
+
Factory.create(:programme, :publishing_enabled => false)
|
53
|
+
Programme.published.should be_empty
|
60
54
|
end
|
61
55
|
|
62
56
|
# it "should not expose a published episode published an hour ago" do
|
@@ -65,26 +59,26 @@ describe PublishingLogic::ModelLogic do
|
|
65
59
|
# end
|
66
60
|
|
67
61
|
it "should not include objects with a published_until in the past" do
|
68
|
-
Factory.create(:
|
62
|
+
Factory.create(:programme,
|
69
63
|
:publishing_enabled => true,
|
70
64
|
:published_until => 5.seconds.ago)
|
71
|
-
|
65
|
+
Programme.published.should be_empty
|
72
66
|
end
|
73
67
|
|
74
68
|
it "should not include objects with a published_at in the future" do
|
75
|
-
Factory.create(:
|
69
|
+
Factory.create(:programme,
|
76
70
|
:publishing_enabled => true,
|
77
71
|
:published_at => 5.seconds.from_now)
|
78
|
-
|
72
|
+
Programme.published.should be_empty
|
79
73
|
end
|
80
74
|
|
81
75
|
it "should get a new Time.now for each invocation of the named scope" do
|
82
|
-
item = Factory.create(:
|
76
|
+
item = Factory.create(:programme,
|
83
77
|
:publishing_enabled => true,
|
84
78
|
:published_until => 10.days.from_now)
|
85
79
|
mock_now = mock('now', :utc => 20.days.from_now, :to_f => 0)
|
86
80
|
Time.stub(:now).and_return mock_now
|
87
|
-
|
81
|
+
Programme.published.should be_empty
|
88
82
|
end
|
89
83
|
|
90
84
|
it "should use the utc of the current time" do
|
@@ -92,7 +86,7 @@ describe PublishingLogic::ModelLogic do
|
|
92
86
|
mock_now = mock('now')
|
93
87
|
Time.stub(:now).and_return mock_now
|
94
88
|
mock_now.should_receive(:utc).twice
|
95
|
-
|
89
|
+
Programme.published
|
96
90
|
end
|
97
91
|
|
98
92
|
describe "newest" do
|
@@ -101,12 +95,12 @@ describe PublishingLogic::ModelLogic do
|
|
101
95
|
end
|
102
96
|
|
103
97
|
it "should be the most recently published object" do
|
104
|
-
|
98
|
+
Programme.published.newest.should == @object1
|
105
99
|
end
|
106
100
|
end
|
107
101
|
describe "oldest" do
|
108
102
|
it "should be the object published the longest ago" do
|
109
|
-
|
103
|
+
Programme.published.oldest.should == @object3
|
110
104
|
end
|
111
105
|
end
|
112
106
|
end
|
@@ -115,18 +109,18 @@ describe PublishingLogic::ModelLogic do
|
|
115
109
|
describe "by date oldest first" do
|
116
110
|
it "should return the items, oldest first" do
|
117
111
|
create_objects_with_different_published_at_dates
|
118
|
-
|
112
|
+
Programme.by_date_oldest_first.map(&:id).should == [@object3,
|
119
113
|
@object2,
|
120
114
|
@object1].map(&:id)
|
121
115
|
end
|
122
116
|
|
123
117
|
it "should order by created_at date if published_ats are equal" do
|
124
118
|
create_objects_with_different_published_at_dates
|
125
|
-
@object2b = Factory.create(:
|
119
|
+
@object2b = Factory.create(:programme,
|
126
120
|
:publishing_enabled => true,
|
127
121
|
:published_at => @object2.published_at,
|
128
122
|
:created_at => 3.days.ago)
|
129
|
-
|
123
|
+
Programme.by_date_oldest_first.map(&:id).should == [@object3,
|
130
124
|
@object2b,
|
131
125
|
@object2,
|
132
126
|
@object1].map(&:id)
|
@@ -136,18 +130,18 @@ describe PublishingLogic::ModelLogic do
|
|
136
130
|
describe "by date newest first" do
|
137
131
|
it "should return the items, oldest first" do
|
138
132
|
create_objects_with_different_published_at_dates
|
139
|
-
|
133
|
+
Programme.by_date_newest_first.map(&:id).should == [@object1,
|
140
134
|
@object2,
|
141
135
|
@object3].map(&:id)
|
142
136
|
end
|
143
137
|
|
144
138
|
it "should order by created_at date if published_ats are equal" do
|
145
139
|
create_objects_with_different_published_at_dates
|
146
|
-
@object2b = Factory.create(:
|
140
|
+
@object2b = Factory.create(:programme,
|
147
141
|
:publishing_enabled => true,
|
148
142
|
:published_at => @object2.published_at,
|
149
143
|
:created_at => 3.days.from_now)
|
150
|
-
|
144
|
+
Programme.by_date_newest_first.map(&:id).should == [@object1,
|
151
145
|
@object2b,
|
152
146
|
@object2,
|
153
147
|
@object3].map(&:id)
|
@@ -157,8 +151,8 @@ describe PublishingLogic::ModelLogic do
|
|
157
151
|
it "should have a newest first ordering that is the reverse of the oldest first ordering for identical objects" do
|
158
152
|
creation_time = 2.days.ago
|
159
153
|
publish_time = 1.days.ago
|
160
|
-
5.times { Factory.create(:
|
161
|
-
|
154
|
+
5.times { Factory.create(:programme, :created_at => creation_time, :published_at => publish_time) }
|
155
|
+
Programme.by_date_newest_first.map(&:id).should == Programme.by_date_oldest_first.map(&:id).reverse
|
162
156
|
end
|
163
157
|
end
|
164
158
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: publishing_logic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Unboxed Consulting
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-17 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -63,7 +63,7 @@ files:
|
|
63
63
|
- spec/spec.opts
|
64
64
|
- spec/spec_helper.rb
|
65
65
|
has_rdoc: true
|
66
|
-
homepage: http://github.com/
|
66
|
+
homepage: http://github.com/unboxed/publishing_logic
|
67
67
|
licenses: []
|
68
68
|
|
69
69
|
post_install_message:
|