ismasan-ar_publish_control 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +7 -0
- data/ar_publish_control.gemspec +1 -1
- data/lib/ar_publish_control.rb +11 -1
- data/spec/ar_publish_control_spec.rb +16 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -72,6 +72,13 @@ Adds published, unpublished and published_only(boolean) named_scopes to your mod
|
|
72
72
|
|
73
73
|
# You can access the ones marked as "published" but with a publish date in the future
|
74
74
|
@upcoming_post = Post.upcoming
|
75
|
+
@upcoming_post.first.upcoming? # => true
|
76
|
+
|
77
|
+
# Finally, you can fetch objects that have expired (ie. they have an unpublish_at date set in the past)
|
78
|
+
@expired_posts = Post.expired
|
79
|
+
@expired_posts.first.expired? # => true
|
80
|
+
|
81
|
+
All of these named_scopes can be chain with other finder conditions and paginated with will_paginate
|
75
82
|
|
76
83
|
== REQUIREMENTS:
|
77
84
|
|
data/ar_publish_control.gemspec
CHANGED
data/lib/ar_publish_control.rb
CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
module ArPublishControl
|
5
|
-
VERSION = '0.0.
|
5
|
+
VERSION = '0.0.4'
|
6
6
|
# This is a gem version of http://github.com/avdgaag/acts_as_publishable ( a Rails plugin)
|
7
7
|
# Thanks to Avdaag for his awesome, super readable code which I ripped off for this gem.
|
8
8
|
#
|
@@ -94,6 +94,7 @@ module ArPublishControl
|
|
94
94
|
named_scope :published, lambda{{:conditions => published_conditions}}
|
95
95
|
named_scope :unpublished, lambda{{:conditions => unpublished_conditions}}
|
96
96
|
named_scope :upcoming, lambda{{:conditions => upcoming_conditions}}
|
97
|
+
named_scope :expired, lambda {{:conditions => expired_conditions}}
|
97
98
|
|
98
99
|
named_scope :published_only, lambda {|*args|
|
99
100
|
bool = (args.first.nil? ? true : (args.first)) # nil = true by default
|
@@ -135,6 +136,11 @@ module ArPublishControl
|
|
135
136
|
t = Time.now
|
136
137
|
["(#{table_name}.is_published = ? AND #{table_name}.publish_at > ?)",true,t]
|
137
138
|
end
|
139
|
+
|
140
|
+
def expired_conditions
|
141
|
+
t = Time.now
|
142
|
+
["(#{table_name}.unpublish_at IS NOT NULL AND #{table_name}.unpublish_at < ?)",t]
|
143
|
+
end
|
138
144
|
end
|
139
145
|
|
140
146
|
module InstanceMethods
|
@@ -170,6 +176,10 @@ module ArPublishControl
|
|
170
176
|
(is_published? && publish_at > Time.now)
|
171
177
|
end
|
172
178
|
|
179
|
+
def expired?
|
180
|
+
(!unpublish_at.nil? && unpublish_at < Time.now)
|
181
|
+
end
|
182
|
+
|
173
183
|
# Indefinitely publish the current object right now
|
174
184
|
def publish
|
175
185
|
return if published?
|
@@ -144,3 +144,19 @@ describe Post, 'upcoming' do
|
|
144
144
|
Post.upcoming.size.should == 2
|
145
145
|
end
|
146
146
|
end
|
147
|
+
|
148
|
+
describe Post, 'expired' do
|
149
|
+
before(:each) do
|
150
|
+
Post.destroy_all
|
151
|
+
@p1 = Post.create(:title => 'p1',:is_published => true,:publish_at => 2.weeks.ago) #published
|
152
|
+
@p2 = Post.create(:title => 'p2',:is_published => true,:publish_at => 2.weeks.ago,:unpublish_at => 1.day.ago)#expired
|
153
|
+
@p3 = Post.create(:title => 'p3',:is_published => false,:publish_at => 3.days.ago,:unpublish_at => 2.hours.ago)#unpublished and expired
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have expired" do
|
157
|
+
@p1.expired?.should be_false
|
158
|
+
@p2.expired?.should be_true
|
159
|
+
@p3.expired?.should be_true
|
160
|
+
Post.expired.size.should == 2
|
161
|
+
end
|
162
|
+
end
|