acts_as_publicable 0.0.2 → 0.0.3
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.textile
CHANGED
@@ -13,14 +13,17 @@ In order to use in your model, write something like the following:
|
|
13
13
|
end
|
14
14
|
</pre>
|
15
15
|
|
16
|
-
With that you get
|
16
|
+
With that you get three very simple scopes:
|
17
17
|
|
18
18
|
<pre>
|
19
19
|
ruby-1.9.2-p180 :001 > Article.unpublished
|
20
|
-
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published
|
20
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published = 0)
|
21
21
|
=> []
|
22
22
|
ruby-1.9.2-p180 :002 > Article.published
|
23
|
-
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published
|
23
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published = 1)
|
24
|
+
=> []
|
25
|
+
ruby-1.9.2-p180 :003 > Article.by_published_state(true)
|
26
|
+
Article Load (0.2ms) SELECT `articles`.* FROM `articles` WHERE (published = 1)
|
24
27
|
=> []
|
25
28
|
</pre>
|
26
29
|
|
@@ -8,8 +8,9 @@ module ActsAsPublicable
|
|
8
8
|
module ClassMethods
|
9
9
|
def acts_as_publicable
|
10
10
|
raise ActsAsPublicable::FieldNotPresentError unless column_names.include?(:published.to_s)
|
11
|
-
scope :
|
12
|
-
scope :
|
11
|
+
scope :by_published_state, lambda { |state| where("published = ?",state) }
|
12
|
+
scope :published, by_published_state(true)
|
13
|
+
scope :unpublished, by_published_state(false)
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -1,9 +1,11 @@
|
|
1
1
|
class AddPublishedTo<%= table_name.camelize %> < ActiveRecord::Migration
|
2
|
+
|
2
3
|
def self.up
|
3
|
-
add_column :<%= table_name %>, :published, :boolean
|
4
|
+
add_column :<%= table_name %>, :published, :boolean, :default => false
|
4
5
|
end
|
5
6
|
|
6
7
|
def self.down
|
7
8
|
remove_column :<%= table_name %>, :published
|
8
9
|
end
|
10
|
+
|
9
11
|
end
|
@@ -9,12 +9,57 @@ describe "acts_as_publicable" do
|
|
9
9
|
}.should raise_error ActsAsPublicable::FieldNotPresentError
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
Article.
|
12
|
+
before(:all) do
|
13
|
+
Article.create!(:title=>"first",:published=>true)
|
14
|
+
Article.create!(:title=>"second")
|
15
|
+
Article.create!(:title=>"third")
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
context "published scope" do
|
19
|
+
|
20
|
+
it "responds to published" do
|
21
|
+
Article.should respond_to(:published)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns the published articles" do
|
25
|
+
Article.published.all? {|a| a.published}.should be true
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
context "unpublished scope" do
|
31
|
+
|
32
|
+
it "adds unpublished scope" do
|
33
|
+
Article.should respond_to(:unpublished)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the unpublished articles" do
|
37
|
+
Article.unpublished.any? {|a| a.published}.should be false
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context "by_published_state" do
|
43
|
+
|
44
|
+
it "adds by_published_state scope" do
|
45
|
+
Article.should respond_to(:by_published_state)
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with true as parameter" do
|
49
|
+
|
50
|
+
it "returns the published articles" do
|
51
|
+
Article.by_published_state(true).all? {|a| a.published}.should be true
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with false as parameter" do
|
57
|
+
|
58
|
+
it "returns the unpublished articles" do
|
59
|
+
Article.by_published_state(false).any? {|a| a.published}.should be false
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
18
63
|
end
|
19
64
|
|
20
65
|
describe "#publish!" do
|
data/spec/support/models.rb
CHANGED