ismasan-ar_publish_control 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -1
- data/ar_publish_control.gemspec +1 -1
- data/lib/ar_publish_control.rb +10 -5
- data/spec/ar_publish_control_spec.rb +15 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -76,10 +76,13 @@ Adds published, unpublished and published_only(boolean) named_scopes to your mod
|
|
76
76
|
@upcoming_post = Post.upcoming
|
77
77
|
@upcoming_post.first.upcoming? # => true
|
78
78
|
|
79
|
-
#
|
79
|
+
# You can fetch objects that have expired (ie. they have an unpublish_at date set in the past)
|
80
80
|
@expired_posts = Post.expired
|
81
81
|
@expired_posts.first.expired? # => true
|
82
82
|
|
83
|
+
# Finally, if you want those objects where is_published == false, regardless of dates
|
84
|
+
@drafts = Post.draft
|
85
|
+
|
83
86
|
All of these named_scopes can be chained with other finder conditions and paginated with will_paginate
|
84
87
|
|
85
88
|
== REQUIREMENTS:
|
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.9'
|
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
|
#
|
@@ -31,12 +31,16 @@ module ArPublishControl
|
|
31
31
|
# You can use two named_scopes to find the published or unpublished objects.
|
32
32
|
# You can chain them with other scopes and use them on association collections:
|
33
33
|
#
|
34
|
-
# Post.all.
|
35
|
-
# Post.published.
|
36
|
-
# Post.unpublished.
|
34
|
+
# Post.all.count # => 15
|
35
|
+
# Post.published.count # => 10
|
36
|
+
# Post.unpublished.count # => 5 includes upcoming and expired
|
37
|
+
# Post.draft.count # => where is_published == false regardless of date
|
37
38
|
# @post.comments.published
|
38
39
|
#
|
39
40
|
# Post.recent.published
|
41
|
+
#
|
42
|
+
# Klass.unpublished includes all objects which is_published flag is set to false and/or are expired or upcoming.
|
43
|
+
# If you specifically want those where is_published == false regardless of dates, use Klass.draft
|
40
44
|
#
|
41
45
|
# There's a third named_scope that you can pass a boolean in order to find only published items or all of them
|
42
46
|
# This is useful in controller for permission-based publish control
|
@@ -46,7 +50,7 @@ module ArPublishControl
|
|
46
50
|
#
|
47
51
|
# Available statuses. This constant is useful for autogenerated routes, controller actions and view helpers
|
48
52
|
#
|
49
|
-
STATUSES = [:published, :
|
53
|
+
STATUSES = [:published, :draft, :upcoming, :expired]
|
50
54
|
|
51
55
|
module Publishable
|
52
56
|
|
@@ -99,6 +103,7 @@ module ArPublishControl
|
|
99
103
|
named_scope :unpublished, lambda{{:conditions => unpublished_conditions}}
|
100
104
|
named_scope :upcoming, lambda{{:conditions => upcoming_conditions}}
|
101
105
|
named_scope :expired, lambda {{:conditions => expired_conditions}}
|
106
|
+
named_scope :draft, :conditions => {:is_published => false}
|
102
107
|
|
103
108
|
named_scope :published_only, lambda {|*args|
|
104
109
|
bool = (args.first.nil? ? true : (args.first)) # nil = true by default
|
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe ArPublishControl do
|
4
4
|
it "should have array of available statuses" do
|
5
|
-
(ArPublishControl::STATUSES == [:published, :
|
5
|
+
(ArPublishControl::STATUSES == [:published, :draft, :upcoming, :expired]).should be_true
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -170,6 +170,20 @@ describe Post, 'expired' do
|
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
173
|
+
describe Post, 'draft' do
|
174
|
+
before(:each) do
|
175
|
+
Post.destroy_all
|
176
|
+
@p1 = Post.create(:title => 'p1',:is_published => true,:publish_at => 2.weeks.ago) #published
|
177
|
+
@p2 = Post.create(:title => 'p2',:is_published => true,:publish_at => 2.weeks.ago,:unpublish_at => 1.day.ago)#expired
|
178
|
+
@p3 = Post.create(:title => 'p3',:is_published => false,:publish_at => 3.days.ago,:unpublish_at => 2.hours.ago)#unpublished and expired
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should have draft" do
|
182
|
+
Post.draft.count.should == 1
|
183
|
+
Post.draft.first.should == @p3
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
173
187
|
describe "new record" do
|
174
188
|
it "should default to Time.now" do
|
175
189
|
# d = Time.now
|