undercarriage 0.5.7 → 1.0.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,41 @@ 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
 
32
+ ##
33
+ # Resource scope
34
+ #
35
+ # @return [Object] resource class scope
36
+ def resource_scope
37
+ model_class
38
+ end
39
+
23
40
  ##
24
41
  # New content action
25
42
  #
26
43
  # Decide what content to load based on action name
27
44
  #
45
+ # @return [Object,Hash]
28
46
  def resource_new_content
29
- action_name == 'new' ? new_resource_content : create_resource_content
47
+ action_name == "new" ? new_resource_content : create_resource_content
30
48
  end
31
49
 
32
50
  ##
@@ -34,7 +52,7 @@ module Undercarriage
34
52
  #
35
53
  # Used for `show`, `edit`, `update` and `destroy` actions unless overwritten
36
54
  #
37
- # Usage
55
+ # @example Controller
38
56
  # class ExamplesController < ApplicationController
39
57
  # include Undercarriage::Controllers::RestfulConcern
40
58
  #
@@ -55,7 +73,6 @@ module Undercarriage
55
73
  # # authorize @example
56
74
  # # end
57
75
  # end
58
- #
59
76
  def resource_content
60
77
  resource_id = params.fetch(:id)
61
78
  resource_query = model_class.find(resource_id)
@@ -69,11 +86,10 @@ module Undercarriage
69
86
  # Called first thing from `new`, `create`, `edit` and `update` actions. Meant to build a basic resource before
70
87
  # the action is evaluated
71
88
  #
72
- # Usage
73
- # nested_resource_pre_build
89
+ # @example Controller
90
+ # def nested_resource_pre_build
74
91
  # @example.build_image if @example.image.blank?
75
92
  # end
76
- #
77
93
  def nested_resource_pre_build; end
78
94
 
79
95
  ##
@@ -83,11 +99,10 @@ module Undercarriage
83
99
  # is called right after `nested_resource_pre_build`. For the `create` and `update` actions, it is only called
84
100
  # after validation has failed and before the view is rendered.
85
101
  #
86
- # Usage
87
- # nested_resource_build
102
+ # @example Controller
103
+ # def nested_resource_build
88
104
  # @example.build_image if @example.image.blank?
89
105
  # end
90
- #
91
106
  def nested_resource_build; end
92
107
 
93
108
  ##
@@ -95,11 +110,10 @@ module Undercarriage
95
110
  #
96
111
  # Callback after `create` action has created the record.
97
112
  #
98
- # Usage
99
- # after_create_action
113
+ # @example Controller
114
+ # def after_create_action
100
115
  # ExampleJob.perform_later(@example.id)
101
116
  # end
102
- #
103
117
  def after_create_action; end
104
118
 
105
119
  ##
@@ -107,12 +121,23 @@ module Undercarriage
107
121
  #
108
122
  # Callback after `update` action has updated the record.
109
123
  #
110
- # Usage
111
- # after_update_action
124
+ # @example Controller
125
+ # def after_update_action
112
126
  # ExampleJob.perform_later(@example.id)
113
127
  # end
114
- #
115
128
  def after_update_action; end
129
+
130
+ ##
131
+ # Unprocessable status
132
+ #
133
+ # Rack 3.1 (bundled with Rails >= 8.0) renamed the RFC 9110-aligned `422` status symbol from
134
+ # `:unprocessable_entity` (now deprecated) to `:unprocessable_content`. Rails < 8.0 doesn't know the new
135
+ # name, so pick whichever symbol the currently loaded Rails understands.
136
+ #
137
+ # @return [Symbol] unprocessable status type
138
+ def unprocessable_status
139
+ Rails::VERSION::MAJOR >= 8 ? :unprocessable_content : :unprocessable_entity
140
+ end
116
141
  end
117
142
  end
118
143
  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,8 +54,8 @@ 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
@@ -65,7 +65,7 @@ module Undercarriage
65
65
  ##
66
66
  # Create restful action
67
67
  #
68
- # Usage
68
+ # @example Controller
69
69
  # class ExamplesController < ApplicationController
70
70
  # include Undercarriage::Controllers::RestfulConcern
71
71
  #
@@ -94,15 +94,20 @@ module Undercarriage
94
94
  # # ...
95
95
  # # end
96
96
  # end
97
- #
98
97
  def create_resource_content
99
- resource_query = model_class.new(create_resource_params)
98
+ resource_query = resource_scope.new(create_resource_params)
100
99
 
101
100
  instance_variable_set("@#{instance_name}", resource_query)
102
101
  end
103
102
 
104
103
  private
105
104
 
105
+ ##
106
+ # Create resource before_action callback
107
+ #
108
+ # Memoizes the built resource into `@create_resource` ahead of the `create` action.
109
+ #
110
+ # @return [Object] the built resource
106
111
  def create_resource
107
112
  @create_resource ||= resource_new_content
108
113
  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
  #
@@ -85,13 +85,18 @@ module Undercarriage
85
85
  # # ...
86
86
  # # end
87
87
  # end
88
- #
89
88
  def destroy_resource_content
90
89
  resource_content
91
90
  end
92
91
 
93
92
  private
94
93
 
94
+ ##
95
+ # Destroy resource before_action callback
96
+ #
97
+ # Memoizes the resource to be destroyed into `@destroy_resource` ahead of the `destroy` action.
98
+ #
99
+ # @return [Object] the resource to destroy
95
100
  def destroy_resource
96
101
  @destroy_resource ||= destroy_resource_content
97
102
  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
  #
@@ -77,13 +77,18 @@ module Undercarriage
77
77
  # # ...
78
78
  # # end
79
79
  # end
80
- #
81
80
  def edit_resource_content
82
81
  resource_content
83
82
  end
84
83
 
85
84
  private
86
85
 
86
+ ##
87
+ # Edit resource before_action callback
88
+ #
89
+ # Memoizes the resource to be edited into `@edit_resource` ahead of the `edit` action.
90
+ #
91
+ # @return [Object] the resource to edit
87
92
  def edit_resource
88
93
  @edit_resource ||= edit_resource_content
89
94
  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,7 +38,6 @@ module Undercarriage
37
38
  # # ...
38
39
  # # end
39
40
  # end
40
- #
41
41
  def index; end
42
42
 
43
43
  protected
@@ -45,7 +45,7 @@ module Undercarriage
45
45
  ##
46
46
  # Index restful action
47
47
  #
48
- # Usage
48
+ # @example Controller
49
49
  # class ExamplesController < ApplicationController
50
50
  # include Undercarriage::Controllers::RestfulConcern
51
51
  #
@@ -66,15 +66,20 @@ module Undercarriage
66
66
  # # authorize @examples
67
67
  # # end
68
68
  # end
69
- #
70
69
  def resources_content
71
- resources_query = model_class.all
70
+ resources_query = resource_scope.all
72
71
 
73
72
  instance_variable_set("@#{instances_name}", resources_query)
74
73
  end
75
74
 
76
75
  private
77
76
 
77
+ ##
78
+ # Index resources before_action callback
79
+ #
80
+ # Memoizes the resource collection into `@index_resources` ahead of the `index` action.
81
+ #
82
+ # @return [Object] the resource collection
78
83
  def index_resources
79
84
  @index_resources ||= resources_content
80
85
  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
@@ -48,7 +48,7 @@ module Undercarriage
48
48
  ##
49
49
  # New restful action
50
50
  #
51
- # Usage
51
+ # @example Controller
52
52
  # class ExamplesController < ApplicationController
53
53
  # include Undercarriage::Controllers::RestfulConcern
54
54
  #
@@ -77,15 +77,20 @@ module Undercarriage
77
77
  # # ...
78
78
  # # end
79
79
  # end
80
- #
81
80
  def new_resource_content
82
- resource_query = model_class.new
81
+ resource_query = resource_scope.new
83
82
 
84
83
  instance_variable_set("@#{instance_name}", resource_query)
85
84
  end
86
85
 
87
86
  private
88
87
 
88
+ ##
89
+ # New resource before_action callback
90
+ #
91
+ # Memoizes the built resource into `@new_resource` ahead of the `new` action.
92
+ #
93
+ # @return [Object] the built resource
89
94
  def new_resource
90
95
  @new_resource ||= resource_new_content
91
96
  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
  #
@@ -74,13 +74,18 @@ module Undercarriage
74
74
  # # ...
75
75
  # # end
76
76
  # end
77
- #
78
77
  def show_resource_content
79
78
  resource_content
80
79
  end
81
80
 
82
81
  private
83
82
 
83
+ ##
84
+ # Show resource before_action callback
85
+ #
86
+ # Memoizes the resource to be shown into `@show_resource` ahead of the `show` action.
87
+ #
88
+ # @return [Object] the resource to show
84
89
  def show_resource
85
90
  @show_resource ||= show_resource_content
86
91
  end
@@ -10,22 +10,23 @@ module Undercarriage
10
10
  ##
11
11
  # Update 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::UpdateConcern
16
16
  # end
17
- #
18
17
  module UpdateConcern
19
18
  extend ActiveSupport::Concern
20
19
 
21
20
  included do
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+
22
23
  before_action :update_resource, only: %i[update]
23
24
  end
24
25
 
25
26
  ##
26
27
  # Update 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 update
42
42
  nested_resource_pre_build
43
43
 
@@ -54,8 +54,8 @@ module Undercarriage
54
54
  else
55
55
  nested_resource_build
56
56
 
57
- format.html { render :edit }
58
- format.json { render json: @update_resource.errors, status: :unprocessable_entity }
57
+ format.html { render :edit, status: unprocessable_status }
58
+ format.json { render json: @update_resource.errors, status: unprocessable_status }
59
59
  end
60
60
  end
61
61
  end
@@ -65,7 +65,7 @@ module Undercarriage
65
65
  ##
66
66
  # Update restful action
67
67
  #
68
- # Usage
68
+ # @example Controller
69
69
  # class ExamplesController < ApplicationController
70
70
  # include Undercarriage::Controllers::RestfulConcern
71
71
  #
@@ -94,13 +94,18 @@ module Undercarriage
94
94
  # # ...
95
95
  # # end
96
96
  # end
97
- #
98
97
  def update_resource_content
99
98
  resource_content
100
99
  end
101
100
 
102
101
  private
103
102
 
103
+ ##
104
+ # Update resource before_action callback
105
+ #
106
+ # Memoizes the resource to be updated into `@update_resource` ahead of the `update` action.
107
+ #
108
+ # @return [Object] the resource to update
104
109
  def update_resource
105
110
  @update_resource ||= update_resource_content
106
111
  end