undercarriage 0.5.7 → 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.
@@ -10,23 +10,31 @@ module Undercarriage
10
10
  ##
11
11
  # Base restful action
12
12
  #
13
- # Usage
13
+ # BaseConcern is not meant to be included alone
14
+ #
15
+ # @example Controller
14
16
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::Restful::Actions::BaseConcern
17
+ # include Undercarriage::Controllers::RestfulConcern
16
18
  # end
17
- #
18
19
  module BaseConcern
19
20
  extend ActiveSupport::Concern
20
21
 
22
+ included do
23
+ include Undercarriage::Controllers::Restful::FlashConcern
24
+ include Undercarriage::Controllers::Restful::LocationAfterConcern
25
+ include Undercarriage::Controllers::Restful::NamespaceConcern
26
+ include Undercarriage::Controllers::Restful::PermittedAttributesConcern
27
+ include Undercarriage::Controllers::Restful::UtilityConcern
28
+ end
29
+
21
30
  protected
22
31
 
23
32
  ##
24
- # New content action
25
- #
26
- # Decide what content to load based on action name
33
+ # Resource scope
27
34
  #
28
- def resource_new_content
29
- action_name == 'new' ? new_resource_content : create_resource_content
35
+ # @return [Object] resource class scope
36
+ def resource_scope
37
+ model_class
30
38
  end
31
39
 
32
40
  ##
@@ -34,7 +42,7 @@ module Undercarriage
34
42
  #
35
43
  # Used for `show`, `edit`, `update` and `destroy` actions unless overwritten
36
44
  #
37
- # Usage
45
+ # @example Controller
38
46
  # class ExamplesController < ApplicationController
39
47
  # include Undercarriage::Controllers::RestfulConcern
40
48
  #
@@ -54,13 +62,52 @@ module Undercarriage
54
62
  # #
55
63
  # # authorize @example
56
64
  # # end
57
- # end
58
65
  #
66
+ # ##
67
+ # # To override only a single action's query, override its `*_content` hook instead of `resource_content`
68
+ # # itself. The other actions keep using `resource_content`
69
+ # #
70
+ # # def show_content
71
+ # # super
72
+ # #
73
+ # # authorize @example
74
+ # # end
75
+ # end
59
76
  def resource_content
60
77
  resource_id = params.fetch(:id)
61
- resource_query = model_class.find(resource_id)
78
+ resource_scope.find(resource_id)
79
+ end
62
80
 
63
- instance_variable_set("@#{instance_name}", resource_query)
81
+ ##
82
+ # Resource content for the `destroy` action
83
+ #
84
+ # @return [Object,Hash]
85
+ def destroy_content
86
+ resource_content
87
+ end
88
+
89
+ ##
90
+ # Resource content for the `edit` action
91
+ #
92
+ # @return [Object,Hash]
93
+ def edit_content
94
+ resource_content
95
+ end
96
+
97
+ ##
98
+ # Resource content for the `show` action
99
+ #
100
+ # @return [Object,Hash]
101
+ def show_content
102
+ resource_content
103
+ end
104
+
105
+ ##
106
+ # Resource content for the `update` action
107
+ #
108
+ # @return [Object,Hash]
109
+ def update_content
110
+ resource_content
64
111
  end
65
112
 
66
113
  ##
@@ -69,11 +116,10 @@ module Undercarriage
69
116
  # Called first thing from `new`, `create`, `edit` and `update` actions. Meant to build a basic resource before
70
117
  # the action is evaluated
71
118
  #
72
- # Usage
73
- # nested_resource_pre_build
119
+ # @example Controller
120
+ # def nested_resource_pre_build
74
121
  # @example.build_image if @example.image.blank?
75
122
  # end
76
- #
77
123
  def nested_resource_pre_build; end
78
124
 
79
125
  ##
@@ -83,11 +129,10 @@ module Undercarriage
83
129
  # is called right after `nested_resource_pre_build`. For the `create` and `update` actions, it is only called
84
130
  # after validation has failed and before the view is rendered.
85
131
  #
86
- # Usage
87
- # nested_resource_build
132
+ # @example Controller
133
+ # def nested_resource_build
88
134
  # @example.build_image if @example.image.blank?
89
135
  # end
90
- #
91
136
  def nested_resource_build; end
92
137
 
93
138
  ##
@@ -95,11 +140,10 @@ module Undercarriage
95
140
  #
96
141
  # Callback after `create` action has created the record.
97
142
  #
98
- # Usage
99
- # after_create_action
143
+ # @example Controller
144
+ # def after_create_action
100
145
  # ExampleJob.perform_later(@example.id)
101
146
  # end
102
- #
103
147
  def after_create_action; end
104
148
 
105
149
  ##
@@ -107,12 +151,23 @@ module Undercarriage
107
151
  #
108
152
  # Callback after `update` action has updated the record.
109
153
  #
110
- # Usage
111
- # after_update_action
154
+ # @example Controller
155
+ # def after_update_action
112
156
  # ExampleJob.perform_later(@example.id)
113
157
  # end
114
- #
115
158
  def after_update_action; end
159
+
160
+ ##
161
+ # Unprocessable status
162
+ #
163
+ # Rack 3.1 (bundled with Rails >= 8.0) renamed the RFC 9110-aligned `422` status symbol from
164
+ # `:unprocessable_entity` (now deprecated) to `:unprocessable_content`. Rails < 8.0 doesn't know the new
165
+ # name, so pick whichever symbol the currently loaded Rails understands.
166
+ #
167
+ # @return [Symbol] unprocessable status type
168
+ def unprocessable_status
169
+ Rails::VERSION::MAJOR >= 8 ? :unprocessable_content : :unprocessable_entity
170
+ end
116
171
  end
117
172
  end
118
173
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Create restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::CreateConcern
16
16
  # end
17
- #
18
17
  module CreateConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :create_resource, only: %i[create]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Create action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def create
42
42
  nested_resource_pre_build
43
43
 
@@ -54,18 +54,26 @@ module Undercarriage
54
54
  else
55
55
  nested_resource_build
56
56
 
57
- format.html { render :new, status: :unprocessable_entity }
58
- format.json { render json: @create_resource.errors, status: :unprocessable_entity }
57
+ format.html { render :new, status: unprocessable_status }
58
+ format.json { render json: @create_resource.errors, status: unprocessable_status }
59
59
  end
60
60
  end
61
61
  end
62
62
 
63
63
  protected
64
64
 
65
+ ##
66
+ # Create resource content
67
+ #
68
+ # @return [Object] the built resource
69
+ def create_content
70
+ resource_scope.new(create_resource_params)
71
+ end
72
+
65
73
  ##
66
74
  # Create restful action
67
75
  #
68
- # Usage
76
+ # @example Controller
69
77
  # class ExamplesController < ApplicationController
70
78
  # include Undercarriage::Controllers::RestfulConcern
71
79
  #
@@ -87,24 +95,30 @@ module Undercarriage
87
95
  # # end
88
96
  #
89
97
  # ##
90
- # # The `resource_new_content` method can also be overwritten. This method is meant to share content with
91
- # # the `new` action
98
+ # # To change the underlying build without touching instance variable assignment, override
99
+ # # `create_content` instead. Note this is independent from `new_content` (`create` no longer shares
100
+ # # this method with `new`)
92
101
  # #
93
- # # def resource_new_content
102
+ # # def create_content
94
103
  # # ...
95
104
  # # end
96
105
  # end
97
- #
98
106
  def create_resource_content
99
- resource_query = model_class.new(create_resource_params)
107
+ resource_query = create_content
100
108
 
101
109
  instance_variable_set("@#{instance_name}", resource_query)
102
110
  end
103
111
 
104
112
  private
105
113
 
114
+ ##
115
+ # Create resource before_action callback
116
+ #
117
+ # Memoizes the built resource into `@create_resource` ahead of the `create` action.
118
+ #
119
+ # @return [Object] the built resource
106
120
  def create_resource
107
- @create_resource ||= resource_new_content
121
+ @create_resource ||= create_resource_content
108
122
  end
109
123
  end
110
124
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Destroy restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::DestroyConcern
16
16
  # end
17
- #
18
17
  module DestroyConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :destroy_resource, only: %i[destroy]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Destroy action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def destroy
42
42
  @destroy_resource.destroy
43
43
 
@@ -56,7 +56,7 @@ module Undercarriage
56
56
  ##
57
57
  # Destroy restful action
58
58
  #
59
- # Usage
59
+ # @example Controller
60
60
  # class ExamplesController < ApplicationController
61
61
  # include Undercarriage::Controllers::RestfulConcern
62
62
  #
@@ -84,14 +84,27 @@ module Undercarriage
84
84
  # # def resource_content
85
85
  # # ...
86
86
  # # end
87
- # end
88
87
  #
88
+ # ##
89
+ # # To change the query for `destroy` only, override `destroy_content` instead. The `show`, `edit` and
90
+ # # `update` actions are unaffected
91
+ # #
92
+ # # def destroy_content
93
+ # # ...
94
+ # # end
95
+ # end
89
96
  def destroy_resource_content
90
- resource_content
97
+ instance_variable_set("@#{instance_name}", destroy_content)
91
98
  end
92
99
 
93
100
  private
94
101
 
102
+ ##
103
+ # Destroy resource before_action callback
104
+ #
105
+ # Memoizes the resource to be destroyed into `@destroy_resource` ahead of the `destroy` action.
106
+ #
107
+ # @return [Object] the resource to destroy
95
108
  def destroy_resource
96
109
  @destroy_resource ||= destroy_resource_content
97
110
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Edit restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::EditConcern
16
16
  # end
17
- #
18
17
  module EditConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :edit_resource, only: %i[edit]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Edit action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def edit
42
42
  nested_resource_pre_build
43
43
  nested_resource_build
@@ -48,7 +48,7 @@ module Undercarriage
48
48
  ##
49
49
  # Edit restful action
50
50
  #
51
- # Usage
51
+ # @example Controller
52
52
  # class ExamplesController < ApplicationController
53
53
  # include Undercarriage::Controllers::RestfulConcern
54
54
  #
@@ -76,14 +76,27 @@ module Undercarriage
76
76
  # # def resource_content
77
77
  # # ...
78
78
  # # end
79
- # end
80
79
  #
80
+ # ##
81
+ # # To change the query for `edit` only, override `edit_content` instead. The `show`, `update` and
82
+ # # `destroy` actions are unaffected
83
+ # #
84
+ # # def edit_content
85
+ # # ...
86
+ # # end
87
+ # end
81
88
  def edit_resource_content
82
- resource_content
89
+ instance_variable_set("@#{instance_name}", edit_content)
83
90
  end
84
91
 
85
92
  private
86
93
 
94
+ ##
95
+ # Edit resource before_action callback
96
+ #
97
+ # Memoizes the resource to be edited into `@edit_resource` ahead of the `edit` action.
98
+ #
99
+ # @return [Object] the resource to edit
87
100
  def edit_resource
88
101
  @edit_resource ||= edit_resource_content
89
102
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Index restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::IndexConcern
16
16
  # end
17
- #
18
17
  module IndexConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :index_resources, only: %i[index]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Index action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,15 +38,22 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def index; end
42
42
 
43
43
  protected
44
44
 
45
+ ##
46
+ # Index resource content
47
+ #
48
+ # @return [Object] resource collection scope
49
+ def index_content
50
+ resource_scope.all
51
+ end
52
+
45
53
  ##
46
54
  # Index restful action
47
55
  #
48
- # Usage
56
+ # @example Controller
49
57
  # class ExamplesController < ApplicationController
50
58
  # include Undercarriage::Controllers::RestfulConcern
51
59
  #
@@ -65,16 +73,29 @@ module Undercarriage
65
73
  # #
66
74
  # # authorize @examples
67
75
  # # end
68
- # end
69
76
  #
77
+ # ##
78
+ # # To change the underlying query without touching instance variable assignment, override
79
+ # # `index_content` instead
80
+ # #
81
+ # # def index_content
82
+ # # ...
83
+ # # end
84
+ # end
70
85
  def resources_content
71
- resources_query = model_class.all
86
+ resources_query = index_content
72
87
 
73
88
  instance_variable_set("@#{instances_name}", resources_query)
74
89
  end
75
90
 
76
91
  private
77
92
 
93
+ ##
94
+ # Index resources before_action callback
95
+ #
96
+ # Memoizes the resource collection into `@index_resources` ahead of the `index` action.
97
+ #
98
+ # @return [Object] the resource collection
78
99
  def index_resources
79
100
  @index_resources ||= resources_content
80
101
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # New restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::NewConcern
16
16
  # end
17
- #
18
17
  module NewConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :new_resource, only: %i[new]
23
24
  end
24
25
 
25
26
  ##
26
27
  # New action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def new
42
42
  nested_resource_pre_build
43
43
  nested_resource_build
@@ -45,10 +45,18 @@ module Undercarriage
45
45
 
46
46
  protected
47
47
 
48
+ ##
49
+ # New resource content
50
+ #
51
+ # @return [Object] the built resource
52
+ def new_content
53
+ resource_scope.new
54
+ end
55
+
48
56
  ##
49
57
  # New restful action
50
58
  #
51
- # Usage
59
+ # @example Controller
52
60
  # class ExamplesController < ApplicationController
53
61
  # include Undercarriage::Controllers::RestfulConcern
54
62
  #
@@ -70,24 +78,30 @@ module Undercarriage
70
78
  # # end
71
79
  #
72
80
  # ##
73
- # # The `resource_new_content` method can also be overwritten. This method is meant to share content with
74
- # # the `create` action
81
+ # # To change the underlying build without touching instance variable assignment, override
82
+ # # `new_content` instead. Note this is independent from `create_content` (`create` no longer shares
83
+ # # this method with `new`)
75
84
  # #
76
- # # def resource_new_content
85
+ # # def new_content
77
86
  # # ...
78
87
  # # end
79
88
  # end
80
- #
81
89
  def new_resource_content
82
- resource_query = model_class.new
90
+ resource_query = new_content
83
91
 
84
92
  instance_variable_set("@#{instance_name}", resource_query)
85
93
  end
86
94
 
87
95
  private
88
96
 
97
+ ##
98
+ # New resource before_action callback
99
+ #
100
+ # Memoizes the built resource into `@new_resource` ahead of the `new` action.
101
+ #
102
+ # @return [Object] the built resource
89
103
  def new_resource
90
- @new_resource ||= resource_new_content
104
+ @new_resource ||= new_resource_content
91
105
  end
92
106
  end
93
107
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Show restful action
12
12
  #
13
- # Usage
13
+ # @example Controller
14
14
  # class ExamplesController < ApplicationController
15
- # include Undercarriage::Controllers::RestfulConcern
15
+ # include Undercarriage::Controllers::Restful::Actions::ShowConcern
16
16
  # end
17
- #
18
17
  module ShowConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :show_resource, only: %i[show]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Show action
27
28
  #
28
- # Usage
29
+ # @example Controller
29
30
  # class ExamplesController < ApplicationController
30
31
  # include Undercarriage::Controllers::RestfulConcern
31
32
  #
@@ -37,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def show; end
42
42
 
43
43
  protected
@@ -45,7 +45,7 @@ module Undercarriage
45
45
  ##
46
46
  # Show restful action
47
47
  #
48
- # Usage
48
+ # @example Controller
49
49
  # class ExamplesController < ApplicationController
50
50
  # include Undercarriage::Controllers::RestfulConcern
51
51
  #
@@ -73,14 +73,27 @@ module Undercarriage
73
73
  # # def resource_content
74
74
  # # ...
75
75
  # # end
76
- # end
77
76
  #
77
+ # ##
78
+ # # To change the query for `show` only, override `show_content` instead. The `edit`, `update` and
79
+ # # `destroy` actions are unaffected
80
+ # #
81
+ # # def show_content
82
+ # # ...
83
+ # # end
84
+ # end
78
85
  def show_resource_content
79
- resource_content
86
+ instance_variable_set("@#{instance_name}", show_content)
80
87
  end
81
88
 
82
89
  private
83
90
 
91
+ ##
92
+ # Show resource before_action callback
93
+ #
94
+ # Memoizes the resource to be shown into `@show_resource` ahead of the `show` action.
95
+ #
96
+ # @return [Object] the resource to show
84
97
  def show_resource
85
98
  @show_resource ||= show_resource_content
86
99
  end