publishable 0.2.2 → 0.3.0
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/VERSION +1 -1
- data/db/schema.rb +3 -0
- data/lib/publishable.rb +27 -21
- data/publishable.gemspec +1 -1
- data/spec/publishable_spec.rb +36 -4
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/db/schema.rb
CHANGED
data/lib/publishable.rb
CHANGED
@@ -1,31 +1,37 @@
|
|
1
1
|
require 'publishable/railtie' if defined?(Rails)
|
2
2
|
|
3
3
|
module Publishable
|
4
|
+
def self.extended(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
4
8
|
module ClassMethods
|
5
|
-
def publishable
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
def publishable(options = {})
|
10
|
+
column_name = (options[:on] || :published_at).to_s
|
11
|
+
|
12
|
+
if respond_to?(:scope)
|
13
|
+
scope :published, lambda { |time = Time.now|
|
14
|
+
where("#{column_name} IS NOT NULL AND #{column_name} <= ?", time.utc)
|
15
|
+
}
|
9
16
|
|
10
|
-
|
11
|
-
|
12
|
-
|
17
|
+
scope :unpublished, lambda { |time = Time.now|
|
18
|
+
where("#{column_name} IS NULL OR #{column_name} > ?", time.utc)
|
19
|
+
}
|
20
|
+
end
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
module InstanceMethods
|
19
|
-
def published?(time = Time.now)
|
20
|
-
published_at ? published_at <= time : false
|
21
|
-
end
|
22
|
+
class_eval <<-EVIL, __FILE__, __LINE__ + 1
|
23
|
+
def published?(time = Time.now)
|
24
|
+
#{column_name} ? #{column_name} <= time : false
|
25
|
+
end
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def publish(time = Time.now)
|
28
|
+
self.#{column_name} = time unless published?(time)
|
29
|
+
end
|
30
|
+
|
31
|
+
def publish!(time = Time.now)
|
32
|
+
publish(time) && (!respond_to?(:save) || save)
|
33
|
+
end
|
34
|
+
EVIL
|
29
35
|
end
|
30
36
|
end
|
31
37
|
end
|
data/publishable.gemspec
CHANGED
data/spec/publishable_spec.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require File.expand_path('../spec_helper', __FILE__)
|
2
2
|
|
3
3
|
class Album < ActiveRecord::Base
|
4
|
-
extend Publishable
|
4
|
+
extend Publishable
|
5
5
|
publishable
|
6
6
|
end
|
7
7
|
|
8
|
+
class Feature < ActiveRecord::Base
|
9
|
+
extend Publishable
|
10
|
+
publishable :on => :public_since
|
11
|
+
end
|
12
|
+
|
8
13
|
describe Publishable do
|
9
14
|
ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
|
10
15
|
:database => File.expand_path('../../db/test.sqlite3', __FILE__)
|
@@ -50,9 +55,9 @@ describe Publishable do
|
|
50
55
|
describe "scope" do
|
51
56
|
before do
|
52
57
|
@album_1 = Album.create!(:published_at => nil)
|
53
|
-
@album_2 = Album.create!(:published_at => Time.now
|
54
|
-
@album_3 = Album.create!(:published_at => Time.now
|
55
|
-
@album_4 = Album.create!(:published_at => Time.now
|
58
|
+
@album_2 = Album.create!(:published_at => Time.now - 60)
|
59
|
+
@album_3 = Album.create!(:published_at => Time.now)
|
60
|
+
@album_4 = Album.create!(:published_at => Time.now + 60)
|
56
61
|
end
|
57
62
|
|
58
63
|
it "should find published records" do
|
@@ -63,4 +68,31 @@ describe Publishable do
|
|
63
68
|
Album.unpublished.should == [@album_1, @album_4]
|
64
69
|
end
|
65
70
|
end
|
71
|
+
|
72
|
+
describe "other column" do
|
73
|
+
it "should be publishable" do
|
74
|
+
@feature = Feature.new
|
75
|
+
@feature.should_not be_published
|
76
|
+
@feature.publish
|
77
|
+
@feature.should be_published
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "scope" do
|
81
|
+
before do
|
82
|
+
Feature.delete_all
|
83
|
+
@feature_1 = Feature.create!(:public_since => nil)
|
84
|
+
@feature_2 = Feature.create!(:public_since => Time.now - 60)
|
85
|
+
@feature_3 = Feature.create!(:public_since => Time.now)
|
86
|
+
@feature_4 = Feature.create!(:public_since => Time.now + 60)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should find published records" do
|
90
|
+
Feature.published.should == [@feature_2, @feature_3]
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should find unpublished records" do
|
94
|
+
Feature.unpublished.should == [@feature_1, @feature_4]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
66
98
|
end
|