draft_punk 0.2.4 → 0.2.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4c2aca3e5f9b7e5011aba748018abc3ff2aa32f
4
- data.tar.gz: 45acfa1318c3766274aa0bc63bd7ea6650f44993
3
+ metadata.gz: 6612bf1ad663d5fc879a40d939cccb2d45c06f02
4
+ data.tar.gz: aea192899a505fecb54189e78201daad4f786a0a
5
5
  SHA512:
6
- metadata.gz: e2f337e47a5865bbed32ad1e71bb04b6edc15be4b8b561b6535f1922f9d0da3e9c6fb7b087ee1e2ac9e0d438c941a2b298f10b05348e5344eb4fef974824378e
7
- data.tar.gz: 039a98ee57ff53e83f757336d4ff6c74c54a69ff4a29c52817cf26b7e4b2f4232849218a0e04b8a899ce49a3c1056e48d75da25e84f73ce2fb9c47ca52abbc9b
6
+ metadata.gz: 5f07c766efcc41b6590ceec5ce864960ed9f49384e15fb16477f9ae47240ab1e29ce034f8505286248dada954738f8077aeac42083dc705a01632c25a097320d
7
+ data.tar.gz: 5c5cf5a7b23d16b3f83d744f91fa9596bb74ac97f33da9eef76fc82d927fcdca41cd71f59eef2adbcd42de7fe79516a45aef7ffeff13732985f60ea2072cf1f3
data/README.md CHANGED
@@ -206,6 +206,14 @@ You can access `self` (which is the DRAFT version being created), or the `tempor
206
206
  logger.warn "#{self.name} is being created from #{temporary_approved_object.class.name} ##{temporary_approved_object.id}" # outputs: DerpCorp is being created from Business #1
207
207
  end
208
208
 
209
+ ### After create callback
210
+
211
+ If you define a method on your model called `after_create_draft`, that method will be executed before the draft is created. This is useful in cases when you need a fully set-up draft to modify. For instance, after all of its associations have been set.
212
+
213
+ You can access `self` (which is the DRAFT version being created), or the `temporary_approved_object` (the original object) in this method
214
+
215
+ **Note that you are responsible for any saves needed**. draft_punk does not save again after your after_create executes
216
+
209
217
  ## Options before publishing a draft
210
218
 
211
219
  ### Before publish draft method
@@ -127,10 +127,7 @@ module DraftPunk
127
127
  target_class.amoeba do
128
128
  enable
129
129
  include_association target_class.const_get(:DRAFT_VALID_ASSOCIATIONS)
130
- customize(lambda {|live_obj, draft_obj|
131
- draft_obj.approved_version_id = live_obj.id if draft_obj.respond_to?(:approved_version_id)
132
- draft_obj.temporary_approved_object = live_obj
133
- })
130
+ customize target_class.set_approved_version_id_callback
134
131
  end
135
132
  target_class.const_set :DRAFT_PUNK_IS_SETUP, true
136
133
 
@@ -138,6 +135,17 @@ module DraftPunk
138
135
  setup_draft_association_persistance_for_children_of target_class, associations
139
136
  end
140
137
 
138
+ public ###################################################################
139
+
140
+ def set_approved_version_id_callback
141
+ lambda do |live_obj, draft_obj|
142
+ draft_obj.approved_version_id = live_obj.id if draft_obj.respond_to?(:approved_version_id)
143
+ draft_obj.temporary_approved_object = live_obj
144
+ end
145
+ end
146
+
147
+ private ###################################################################
148
+
141
149
  def setup_draft_association_persistance_for_children_of(target_class, associations=nil)
142
150
  associations = target_class.draft_target_associations unless associations
143
151
  target_reflections = associations.map do |assoc|
@@ -35,6 +35,12 @@ module DraftPunk
35
35
  def approvable_attributes
36
36
  self.attributes.keys - ["created_at"]
37
37
  end
38
+
39
+ # Evaluates after the draft is created.
40
+ # Override in your model to implement custom behavior.
41
+ def after_create_draft
42
+ end
43
+
38
44
  #############################
39
45
  # END CONFIGURABLE METHODS
40
46
  #############################
@@ -93,6 +99,7 @@ module DraftPunk
93
99
  begin
94
100
  dupe.approved_version = self
95
101
  dupe.save(validate: false)
102
+ dupe.after_create_draft
96
103
  rescue => message
97
104
  raise DraftCreationError, message
98
105
  end
@@ -112,7 +119,7 @@ module DraftPunk
112
119
  self.class.draft_target_associations.each do |assoc|
113
120
  reflection = self.class.reflect_on_association(assoc)
114
121
 
115
- reflection_is_has_many(reflection) ? @live_version.send(assoc).destroy_all : @live_version.send(assoc).destroy
122
+ reflection_is_has_many(reflection) ? @live_version.send(assoc).destroy_all : @live_version.send(assoc).try(:destroy)
116
123
 
117
124
  attribute_updates = {}
118
125
  attribute_updates[reflection.foreign_key] = @live_version.id
@@ -1,3 +1,3 @@
1
1
  module DraftPunk
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draft_punk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hodges
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-04 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amoeba