couch_publish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ module CouchPublish
2
+ def self.included(base)
3
+ base.send :include, Memories unless base.ancestors.include? Memories
4
+ end
5
+
6
+ def publish!(&block)
7
+ self.milestone! &block
8
+ end
9
+
10
+ # Returns true if this version is published
11
+ def published?
12
+ self.milestone? or self.milestone_commit?
13
+ end
14
+
15
+ # Returns true if this version has not been published
16
+ def draft?
17
+ not self.published?
18
+ end
19
+
20
+ # Returns the latest version of the document that was published
21
+ def last_published_version
22
+ self.milestones.empty? ? nil : self.milestones.last.instance
23
+ end
24
+
25
+ # Has any of version of this document ever been published?
26
+ def previously_published?
27
+ not self.milestones.empty?
28
+ end
29
+
30
+ # Discard the working draft, and revert the document
31
+ # back to the last published version
32
+ def discard_draft!(&block)
33
+ discard_draft_exception_check
34
+ self.rollback_to_latest_milestone!
35
+ self.publish! &block
36
+ self
37
+ end
38
+
39
+ # Discard the working draft, and revert the document
40
+ # back to the last published state, but don't save.
41
+ # Note that if you save the document after calling this method,
42
+ # you would still have to call the publish! method
43
+ # if you want the revert to be published
44
+ def discard_draft
45
+ discard_draft_exception_check
46
+ self.rollback_to_latest_milestone
47
+ self
48
+ end
49
+
50
+ def published_versions
51
+ self.milestones
52
+ end
53
+
54
+ private
55
+ def discard_draft_exception_check
56
+ raise "The current version of the document is published. There is no working draft to discard!" if self.published?
57
+ raise "This document has never been published. If we discard the current draft, then we'd end up in an existential conundrum." unless self.previously_published?
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ require 'memories'
2
+ require 'couch_publish/couch_publish'
@@ -0,0 +1,189 @@
1
+ require 'spec_helper'
2
+
3
+ describe CouchPublish do
4
+ context "given a document instance that includes CouchPublish" do
5
+
6
+ describe "#publish!" do
7
+ before do
8
+ @doc = Article.create :title => 'test', :body => 'test test'
9
+ end
10
+
11
+ it "should create a milestone" do
12
+ @doc.milestones.length.should == 0
13
+ @doc.publish!
14
+ @doc.milestones.length.should == 1
15
+ end
16
+ end
17
+
18
+ describe "#published_versions" do
19
+ before do
20
+ @doc = Article.create :title => 'test', :body => 'test test'
21
+ @doc.publish!
22
+ @doc.title = 'test 2'
23
+ @doc.save
24
+ @doc.title = 'test 3'
25
+ @doc.save
26
+ @doc.publish!
27
+ end
28
+
29
+ it "should return all milestones" do
30
+ @doc.published_versions.should == @doc.milestones
31
+ end
32
+ end
33
+
34
+
35
+ describe "#draft?" do
36
+ context "given a new document" do
37
+ before do
38
+ @doc = Article.create :title => 't', :body => 't'
39
+ end
40
+
41
+ it "should return true" do
42
+ @doc.draft?.should be_true
43
+ end
44
+ end
45
+
46
+ context "given a document that was just published" do
47
+ before do
48
+ @doc = Article.create :title => 't', :body => 't'
49
+ @doc.publish!
50
+ end
51
+
52
+ it "should return false" do
53
+ @doc.draft?.should be_false
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#published?" do
59
+ context "given a document that has been published" do
60
+ before do
61
+ @doc = Article.create :title => 'test', :body => 'test'
62
+ @doc.publish!
63
+ end
64
+
65
+ it "should return true" do
66
+ @doc.published?.should be_true
67
+ end
68
+ end
69
+
70
+ context "given a document that has never been published" do
71
+ before do
72
+ @doc = Article.create :title => 'test', :body => 'test'
73
+ end
74
+
75
+ it "should return false" do
76
+ @doc.published?.should be_false
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#last_published_version" do
82
+ context "given a document that has been published" do
83
+ before do
84
+ @doc = Article.create :title => 'test', :body => 'test'
85
+ @doc.publish!
86
+ @doc.title = 'test 2'
87
+ @doc.save
88
+ end
89
+
90
+ it "should return the latest milestone" do
91
+ @doc.last_published_version.should == @doc.milestones.last.instance
92
+ end
93
+ end
94
+
95
+ context "given a document that has never been published" do
96
+ before do
97
+ @doc = Article.create :title => 'test', :body => 'test'
98
+ end
99
+
100
+ it "should return nil" do
101
+ @doc.last_published_version.should be_nil
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#discard_draft!" do
107
+ context "given a document whose current version is published" do
108
+ before do
109
+ @doc = Article.create :title => 'test', :body => 'test'
110
+ @doc.publish!
111
+ @doc.title = 'test 2'
112
+ @doc.save
113
+ @doc.publish!
114
+ end
115
+
116
+ it "should raise an exception" do
117
+ proc {@doc.discard_draft!}.should raise_exception("The current version of the document is published. There is no working draft to discard!")
118
+ end
119
+ end
120
+
121
+ context "given a document that has never been published" do
122
+ before do
123
+ @doc = Article.create :title => 'test', :body => 'test'
124
+ end
125
+
126
+ it "should raise an exception" do
127
+ proc {@doc.discard_draft!}.should raise_exception("This document has never been published. If we discard the current draft, then we'd end up in an existential conundrum.")
128
+ end
129
+ end
130
+
131
+ context "given a document whose current version is not published, but has previous published versions" do
132
+ before do
133
+ @doc = Article.create :title => 'test', :body => 'test'
134
+ @doc.publish!
135
+ @doc.title = 'test 2'
136
+ @doc.save
137
+ end
138
+
139
+ it "should revert the current document to the previous publish state" do
140
+ @doc.discard_draft!.title.should == "test"
141
+ end
142
+
143
+ it "should mark the newly reverted version as published" do
144
+ @doc.discard_draft!.published?.should be_true
145
+ end
146
+ end
147
+ end
148
+
149
+ describe "#discard_draft" do
150
+ context "given a document whose current version is published" do
151
+ before do
152
+ @doc = Article.create :title => 'test', :body => 'test'
153
+ @doc.publish!
154
+ @doc.title = 'test 2'
155
+ @doc.save
156
+ @doc.publish!
157
+ end
158
+
159
+ it "should raise an exception" do
160
+ proc {@doc.discard_draft}.should raise_exception("The current version of the document is published. There is no working draft to discard!")
161
+ end
162
+ end
163
+
164
+ context "given a document that has never been published" do
165
+ before do
166
+ @doc = Article.create :title => 'test', :body => 'test'
167
+ end
168
+
169
+ it "should raise an exception" do
170
+ proc {@doc.discard_draft}.should raise_exception("This document has never been published. If we discard the current draft, then we'd end up in an existential conundrum.")
171
+ end
172
+ end
173
+
174
+ context "given a document whose current version is not published, but has previous published versions" do
175
+ before do
176
+ @doc = Article.create :title => 'test', :body => 'test'
177
+ @doc.publish!
178
+ @doc.title = 'test 2'
179
+ @doc.save
180
+ end
181
+
182
+ it "should revert the current document to the previous publish state" do
183
+ @doc.discard_draft.title.should == "test"
184
+ end
185
+ end
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'rspec'
3
+ require 'couch_publish'
4
+
5
+ COUCHDB_SERVER = CouchRest.new "http://admin:password@localhost:5984"
6
+ COUCH_PUBLISH_TEST_DB = COUCHDB_SERVER.database!('couch_publish_test')
7
+
8
+ class Article < CouchRest::Model::Base
9
+ use_database COUCH_PUBLISH_TEST_DB
10
+ include CouchPublish
11
+
12
+ property :title
13
+ property :body
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ config.mock_with :rspec
18
+
19
+ config.before(:each) do
20
+ COUCH_PUBLISH_TEST_DB.recreate!
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couch_publish
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Parker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-15 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: memories
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 2
34
+ version: 0.3.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Rides on top of the `memories` gem to add simple publishing functionality with rollback and preview support.
38
+ email: moonmaster9000@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/couch_publish.rb
47
+ - lib/couch_publish/couch_publish.rb
48
+ - spec/lib/couch_publish_spec.rb
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/moonmaster9000/couch_publish
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Publishing functionality for couchdb documents.
84
+ test_files:
85
+ - spec/lib/couch_publish_spec.rb
86
+ - spec/spec_helper.rb