garden_variety 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e0504f9a8b485b41628697e9b5b3061e697874d
4
- data.tar.gz: 69d7067b8054ce75955a12cf47420901f4badf0b
3
+ metadata.gz: 40c8e32c8c62b56caaea0a4346859e6ae74bde2e
4
+ data.tar.gz: 44398e9e38ceaf81e1ef7a729d167b080c0f0f2e
5
5
  SHA512:
6
- metadata.gz: 3cb5e7ee8a146481586d7aa7efabdd48efa03e34617c3f9cd6e46bab3a8730210cc12d3ad9c1ec5d9d17daa675b17b8f08ae6481ac8545a8cc27f7a830b9fe40
7
- data.tar.gz: 1e02647816ee226bf0d46dc7933e74aff5f28a465abbf75d4722d2e69c287581b7b2da56cfbc7fd7fe629705cd19d8beb6eb800b8a7d7e0ff786b276287d822e
6
+ metadata.gz: e17204ddf0a4a15b2ec52af60204d0774d845d71122c41808853fa1febdb849d8c938d33ba02398fe05b41398376917a7d81de783aadf9f14b0794a4659bcc85
7
+ data.tar.gz: eb1a353562e16b894b1fd432b8dbd42fe09fc1ebb8da72d1d422e2257d013d14e98182b148d3713be379dc005b9a32f1f1e2a17bbe6d901ef3fe329fcbfd8eb6
data/README.md CHANGED
@@ -21,7 +21,7 @@ class PostsController < ApplicationController
21
21
  end
22
22
  ```
23
23
 
24
- ...expands to the following conventional implementation:
24
+ ...is equivalent to the following conventional implementation:
25
25
 
26
26
  ```ruby
27
27
  class PostsController < ApplicationController
@@ -67,7 +67,7 @@ class PostsController < ApplicationController
67
67
  def destroy
68
68
  self.resource = find_resource
69
69
  authorize(resource)
70
- resource.destroy
70
+ resource.destroy!
71
71
  redirect_to action: :index
72
72
  end
73
73
 
@@ -305,6 +305,27 @@ conventional. The *garden_variety* helper methods all work as expected
305
305
  because `RegistrationForm` responds to `assign_attributes` and `save`,
306
306
  and has a default (nullary) constructor.
307
307
 
308
+ This pattern of overriding an action merely to respond differently upon
309
+ success is common enough that *garden_variety* provides a concise syntax
310
+ for it:
311
+
312
+ ```ruby
313
+ class RegistrationFormsController < ApplicationController
314
+ garden_variety :new, :create
315
+
316
+ def create
317
+ super{ redirect_to root_path }
318
+ end
319
+ end
320
+ ```
321
+
322
+ Note here the `garden_variety` macro generates a default `create`
323
+ action, which is then invoked with `super` in the `create` override.
324
+ When a block is given to the default `create` action, the block is
325
+ treated as an on-success callback which replaces the default redirect.
326
+ This callback behavior is also available for the `update` and `destroy`
327
+ actions.
328
+
308
329
 
309
330
  ### Non-REST actions
310
331
 
@@ -29,11 +29,14 @@ module GardenVariety
29
29
 
30
30
  module CreateAction
31
31
  # Garden variety controller +create+ action.
32
+ # @overload create()
33
+ # @overload create()
34
+ # @yield on-success callback, replaces default redirect
32
35
  # @return [void]
33
36
  def create
34
37
  self.resource = vest(new_resource)
35
38
  if resource.save
36
- redirect_to resource
39
+ block_given? ? yield : redirect_to(resource)
37
40
  else
38
41
  render :new
39
42
  end
@@ -51,11 +54,14 @@ module GardenVariety
51
54
 
52
55
  module UpdateAction
53
56
  # Garden variety controller +update+ action.
57
+ # @overload update()
58
+ # @overload update()
59
+ # @yield on-success callback, replaces default redirect
54
60
  # @return [void]
55
61
  def update
56
62
  self.resource = vest(find_resource)
57
63
  if resource.save
58
- redirect_to resource
64
+ block_given? ? yield : redirect_to(resource)
59
65
  else
60
66
  render :edit
61
67
  end
@@ -64,12 +70,15 @@ module GardenVariety
64
70
 
65
71
  module DestroyAction
66
72
  # Garden variety controller +destroy+ action.
73
+ # @overload destroy()
74
+ # @overload destroy()
75
+ # @yield on-success callback, replaces default redirect
67
76
  # @return [void]
68
77
  def destroy
69
78
  self.resource = find_resource
70
79
  authorize(resource)
71
- resource.destroy
72
- redirect_to action: :index
80
+ resource.destroy!
81
+ block_given? ? yield : redirect_to(action: :index)
73
82
  end
74
83
  end
75
84
 
@@ -1,3 +1,3 @@
1
1
  module GardenVariety
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garden_variety
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Hefner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-27 00:00:00.000000000 Z
11
+ date: 2017-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails