acts_as_publicable 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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 two very simple scopes:
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 is false)
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 is true)
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 :published, where("published is true")
12
- scope :unpublished, where("published is false")
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,3 +1,3 @@
1
1
  module ActsAsPublicable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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
- it "adds published scope" do
13
- Article.should respond_to(:published)
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
- it "adds unpublished scope" do
17
- Article.should respond_to(:unpublished)
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
@@ -1,4 +1,5 @@
1
1
  class Article < ActiveRecord::Base
2
+ attr_accessible :title,:published
2
3
  acts_as_publicable
3
4
  end
4
5
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_publicable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - lucapette