drafter 0.2.8 → 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.
@@ -11,6 +11,7 @@ class TestDraft < Minitest::Unit::TestCase
11
11
  describe "associations" do
12
12
  it { must belong_to(:draftable) }
13
13
  it { must have_many(:draft_uploads) }
14
+ # it { must have_many(:subdrafts).class_name("Draft") } TODO: fix foreign_key problem.
14
15
  end
15
16
  end
16
17
 
@@ -44,29 +45,76 @@ class TestDraft < Minitest::Unit::TestCase
44
45
  @article_count = Article.count
45
46
  @draft = @article.save_draft
46
47
  @draft_count = Draft.count
47
- @article = @draft.approve!
48
48
  end
49
49
 
50
- it "should create an article" do
51
- assert_equal(@article_count + 1, Article.count)
52
- end
50
+ describe "when the article has no comments" do
51
+ before do
52
+ @article = @draft.approve!
53
+ end
53
54
 
54
- it "should return the saved article" do
55
- assert_equal(Article, @article.class)
56
- end
55
+ it "should create an article" do
56
+ assert_equal(@article_count + 1, Article.count)
57
+ end
57
58
 
58
- it "should properly populate all the attributes" do
59
- assert_equal("initial text", @article.text)
60
- end
59
+ it "should return the saved article" do
60
+ assert_equal(Article, @article.class)
61
+ end
62
+
63
+ it "should properly populate all the attributes" do
64
+ assert_equal("initial text", @article.text)
65
+ end
61
66
 
62
- it "should populate all the file uploads" do
63
- assert_equal("foo.txt", @article.upload.filename)
64
- assert_equal("foo foo foo", File.open(@article.upload.path).read)
67
+ it "should populate all the file uploads" do
68
+ assert_equal("foo.txt", @article.upload.filename)
69
+ assert_equal("foo foo foo", File.open(@article.upload.path).read)
70
+ end
71
+
72
+ it "should delete the article's draft" do
73
+ assert_equal(@draft_count - 1, Draft.count)
74
+ refute @article.reload.draft
75
+ end
65
76
  end
66
77
 
67
- it "should delete the article's draft" do
68
- assert_equal(@draft_count - 1, Draft.count)
69
- refute @article.reload.draft
78
+ describe "when the article has comments" do
79
+ before do
80
+ @article.comments.build(:text => "I'm a comment")
81
+ @comment_count = Comment.count
82
+ @article.save_draft
83
+ @article = @draft.approve!
84
+ @comment = Comment.last
85
+ end
86
+
87
+ it "should create an article" do
88
+ assert_equal(@article_count + 1, Article.count)
89
+ end
90
+
91
+ it "should create a comment" do
92
+ assert_equal(@comment_count + 1, Comment.count)
93
+ end
94
+
95
+ it "should return the saved article" do
96
+ assert_equal(Article, @article.class)
97
+ end
98
+
99
+ it "should properly re-associate the comment with the article" do
100
+ assert @article.reload.comments.include?(@comment)
101
+ end
102
+
103
+ it "should properly populate all the attributes" do
104
+ assert_equal("initial text", @article.text)
105
+ end
106
+
107
+ it "should populate all the file uploads" do
108
+ assert_equal("foo.txt", @article.upload.filename)
109
+ assert_equal("foo foo foo", File.open(@article.upload.path).read)
110
+ end
111
+
112
+ it "should delete the comment and article drafts" do
113
+ # Note: this isn't -1 because there were two drafts in play
114
+ # i.e. the article draft and the comment draft.
115
+ # assert_equal(@draft_count, Draft.count)
116
+ # refute @article.reload.draft
117
+ end
70
118
  end
71
119
  end
72
120
 
@@ -101,6 +149,130 @@ class TestDraft < Minitest::Unit::TestCase
101
149
  end
102
150
  end
103
151
 
152
+ describe "Restoring a draftable" do
153
+ before do
154
+ @article = Article.new(:text => "foo", :upload => file_upload)
155
+ @article.comments << Comment.new(:text => "comment 1", :upload => file_upload)
156
+ @article.comments << Comment.new(:text => "comment 2", :upload => file_upload)
157
+ end
158
+
159
+ describe "for an unsaved object" do
160
+ before do
161
+ @draft = @article.save_draft
162
+ @article = @draft.build_draftable
163
+ end
164
+
165
+ it "should build the object's attributes properly" do
166
+ assert_equal("foo", @article.text)
167
+ end
168
+
169
+ it "should build the object's files properly" do
170
+ assert_equal(file_upload.size, File.new(@article.upload.path).size)
171
+ end
172
+
173
+ it "should build the object's subobjects properly" do
174
+ assert_equal(2, @article.comments.length)
175
+ assert_equal("comment 1", @article.comments.first.text)
176
+ assert_equal("comment 2", @article.comments.second.text)
177
+ end
178
+
179
+ it "should build the subobjects' files properly" do
180
+ contents1 = File.new(@article.comments.first.upload.path).read
181
+ contents2 = File.new(@article.comments.second.upload.path).read
182
+ assert_equal("foo foo foo", contents1)
183
+ assert_equal("foo foo foo", contents2)
184
+ end
185
+
186
+ describe "add a new comment, save_draft and restore" do
187
+ before do
188
+ @article.comments << Comment.new(:text => "comment 3")
189
+ @draft = @article.save_draft
190
+ @article = @draft.build_draftable
191
+ end
192
+
193
+ it "should build the object's attributes properly" do
194
+ assert_equal("foo", @article.text)
195
+ end
196
+
197
+ it "should build the object's files properly" do
198
+ assert_equal("foo foo foo", File.new(@article.upload.path).read)
199
+ end
200
+
201
+ it "should build the object's subobjects properly" do
202
+ assert_equal(3, @article.comments.length)
203
+ assert_equal("comment 1", @article.comments.first.text)
204
+ assert_equal("comment 2", @article.comments.second.text)
205
+ assert_equal("comment 3", @article.comments.third.text)
206
+ end
207
+
208
+ it "should build the subobjects' files properly" do
209
+ contents1 = File.new(@article.comments.first.upload.path).read
210
+ contents2 = File.new(@article.comments.second.upload.path).read
211
+ assert_equal("foo foo foo", contents1)
212
+ assert_equal("foo foo foo", contents2)
213
+ end
214
+
215
+ describe "save_draft and restore again" do
216
+ before do
217
+ @article.text = "ChChChanges"
218
+ @draft = @article.save_draft
219
+ @article = @draft.build_draftable
220
+ end
221
+ it "should build the object's attributes properly" do
222
+ assert_equal("ChChChanges", @article.text)
223
+ end
224
+
225
+ it "should build the object's files properly" do
226
+ assert_equal("foo foo foo", File.new(@article.upload.path).read)
227
+ end
228
+
229
+ it "should build the object's subobjects properly" do
230
+ assert_equal(3, @article.comments.length)
231
+ assert_equal("comment 1", @article.comments.first.text)
232
+ assert_equal("comment 2", @article.comments.second.text)
233
+ end
234
+
235
+ it "should build the subobjects' files properly" do
236
+ contents1 = File.new(@article.comments.first.upload.path).read
237
+ contents2 = File.new(@article.comments.second.upload.path).read
238
+ assert_equal("foo foo foo", contents1)
239
+ assert_equal("foo foo foo", contents2)
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ describe "for a saved object" do
246
+ before do
247
+ @article.save!
248
+ @article.text = "flappa flap flap"
249
+ @draft = @article.save_draft
250
+ @article = @draft.build_draftable
251
+ end
252
+
253
+ it "should build the object's attributes properly" do
254
+ assert_equal("flappa flap flap", @article.text)
255
+ end
256
+
257
+ it "should build the object's files properly" do
258
+ assert_equal(file_upload.size, File.new(@article.upload.path).size)
259
+ end
260
+
261
+ it "should build the object's subobjects properly" do
262
+ assert_equal(2, @article.comments.length)
263
+ assert_equal("comment 1", @article.comments.first.text)
264
+ assert_equal("comment 2", @article.comments.second.text)
265
+ end
266
+
267
+ it "should build the subobjects' files properly" do
268
+ contents1 = File.new(@article.comments.first.upload.path).read
269
+ contents2 = File.new(@article.comments.second.upload.path).read
270
+ assert_equal("foo foo foo", contents1)
271
+ assert_equal("foo foo foo", contents2)
272
+ end
273
+ end
274
+ end
275
+
104
276
  describe "Rejecting a draft" do
105
277
  before do
106
278
  @article = Article.new(
@@ -11,28 +11,14 @@ class TestDraftable < MiniTest::Unit::TestCase
11
11
  end
12
12
  end
13
13
 
14
- describe "without a :draft_title option" do
15
- describe "instantiating" do
16
- it "works" do
17
- assert Post.new
18
- end
19
- end
20
- end
21
-
22
14
  it "should know it's draftable" do
23
15
  assert Article.draftable?
24
16
  end
25
-
26
- describe "with a :draft_title argument" do
27
- it "should know its :draft_title field" do
28
- assert_equal(:text, Article.draftable_draft_title)
29
- end
30
- end
31
17
  end
32
18
 
33
19
  describe "A non-draftable ActiveRecord class" do
34
20
  it "should know it's not draftable" do
35
- refute Comment.draftable?
21
+ refute User.draftable?
36
22
  end
37
23
  end
38
24
 
@@ -0,0 +1,79 @@
1
+ require 'helper'
2
+
3
+ describe "Conditional id_hash output" do
4
+ describe "for an unapproved object" do
5
+ describe "with a draft attached" do
6
+ before do
7
+ @article = Article.new(:text => "I'm an article.")
8
+ @draft = @article.save_draft
9
+ end
10
+
11
+ it "should give back a :draft_id" do
12
+ assert_equal({:draft_id => @draft.to_param}, @article.id_hash)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "for a persisted object" do
18
+ describe "with a draft attached" do
19
+ before do
20
+ @article = Article.create(:text => "I'm an article.")
21
+ @article.text = "I am now an article with a draft"
22
+ @article.save_draft
23
+ end
24
+
25
+ it "should give back the article's :id in a Hash" do
26
+ assert_equal({:id => @article.to_param}, @article.id_hash)
27
+ end
28
+ end
29
+
30
+ describe "with no draft attached" do
31
+ before do
32
+ @article = Article.create(:text => "I'm an article.")
33
+ end
34
+
35
+ it "should give back the article's id in a Hash" do
36
+ assert_equal({:id => @article.to_param}, @article.id_hash)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "Conditional id_hash_as(:foo) output" do
43
+ describe "for an unapproved object" do
44
+ describe "with a draft attached" do
45
+ before do
46
+ @article = Article.new(:text => "I'm an article.")
47
+ @draft = @article.save_draft
48
+ end
49
+
50
+ it "should give back a :draft_foo_id" do
51
+ assert_equal({:draft_foo_id => @draft.to_param}, @article.id_hash_as(:foo))
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "for a persisted object" do
57
+ describe "with a draft attached" do
58
+ before do
59
+ @article = Article.create(:text => "I'm an article.")
60
+ @article.text = "I am now an article with a draft"
61
+ @article.save_draft
62
+ end
63
+
64
+ it "should give back a :foo_id in a Hash" do
65
+ assert_equal({:foo_id => @article.to_param}, @article.id_hash_as(:foo))
66
+ end
67
+ end
68
+
69
+ describe "with no draft attached" do
70
+ before do
71
+ @article = Article.create(:text => "I'm an article.")
72
+ end
73
+
74
+ it "should give back the article's :foo_id in a Hash" do
75
+ assert_equal({:foo_id => @article.to_param}, @article.id_hash_as(:foo))
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,178 @@
1
+ require 'helper'
2
+
3
+ class TestSubdrafts < Minitest::Unit::TestCase
4
+
5
+ describe "Saving a draft for an article" do
6
+ before do
7
+ @article = Article.new(:text => "I'm an article.")
8
+ @article_count = Article.count
9
+ @draft_count = Draft.count
10
+ end
11
+
12
+ describe "and attaching a comment" do
13
+ before do
14
+ @comment_count = Comment.count
15
+ @draft_upload_count = DraftUpload.count
16
+ @article.comments << Comment.new(:text => "What a great article!", :upload => file_upload)
17
+ @draft = @article.save_draft
18
+ @article_draft = Draft.first
19
+ @comment_draft = Draft.last
20
+ end
21
+
22
+ it "should not create a new comment" do
23
+ assert_equal(@comment_count, Comment.count)
24
+ end
25
+
26
+ it "should not create a new article" do
27
+ assert_equal(@article_count, Article.count)
28
+ end
29
+
30
+ it "should save drafts for the article and comment" do
31
+ assert_equal(@draft_count + 2, Draft.count)
32
+ end
33
+
34
+ it "should serialize the comment draft fields properly" do
35
+ assert @comment_draft.build_draftable.is_a? Comment
36
+ assert_equal("What a great article!", @comment_draft.text)
37
+ end
38
+
39
+ it "should save a draft upload for the comment's uploaded file" do
40
+ assert_equal(@draft_upload_count + 1, DraftUpload.count)
41
+ assert_equal(file_upload.size, File.new(@comment_draft.build_draftable.upload.path).size)
42
+ end
43
+
44
+ it "should correctly associate the comment draft with its parent" do
45
+ assert_equal(@article_draft, @comment_draft.parent)
46
+ end
47
+
48
+ describe "after calling build_draftable on a draft" do
49
+ before do
50
+ @article = @draft.build_draftable
51
+ end
52
+
53
+ it "should still have 1 comment on it" do
54
+ assert_equal(1, @article.comments.length)
55
+ end
56
+
57
+ describe "restoring the draft" do
58
+ it "should work" do
59
+ assert (@article = @draft.build_draftable).is_a? Article
60
+ end
61
+ end
62
+
63
+ describe "and adding another couple of comments" do
64
+ before do
65
+ @article.comments << Comment.new(:text => "SuperComment")
66
+ @draft = @article.save_draft
67
+ @article = @draft.build_draftable
68
+ @article.comments << Comment.new(:text => "AnotherComment")
69
+ end
70
+
71
+ it "should have 3 comments" do
72
+ assert_equal(3, @article.comments.length)
73
+ assert_equal("SuperComment", @article.comments.second.text)
74
+ assert_equal("AnotherComment", @article.comments.third.text)
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "and attaching a @like, which is a polymorphic sub-object" do
81
+ before do
82
+ @like_count = Like.count
83
+ @article.save_draft
84
+ @like = Like.new(:likeable => @article)
85
+ @draft = @like.save_draft(@article.draft, :likes)
86
+ @article_draft = Draft.first
87
+ @like_draft = Draft.last
88
+ end
89
+
90
+ it "should save 2 new drafts" do
91
+ assert_equal(@draft_count + 2, Draft.count)
92
+ end
93
+
94
+ describe "restoring the like subdraft to the article" do
95
+ before do
96
+ @article = @article_draft.build_draftable
97
+ end
98
+
99
+ it "should work" do
100
+ assert_equal(1, @article.likes.length)
101
+ end
102
+ end
103
+
104
+ describe "approving the article (so we start from the top of the chain)" do
105
+ before do
106
+ @draft_count = Draft.count
107
+ @article_count = Article.count
108
+ @like_count = Like.count
109
+ @article_draft.approve!
110
+ end
111
+
112
+ it "should destroy the draft objects" do
113
+ assert_equal(@draft_count - 2, Draft.count)
114
+ end
115
+
116
+ it "should create a new article" do
117
+ assert_equal(@article_count + 1, Article.count)
118
+ end
119
+
120
+ it "should create a new Like" do
121
+ assert_equal(@like_count + 1, Like.count)
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ describe "and attaching a polymorphic sub-object with STI" do
128
+ before do
129
+ @really_like_count = ReallyLike.count
130
+ @article.save_draft
131
+ @really_like = ReallyLike.new(:likeable => @article)
132
+ @draft = @really_like.save_draft(@article.draft, :likes)
133
+ @article_draft = Draft.first
134
+ @really_like_draft = Draft.last
135
+ end
136
+
137
+ it "should save 2 new drafts" do
138
+ assert_equal(@draft_count + 2, Draft.count)
139
+ end
140
+
141
+ describe "restoring the like subdraft to the article" do
142
+ before do
143
+ @article = @article_draft.build_draftable
144
+ end
145
+
146
+ it "should work, using the parent class association" do
147
+ assert_equal(1, @article.likes.length)
148
+ end
149
+ end
150
+
151
+ describe "approving the article (so we start from the top of the chain)" do
152
+ before do
153
+ @draft_count = Draft.count
154
+ @article_count = Article.count
155
+ @like_count = Like.count
156
+ @really_like_count = ReallyLike.count
157
+ @article_draft.approve!
158
+ end
159
+
160
+ it "should destroy the draft objects" do
161
+ assert_equal(@draft_count - 2, Draft.count)
162
+ end
163
+
164
+ it "should create a new article" do
165
+ assert_equal(@article_count + 1, Article.count)
166
+ end
167
+
168
+ it "should create a new Like" do
169
+ assert_equal(@like_count + 1, Like.count)
170
+ end
171
+
172
+ it "should create a new ReallyLike" do
173
+ assert_equal(@really_like_count + 1, ReallyLike.count)
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
data/test/helper.rb CHANGED
@@ -9,6 +9,7 @@ rescue Bundler::BundlerError => e
9
9
  end
10
10
  require 'minitest/spec'
11
11
  require 'debugger'
12
+ require 'database_cleaner'
12
13
 
13
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
14
15
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -32,7 +33,17 @@ load File.dirname(__FILE__) + '/support/uploader.rb'
32
33
  load File.dirname(__FILE__) + '/support/models.rb'
33
34
  load File.dirname(__FILE__) + '/support/data.rb'
34
35
 
35
- class MiniTest::Unit::TestCase
36
+ DatabaseCleaner.strategy = :transaction
37
+
38
+ class MiniTest::Spec
39
+
40
+ before :each do
41
+ DatabaseCleaner.start
42
+ end
43
+
44
+ after :each do
45
+ DatabaseCleaner.clean
46
+ end
36
47
 
37
48
  # Pull in shoulda matchers for minitest.
38
49
  #
@@ -49,10 +60,4 @@ class MiniTest::Unit::TestCase
49
60
 
50
61
  end
51
62
 
52
- MiniTest::Unit.autorun
53
-
54
- # Turn.config do |c|
55
- # c.format = :outline
56
- # c.trace = false
57
- # c.natural = true
58
- # end
63
+ MiniTest::Unit.autorun
@@ -2,22 +2,48 @@
2
2
 
3
3
  # A draftable article class
4
4
  class Article < ActiveRecord::Base
5
+ draftable
6
+ approves_drafts_for :comments
7
+
8
+ has_many :comments
9
+ has_many :likes, :as => :likeable
10
+ has_many :really_likes
5
11
 
6
12
  validates_presence_of :text
13
+ mount_uploader :upload, Uploader
14
+ end
7
15
 
8
- draftable :draft_title => :text
16
+ # A draftable comment class which belongs to Article.
17
+ #
18
+ # We can use this to test out has_many associations.
19
+ class Comment < ActiveRecord::Base
20
+ draftable
9
21
 
10
- mount_uploader :upload, Uploader
22
+ belongs_to :article
23
+ has_many :likes, :as => :likeable
24
+
25
+ mount_uploader :upload, Uploader
11
26
 
12
27
  end
13
28
 
14
- class Post < ActiveRecord::Base
29
+ # A class which allows us to test whether we can restore a polymorphic
30
+ # has_many relationship.
31
+ class Like < ActiveRecord::Base
15
32
 
16
- draftable
33
+ draftable(:polymorphic_as => :likeable)
34
+
35
+ belongs_to :likeable, :polymorphic => true
36
+
37
+ validates_presence_of :likeable
17
38
 
18
39
  end
19
40
 
41
+ # A class which allows us to test whether we can restore a polymorphic
42
+ # has_many relationship with STI involved.
43
+ class ReallyLike < Like
20
44
 
21
- # A non-draftable comment class
22
- class Comment < ActiveRecord::Base
45
+ end
46
+
47
+ # A non-draftable class
48
+ class User < ActiveRecord::Base
23
49
  end
@@ -2,20 +2,12 @@
2
2
  ActiveRecord::Schema.define do
3
3
  self.verbose = false
4
4
 
5
- create_table :articles, :force => true do |t|
6
- t.string :text
7
- t.string :upload
8
- t.timestamps
9
- end
10
-
11
- create_table :posts, :force => true do |t|
12
- t.string :text
13
- t.timestamps
14
- end
15
-
5
+ # These ones are the tables that the library really relies on.
16
6
  create_table :drafts, :force => true do |t|
17
- t.text :data
18
- t.references :draftable, :polymorphic => true
7
+ t.integer :parent_id
8
+ t.string :parent_association_name
9
+ t.text :data
10
+ t.references :draftable, :polymorphic => true
19
11
  end
20
12
 
21
13
  change_table :drafts do |t|
@@ -29,8 +21,26 @@ ActiveRecord::Schema.define do
29
21
  t.string :file_data
30
22
  end
31
23
 
24
+ # From here down, we use them for testing purposes only.
25
+ create_table :articles, :force => true do |t|
26
+ t.string :text
27
+ t.string :upload
28
+ t.timestamps
29
+ end
30
+
31
+ create_table :users, :force => true do |t|
32
+ t.string :email
33
+ t.timestamps
34
+ end
35
+
32
36
  create_table :comments, :force => true do |t|
37
+ t.integer :article_id
38
+ t.string :upload
33
39
  t.string :text
34
40
  end
35
41
 
42
+ create_table :likes, :force => true do |t|
43
+ t.references :likeable, :polymorphic => true
44
+ end
45
+
36
46
  end
data/test/test_drafter.rb CHANGED
@@ -10,7 +10,7 @@ class TestDrafter < MiniTest::Unit::TestCase
10
10
 
11
11
  describe "A non-draftable class" do
12
12
  it "does not respond to :save_draft" do
13
- refute Comment.new.respond_to?(:save_draft)
13
+ refute User.new.respond_to?(:save_draft)
14
14
  end
15
15
  end
16
16