drafter 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "drafter"
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["futurechimp"]
@@ -72,10 +72,53 @@ class Draft < ActiveRecord::Base
72
72
  draftabl
73
73
  end
74
74
 
75
- def approve_subdrafts
76
- subdrafts.each do |subdraft|
77
- subdraft.approve!
75
+ # It's possible to use the :delegate_approval_to => :foo option in the
76
+ # draftable class method, so that this model can be approved as part of its
77
+ # parent model. This method finds the 'approver', i.e. the Draft object which
78
+ # is the parent object of this one and for which calling the 'approve' method
79
+ # will approve this subdraft too.
80
+ #
81
+ # This is currently quite weak, in the sense that it'll screw up as soon
82
+ # as you depart from standard AR class naming conventions. It's a good
83
+ # target for future development, but I think at the moment, IAGNI.
84
+ #
85
+ # At the moment, we just return the parent if a delegate exists. This should
86
+ # do the trick for us right now, as we're not using deeply nested object
87
+ # graphs that need approvals. I've got the start of an alternate, somewhat
88
+ # stronger implementation, underneath that, but we don't have time to do up
89
+ # the general case right now.
90
+ #
91
+ # @return [Draft] approver the draft object which serves as the approval
92
+ # context for this one. Returns self or a delegated draft object.
93
+ def approver
94
+ delegate = self.inflate.class.delegate_approval_to
95
+ if delegate
96
+ parent
97
+ else
98
+ self
78
99
  end
79
100
  end
101
+ #
102
+ # Alternate implementation:
103
+ # def approver
104
+ # if delegate.nil?
105
+ # self
106
+ # elsif delegate == self.parent.draftable_type.underscore.to_sym
107
+ # self.parent
108
+ # else
109
+ # For polymorphs, if we wanted to be really tight about things, we'd
110
+ # check here to see whether the inflated parent object has a has_many
111
+ # which matches this draft's parent_association_name, and if it does,
112
+ # and that association name matches up with the draftable's class name
113
+ # (or a :class_name specified in the relation), then we can conclude
114
+ # that the parent delegation is correct. It might look like:
115
+ # if everything_matches_up <<<< do the code to match the previous paragraph
116
+ # parent
117
+ # else
118
+ # raise "It is unclear what Draft object you're looking for. Please ensure
119
+ # that :delegate_approval_to => :foo points at a valid parent :foo"
120
+ # end
121
+ # end
122
+ # end
80
123
 
81
124
  end
@@ -11,6 +11,9 @@ module Drafter
11
11
  cattr_accessor :polymorphic_as
12
12
  self.polymorphic_as = options[:polymorphic_as]
13
13
 
14
+ cattr_accessor :delegate_approval_to
15
+ self.delegate_approval_to = options[:delegate_approval_to]
16
+
14
17
  class << self
15
18
  def draftable?
16
19
  true
@@ -166,7 +166,7 @@ class TestCreation < Minitest::Unit::TestCase
166
166
  # down the chain.
167
167
  #
168
168
  # For the moment, it should work if you save from the top of the chain.
169
- # Given the time constraints right now, let's use that for the moment.
169
+ # Given the time constraints right now, let's use that.
170
170
  #
171
171
  # describe "a class which may belongs_to something else, e.g. a Comment" do
172
172
  # describe "when no associated class is set, e.g. there's no Article" do
@@ -323,4 +323,64 @@ class TestDraft < Minitest::Unit::TestCase
323
323
  end
324
324
  end
325
325
 
326
+ describe "delegating approval" do
327
+ before do
328
+ @article = Article.new(:text => "I'm an article.")
329
+ end
330
+
331
+ describe "a non-delegated model" do
332
+ before do
333
+ @draft = @article.save_draft
334
+ end
335
+
336
+ it "should return its own draft" do
337
+ assert_equal(@draft, @draft.approver)
338
+ end
339
+ end
340
+
341
+ describe "to a parent model, when the sub-draft is a draft for" do
342
+ describe "just a regular model" do
343
+ before do
344
+ @comment = Comment.new
345
+ @article.comments << @comment
346
+ @article.save_draft
347
+ @article_draft = Draft.first
348
+ @comment_draft = Draft.last
349
+ end
350
+
351
+ it "should return the parent" do
352
+ assert_equal(@article_draft, @comment_draft.approver)
353
+ end
354
+ end
355
+
356
+ describe "a polymorphic model" do
357
+ before do
358
+ @article.save_draft
359
+ @like = Like.new(:likeable => @article) # hack for polymorph
360
+ @draft = @like.save_draft(@article.draft, :likes)
361
+ @article_draft = Draft.first
362
+ @like_draft = Draft.last
363
+ end
364
+
365
+ it "should return the parent" do
366
+ assert_equal(@article_draft, @like_draft.approver)
367
+ end
368
+ end
369
+
370
+ before do
371
+ @article.save_draft
372
+ @like = ReallyLike.new(:likeable => @article) # hack for polymorph
373
+ @draft = @like.save_draft(@article.draft, :likes)
374
+ @article_draft = Draft.first
375
+ @like_draft = Draft.last
376
+ end
377
+
378
+ it "should return the parent" do
379
+ assert_equal(@article_draft, @like_draft.approver)
380
+ end
381
+ end
382
+
383
+
384
+ end
385
+
326
386
  end
@@ -81,7 +81,7 @@ class TestSubdrafts < Minitest::Unit::TestCase
81
81
  before do
82
82
  @like_count = Like.count
83
83
  @article.save_draft
84
- @like = Like.new(:likeable => @article)
84
+ @like = Like.new(:likeable => @article) # hack for polymorph
85
85
  @draft = @like.save_draft(@article.draft, :likes)
86
86
  @article_draft = Draft.first
87
87
  @like_draft = Draft.last
@@ -128,7 +128,7 @@ class TestSubdrafts < Minitest::Unit::TestCase
128
128
  before do
129
129
  @really_like_count = ReallyLike.count
130
130
  @article.save_draft
131
- @really_like = ReallyLike.new(:likeable => @article)
131
+ @really_like = ReallyLike.new(:likeable => @article) # hack for polymorph
132
132
  @draft = @really_like.save_draft(@article.draft, :likes)
133
133
  @article_draft = Draft.first
134
134
  @really_like_draft = Draft.last
@@ -17,7 +17,7 @@ end
17
17
  #
18
18
  # We can use this to test out has_many associations.
19
19
  class Comment < ActiveRecord::Base
20
- draftable
20
+ draftable :delegate_approval_to => :article
21
21
 
22
22
  belongs_to :article
23
23
  has_many :likes, :as => :likeable
@@ -30,7 +30,7 @@ end
30
30
  # has_many relationship.
31
31
  class Like < ActiveRecord::Base
32
32
 
33
- draftable(:polymorphic_as => :likeable)
33
+ draftable(:polymorphic_as => :likeable, :delegate_approval_to => :likeable)
34
34
 
35
35
  belongs_to :likeable, :polymorphic => true
36
36
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drafter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-09-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2168697080 !ruby/object:Gem::Requirement
16
+ requirement: &2155591120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2168697080
24
+ version_requirements: *2155591120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: diffy
27
- requirement: &2168689840 !ruby/object:Gem::Requirement
27
+ requirement: &2155587140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2168689840
35
+ version_requirements: *2155587140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &2168688720 !ruby/object:Gem::Requirement
38
+ requirement: &2155586280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2168688720
46
+ version_requirements: *2155586280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: database_cleaner
49
- requirement: &2168687540 !ruby/object:Gem::Requirement
49
+ requirement: &2155585300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2168687540
57
+ version_requirements: *2155585300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: debugger
60
- requirement: &2168686500 !ruby/object:Gem::Requirement
60
+ requirement: &2155583900 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2168686500
68
+ version_requirements: *2155583900
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest
71
- requirement: &2168685820 !ruby/object:Gem::Requirement
71
+ requirement: &2155583160 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2168685820
79
+ version_requirements: *2155583160
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &2168684900 !ruby/object:Gem::Requirement
82
+ requirement: &2155582360 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2168684900
90
+ version_requirements: *2155582360
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &2168684240 !ruby/object:Gem::Requirement
93
+ requirement: &2155581640 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 1.6.4
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2168684240
101
+ version_requirements: *2155581640
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rcov
104
- requirement: &2168683540 !ruby/object:Gem::Requirement
104
+ requirement: &2155580740 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2168683540
112
+ version_requirements: *2155580740
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: turn
115
- requirement: &2168682640 !ruby/object:Gem::Requirement
115
+ requirement: &2155579900 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *2168682640
123
+ version_requirements: *2155579900
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: carrierwave
126
- requirement: &2168676860 !ruby/object:Gem::Requirement
126
+ requirement: &2155572600 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *2168676860
134
+ version_requirements: *2155572600
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: minitest-rails-shoulda
137
- requirement: &2168676200 !ruby/object:Gem::Requirement
137
+ requirement: &2155571900 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,7 +142,7 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *2168676200
145
+ version_requirements: *2155571900
146
146
  description: A
147
147
  email: dave.hrycyszyn@headlondon.com
148
148
  executables: []
@@ -201,7 +201,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  segments:
203
203
  - 0
204
- hash: 1334542519238778841
204
+ hash: 1555687896561378054
205
205
  required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  none: false
207
207
  requirements: