wallaby-core 0.2.11 → 0.3.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/wallaby/resources_controller.rb +20 -7
- data/app/security/ability.rb +1 -1
- data/config/routes.rb +21 -14
- data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_decorator.rb +5 -17
- data/lib/adaptors/wallaby/custom/model_finder.rb +3 -3
- data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -1
- data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +12 -5
- data/lib/authorizers/wallaby/default_authorization_provider.rb +10 -1
- data/lib/authorizers/wallaby/model_authorizer.rb +41 -16
- data/lib/authorizers/wallaby/pundit_authorization_provider.rb +22 -8
- data/lib/concerns/wallaby/application_concern.rb +41 -71
- data/lib/concerns/wallaby/authentication_concern.rb +29 -127
- data/lib/concerns/wallaby/authorizable.rb +14 -57
- data/lib/concerns/wallaby/baseable.rb +24 -57
- data/lib/concerns/wallaby/configurable.rb +416 -0
- data/lib/concerns/wallaby/decoratable.rb +29 -60
- data/lib/concerns/wallaby/engineable.rb +29 -46
- data/lib/concerns/wallaby/fieldable.rb +45 -56
- data/lib/concerns/wallaby/paginatable.rb +20 -51
- data/lib/concerns/wallaby/prefixable.rb +24 -4
- data/lib/concerns/wallaby/resourcable.rb +130 -72
- data/lib/concerns/wallaby/resources_concern.rb +205 -305
- data/lib/concerns/wallaby/servicable.rb +8 -48
- data/lib/concerns/wallaby/urlable.rb +69 -0
- data/lib/decorators/wallaby/resource_decorator.rb +91 -34
- data/lib/errors/wallaby/class_not_found.rb +1 -2
- data/lib/errors/wallaby/forbidden.rb +1 -2
- data/lib/errors/wallaby/general_error.rb +1 -1
- data/lib/errors/wallaby/invalid_error.rb +1 -2
- data/lib/errors/wallaby/method_removed.rb +5 -0
- data/lib/errors/wallaby/model_not_found.rb +1 -2
- data/lib/errors/wallaby/not_authenticated.rb +1 -2
- data/lib/errors/wallaby/not_found.rb +1 -2
- data/lib/errors/wallaby/not_implemented.rb +1 -2
- data/lib/errors/wallaby/resource_not_found.rb +1 -2
- data/lib/errors/wallaby/unprocessable_entity.rb +1 -2
- data/lib/fields/wallaby/all_fields.rb +63 -0
- data/lib/forms/wallaby/form_builder.rb +1 -1
- data/lib/generators/wallaby/engine/application_generator.rb +33 -0
- data/lib/generators/wallaby/engine/authorizer/USAGE +20 -0
- data/lib/generators/wallaby/engine/authorizer/authorizer_generator.rb +19 -0
- data/lib/generators/wallaby/engine/authorizer/templates/authorizer.rb.erb +35 -0
- data/lib/generators/wallaby/engine/controller/USAGE +20 -0
- data/lib/generators/wallaby/engine/controller/controller_generator.rb +23 -0
- data/lib/generators/wallaby/engine/controller/templates/controller.rb.erb +130 -0
- data/lib/generators/wallaby/engine/decorator/USAGE +20 -0
- data/lib/generators/wallaby/engine/decorator/decorator_generator.rb +19 -0
- data/lib/generators/wallaby/engine/decorator/templates/decorator.rb.erb +5 -0
- data/lib/generators/wallaby/engine/install/USAGE +19 -0
- data/lib/generators/wallaby/engine/install/install_generator.rb +91 -0
- data/lib/generators/wallaby/engine/install/templates/application_authorizer.rb.erb +37 -0
- data/lib/generators/wallaby/engine/install/templates/application_controller.rb.erb +173 -0
- data/lib/generators/wallaby/engine/install/templates/application_decorator.rb.erb +7 -0
- data/lib/generators/wallaby/engine/install/templates/application_paginator.rb.erb +27 -0
- data/lib/generators/wallaby/engine/install/templates/application_servicer.rb.erb +47 -0
- data/lib/generators/wallaby/engine/install/templates/initializer.rb.erb +16 -0
- data/lib/generators/wallaby/engine/paginator/USAGE +20 -0
- data/lib/generators/wallaby/engine/paginator/paginator_generator.rb +19 -0
- data/lib/generators/wallaby/engine/paginator/templates/paginator.rb.erb +25 -0
- data/lib/generators/wallaby/engine/servicer/USAGE +20 -0
- data/lib/generators/wallaby/engine/servicer/servicer_generator.rb +19 -0
- data/lib/generators/wallaby/engine/servicer/templates/servicer.rb.erb +45 -0
- data/lib/helpers/wallaby/application_helper.rb +10 -59
- data/lib/helpers/wallaby/base_helper.rb +11 -11
- data/lib/helpers/wallaby/configuration_helper.rb +36 -4
- data/lib/helpers/wallaby/form_helper.rb +1 -1
- data/lib/helpers/wallaby/index_helper.rb +19 -9
- data/lib/helpers/wallaby/links_helper.rb +18 -85
- data/lib/helpers/wallaby/resources_helper.rb +39 -7
- data/lib/helpers/wallaby/secure_helper.rb +20 -19
- data/lib/interfaces/wallaby/mode.rb +8 -8
- data/lib/interfaces/wallaby/model_authorization_provider.rb +23 -22
- data/lib/interfaces/wallaby/model_decorator.rb +36 -48
- data/lib/interfaces/wallaby/model_finder.rb +3 -3
- data/lib/interfaces/wallaby/model_pagination_provider.rb +2 -6
- data/lib/interfaces/wallaby/model_service_provider.rb +4 -4
- data/lib/paginators/wallaby/model_paginator.rb +1 -1
- data/lib/responders/wallaby/json_api_responder.rb +10 -5
- data/lib/responders/wallaby/resources_responder.rb +10 -3
- data/lib/routes/wallaby/engines/base_route.rb +78 -0
- data/lib/routes/wallaby/engines/custom_app_route.rb +92 -0
- data/lib/routes/wallaby/engines/engine_route.rb +77 -0
- data/lib/routes/wallaby/resources_router.rb +100 -45
- data/lib/servicers/wallaby/model_servicer.rb +13 -13
- data/lib/services/wallaby/authorizer_finder.rb +23 -0
- data/lib/services/wallaby/class_finder.rb +42 -0
- data/lib/services/wallaby/controller_finder.rb +29 -0
- data/lib/services/wallaby/decorator_finder.rb +34 -0
- data/lib/services/wallaby/default_models_excluder.rb +45 -0
- data/lib/services/wallaby/engine_name_finder.rb +14 -11
- data/lib/services/wallaby/engine_url_for.rb +82 -37
- data/lib/services/wallaby/fields_regulator.rb +34 -0
- data/lib/services/wallaby/map/mode_mapper.rb +5 -5
- data/lib/services/wallaby/map/model_class_mapper.rb +1 -1
- data/lib/services/wallaby/model_class_filter.rb +29 -0
- data/lib/services/wallaby/paginator_finder.rb +24 -0
- data/lib/services/wallaby/prefixes_builder.rb +49 -8
- data/lib/services/wallaby/servicer_finder.rb +31 -0
- data/lib/services/wallaby/sorting/hash_builder.rb +9 -0
- data/lib/services/wallaby/sorting/link_builder.rb +7 -10
- data/lib/services/wallaby/sorting/next_builder.rb +1 -12
- data/lib/services/wallaby/sorting/single_builder.rb +1 -1
- data/lib/support/action_dispatch/routing/mapper.rb +29 -4
- data/lib/utils/wallaby/field_utils.rb +9 -8
- data/lib/utils/wallaby/inflector.rb +94 -0
- data/lib/utils/wallaby/locale.rb +2 -2
- data/lib/utils/wallaby/module_utils.rb +3 -10
- data/lib/utils/wallaby/utils.rb +21 -14
- data/lib/wallaby/class_array.rb +18 -13
- data/lib/wallaby/class_hash.rb +16 -14
- data/lib/wallaby/classifier.rb +4 -2
- data/lib/wallaby/configuration/features.rb +8 -2
- data/lib/wallaby/configuration/mapping.rb +66 -112
- data/lib/wallaby/configuration/metadata.rb +15 -12
- data/lib/wallaby/configuration/models.rb +27 -25
- data/lib/wallaby/configuration/pagination.rb +15 -19
- data/lib/wallaby/configuration/security.rb +88 -80
- data/lib/wallaby/configuration/sorting.rb +15 -17
- data/lib/wallaby/configuration.rb +58 -23
- data/lib/wallaby/constants.rb +21 -13
- data/lib/wallaby/core/version.rb +1 -1
- data/lib/wallaby/core.rb +34 -10
- data/lib/wallaby/deprecator.rb +81 -0
- data/lib/wallaby/engine.rb +1 -18
- data/lib/wallaby/guesser.rb +45 -0
- data/lib/wallaby/logger.rb +35 -13
- data/lib/wallaby/map.rb +11 -88
- data/lib/wallaby/preloader.rb +8 -28
- metadata +113 -14
- data/config/locales/wallaby_class.en.yml +0 -9
- data/lib/concerns/wallaby/defaultable.rb +0 -38
- data/lib/concerns/wallaby/shared_helpers.rb +0 -22
- data/lib/services/wallaby/map/model_class_collector.rb +0 -49
- data/lib/services/wallaby/type_renderer.rb +0 -40
- data/lib/utils/wallaby/model_utils.rb +0 -52
- data/lib/utils/wallaby/test_utils.rb +0 -34
@@ -1,53 +1,59 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Wallaby
|
4
|
-
# Resources concern
|
4
|
+
# Resources concern defining the controller behaviors
|
5
|
+
# for {ResourcesController} and other controllers that include itself
|
5
6
|
module ResourcesConcern
|
6
7
|
extend ActiveSupport::Concern
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
9
|
+
include Authorizable
|
10
|
+
include Baseable
|
11
|
+
include Configurable
|
12
|
+
include Decoratable
|
13
|
+
include Prefixable
|
14
|
+
include Paginatable
|
15
|
+
include Resourcable
|
16
|
+
include Servicable
|
17
|
+
|
18
|
+
included do
|
19
|
+
include View
|
20
|
+
alias_method :_prefixes, :wallaby_prefixes
|
21
|
+
|
22
|
+
include ApplicationConcern
|
23
|
+
include AuthenticationConcern
|
24
|
+
|
25
|
+
base_class! if self == ResourcesController
|
26
|
+
|
27
|
+
# NOTE: to ensure Wallaby's layout
|
28
|
+
# is not inheriting from/impacted by parent controller's layout.
|
29
|
+
if respond_to?(:layout)
|
30
|
+
try(
|
31
|
+
# inherit? or include?
|
32
|
+
self == ResourcesController ? :layout : :theme_name=,
|
33
|
+
ResourcesController.controller_path
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
self.responder = ResourcesResponder
|
38
|
+
respond_to :html
|
39
|
+
respond_to :json
|
40
|
+
respond_to :csv
|
41
|
+
try :helper, ResourcesHelper
|
42
|
+
before_action :authenticate_wallaby_user!
|
43
|
+
end
|
37
44
|
|
38
|
-
# @!method home
|
39
45
|
# @note This is a template method that can be overridden by subclasses.
|
40
46
|
# This is an action for landing page display. It does nothing more than rendering `home` template.
|
41
47
|
#
|
42
48
|
# It can be replaced completely in subclasses as below:
|
43
49
|
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
|
48
|
-
|
50
|
+
# def home
|
51
|
+
# generate_dashboard_report
|
52
|
+
# end
|
53
|
+
def home
|
54
|
+
# do nothing
|
55
|
+
end
|
49
56
|
|
50
|
-
# @!method index(options = {}, &block)
|
51
57
|
# @note This is a template method that can be overridden by subclasses.
|
52
58
|
# This is a resourceful action to list records that user can access.
|
53
59
|
#
|
@@ -56,73 +62,75 @@ module Wallaby
|
|
56
62
|
# `WARN: Please keep in mind that Wallaby User Interface requires **index**
|
57
63
|
# action to respond to **csv** and **json** format as well.`
|
58
64
|
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# ```
|
65
|
+
# def index
|
66
|
+
# # do something before the original action
|
67
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
68
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
69
|
+
# index!(responder_options) do |format|
|
70
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
71
|
+
# # customize response behaviour, or do something before the request is rendered
|
72
|
+
# end
|
73
|
+
# end
|
69
74
|
#
|
70
75
|
# Otherwise, it can be replaced completely in subclasses:
|
71
76
|
#
|
72
77
|
# `WARN: Please keep in mind that Wallaby User Interface requires **index**
|
73
78
|
# action to respond to **csv** and **json** format as well.`
|
74
79
|
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
# ```
|
82
|
-
# @param options [Hash] (since 5.2.0) options for
|
80
|
+
# def index
|
81
|
+
# # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
|
82
|
+
# @collection = Product.all
|
83
|
+
# respond_with @collection
|
84
|
+
# end
|
85
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
83
86
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
84
87
|
# respond_with}
|
85
88
|
# @yield [format] block for
|
86
89
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
87
90
|
# respond_with}
|
88
91
|
# to customize response behaviour.
|
89
|
-
# @raise [
|
92
|
+
# @raise [Forbidden] if user has no access
|
93
|
+
def index(**responder_options, &block)
|
94
|
+
current_authorizer.authorize :index, current_model_class
|
95
|
+
respond_with collection, responder_options, &block
|
96
|
+
end
|
97
|
+
alias_method :index!, :index
|
90
98
|
|
91
|
-
# @!method new(options = {}, &block)
|
92
99
|
# @note This is a template method that can be overridden by subclasses.
|
93
100
|
# This is a resourceful action to show the form to create record that user is allowed to.
|
94
101
|
#
|
95
102
|
# It can be customized as below in subclasses:
|
96
103
|
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
# ```
|
104
|
+
# def new
|
105
|
+
# # do something before the original action
|
106
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
107
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
108
|
+
# new!(responder_options) do |format|
|
109
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
110
|
+
# # customize response behaviour, or do something before the request is rendered
|
111
|
+
# end
|
112
|
+
# end
|
107
113
|
#
|
108
114
|
# Otherwise, it can be replaced completely in subclasses:
|
109
115
|
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
# ```
|
116
|
-
# @param options [Hash] (since 5.2.0) options for
|
116
|
+
# def new
|
117
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
118
|
+
# @resource = Product.new new_arrival: true
|
119
|
+
# end
|
120
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
117
121
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
118
122
|
# respond_with}
|
119
123
|
# @yield [format] block for
|
120
124
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
121
125
|
# respond_with}
|
122
126
|
# to customize response behaviour.
|
123
|
-
# @raise [
|
127
|
+
# @raise [Forbidden] if user has no access
|
128
|
+
def new(**responder_options, &block)
|
129
|
+
current_authorizer.authorize :new, new_resource
|
130
|
+
respond_with new_resource, responder_options, &block
|
131
|
+
end
|
132
|
+
alias_method :new!, :new
|
124
133
|
|
125
|
-
# @!method create(options = {}, &block)
|
126
134
|
# @note This is a template method that can be overridden by subclasses.
|
127
135
|
# This is a resourceful action to create a record that user is allowed to.
|
128
136
|
#
|
@@ -131,110 +139,113 @@ module Wallaby
|
|
131
139
|
#
|
132
140
|
# It can be customized as below in subclasses:
|
133
141
|
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
# ```
|
142
|
+
# def create
|
143
|
+
# # do something before the original action
|
144
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
145
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
146
|
+
# create!(responder_options) do |format|
|
147
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
148
|
+
# # customize response behaviour, or do something before the request is rendered
|
149
|
+
# end
|
150
|
+
# end
|
144
151
|
#
|
145
152
|
# Otherwise, it can be replaced completely in subclasses:
|
146
153
|
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
# @param options [Hash] (since 5.2.0) options for
|
154
|
+
# def create
|
155
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
156
|
+
# @resource = Product.new resource_params.merge(new_arrival: true)
|
157
|
+
# if @resource.save
|
158
|
+
# redirect_to helper.index_path(current_model_class)
|
159
|
+
# else
|
160
|
+
# render :new
|
161
|
+
# end
|
162
|
+
# end
|
163
|
+
# @param location [Proc, String] (since 0.3.0) location for responder_options
|
164
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
159
165
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
160
|
-
# respond_with}
|
161
|
-
# @option options [ActionController::Parameters, Hash] :params
|
162
|
-
# permitted parameters for servicer to create the record. _(defaults to: {#resource_params})_
|
166
|
+
# respond_with}
|
163
167
|
# @yield [format] block for
|
164
168
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
165
169
|
# respond_with}
|
166
170
|
# to customize response behaviour.
|
167
|
-
# @raise [
|
171
|
+
# @raise [Forbidden] if user has no access
|
172
|
+
def create(location: -> { { action: :show, id: new_resource.id } }, **responder_options, &block)
|
173
|
+
current_authorizer.authorize :create, new_resource
|
174
|
+
current_servicer.create new_resource, create_params
|
175
|
+
respond_with new_resource, responder_options.merge(location: location), &block
|
176
|
+
end
|
177
|
+
alias_method :create!, :create
|
168
178
|
|
169
|
-
# @!method show(options = {}, &block)
|
170
179
|
# @note This is a template method that can be overridden by subclasses.
|
171
180
|
# This is a resourceful action to display the record details that user is allowed to.
|
172
181
|
#
|
173
182
|
# It can be customized as below in subclasses:
|
174
183
|
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
# ```
|
184
|
+
# def show
|
185
|
+
# # do something before the original action
|
186
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
187
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
188
|
+
# show!(responder_options) do |format|
|
189
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
190
|
+
# # customize response behaviour, or do something before the request is rendered
|
191
|
+
# end
|
192
|
+
# end
|
185
193
|
#
|
186
194
|
# Otherwise, it can be replaced completely in subclasses:
|
187
195
|
#
|
188
|
-
#
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
193
|
-
# ```
|
194
|
-
# @param options [Hash] (since 5.2.0) options for
|
196
|
+
# def show
|
197
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
198
|
+
# @resource = Product.find_by_slug params[:id]
|
199
|
+
# end
|
200
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
195
201
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
196
202
|
# respond_with}
|
197
203
|
# @yield [format] block for
|
198
204
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
199
205
|
# respond_with}
|
200
206
|
# to customize response behaviour.
|
201
|
-
# @raise [
|
207
|
+
# @raise [Forbidden] if user has no access
|
208
|
+
def show(**responder_options, &block)
|
209
|
+
current_authorizer.authorize :show, resource
|
210
|
+
respond_with resource, responder_options, &block
|
211
|
+
end
|
212
|
+
alias_method :show!, :show
|
202
213
|
|
203
|
-
# @!method edit(options = {}, &block)
|
204
214
|
# @note This is a template method that can be overridden by subclasses.
|
205
215
|
# This is a resourceful action to show the form to edit record that user is allowed to.
|
206
216
|
#
|
207
217
|
# It can be customized as below in subclasses:
|
208
218
|
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
218
|
-
# ```
|
219
|
+
# def edit
|
220
|
+
# # do something before the original action
|
221
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
222
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
223
|
+
# edit!(responder_options) do |format|
|
224
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
225
|
+
# # customize response behaviour, or do something before the request is rendered
|
226
|
+
# end
|
227
|
+
# end
|
219
228
|
#
|
220
229
|
# Otherwise, it can be replaced completely in subclasses:
|
221
230
|
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
# ```
|
228
|
-
# @param options [Hash] (since 5.2.0) options for
|
231
|
+
# def edit
|
232
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
233
|
+
# @resource = Product.find_by_slug params[:id]
|
234
|
+
# end
|
235
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
229
236
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
230
237
|
# respond_with}
|
231
238
|
# @yield [format] block for
|
232
239
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
233
240
|
# respond_with}
|
234
241
|
# to customize response behaviour.
|
235
|
-
# @raise [
|
242
|
+
# @raise [Forbidden] if user has no access
|
243
|
+
def edit(**responder_options, &block)
|
244
|
+
current_authorizer.authorize :edit, resource
|
245
|
+
respond_with resource, responder_options, &block
|
246
|
+
end
|
247
|
+
alias_method :edit!, :edit
|
236
248
|
|
237
|
-
# @!method update(options = {}, &block)
|
238
249
|
# @note This is a template method that can be overridden by subclasses.
|
239
250
|
# This is a resourceful action to update the record that user is allowed to.
|
240
251
|
#
|
@@ -243,192 +254,81 @@ module Wallaby
|
|
243
254
|
#
|
244
255
|
# It can be customized as below in subclasses:
|
245
256
|
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
# ```
|
257
|
+
# def update
|
258
|
+
# # do something before the original action
|
259
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
260
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
261
|
+
# update!(responder_options) do |format|
|
262
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
263
|
+
# # customize response behaviour, or do something before the request is rendered
|
264
|
+
# end
|
265
|
+
# end
|
256
266
|
#
|
257
267
|
# Otherwise, it can be replaced completely in subclasses:
|
258
268
|
#
|
259
|
-
#
|
260
|
-
#
|
261
|
-
#
|
262
|
-
#
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
267
|
-
#
|
268
|
-
#
|
269
|
-
#
|
270
|
-
#
|
271
|
-
# @param options [Hash] (since 5.2.0) options for
|
269
|
+
# def update
|
270
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
271
|
+
# @resource = Product.find_by_slug params[:id]
|
272
|
+
# @resource.assign_attributes resource_params.merge(new_arrival: true)
|
273
|
+
# if @resource.save
|
274
|
+
# redirect_to helper.index_path(current_model_class)
|
275
|
+
# else
|
276
|
+
# render :new
|
277
|
+
# end
|
278
|
+
# end
|
279
|
+
# @param location [Proc, String] (since 0.3.0) location for responder_options
|
280
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
272
281
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
273
|
-
# respond_with}
|
274
|
-
# @option options [ActionController::Parameters, Hash] :params
|
275
|
-
# permitted parameters for servicer to update the record. _(defaults to: {#resource_params})_
|
282
|
+
# respond_with}
|
276
283
|
# @yield [format] block for
|
277
284
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
278
285
|
# respond_with}
|
279
286
|
# to customize response behaviour.
|
280
|
-
# @raise [
|
287
|
+
# @raise [Forbidden] if user has no access
|
288
|
+
def update(location: -> { { action: :show } }, **responder_options, &block)
|
289
|
+
current_authorizer.authorize :update, resource
|
290
|
+
current_servicer.update resource, update_params
|
291
|
+
respond_with resource, responder_options.merge(location: location), &block
|
292
|
+
end
|
293
|
+
alias_method :update!, :update
|
281
294
|
|
282
|
-
# @!method destroy(options = {}, &block)
|
283
295
|
# @note This is a template method that can be overridden by subclasses.
|
284
296
|
# This is a resourceful action to delete the record that user is allowed to.
|
285
297
|
#
|
286
298
|
# It can be customized as below in subclasses:
|
287
299
|
#
|
288
|
-
#
|
289
|
-
#
|
290
|
-
#
|
291
|
-
#
|
292
|
-
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
#
|
297
|
-
# ```
|
300
|
+
# def destroy
|
301
|
+
# # do something before the original action
|
302
|
+
# responder_options = {} # NOTE: see `responder_options` parameter for more details
|
303
|
+
# # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
|
304
|
+
# destroy!(responder_options) do |format|
|
305
|
+
# # NOTE: this block is for `respond_with` which works similar to `respond_to`
|
306
|
+
# # customize response behaviour, or do something before the request is rendered
|
307
|
+
# end
|
308
|
+
# end
|
298
309
|
#
|
299
310
|
# Otherwise, it can be replaced completely in subclasses:
|
300
311
|
#
|
301
|
-
#
|
302
|
-
#
|
303
|
-
#
|
304
|
-
#
|
305
|
-
#
|
306
|
-
#
|
307
|
-
#
|
308
|
-
#
|
309
|
-
# @param options [Hash] (since 5.2.0) options for
|
312
|
+
# def destroy
|
313
|
+
# # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
|
314
|
+
# @resource = Product.find_by_slug params[:id]
|
315
|
+
# @resource.destroy
|
316
|
+
# redirect_to helper.index_path(current_model_class)
|
317
|
+
# end
|
318
|
+
# @param location [Proc, String] (since 0.3.0) location for responder_options
|
319
|
+
# @param responder_options [Hash] (since wallaby-5.2.0) responder_options for
|
310
320
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
311
|
-
# respond_with}
|
312
|
-
# @option options [ActionController::Parameters, Hash] :params
|
313
|
-
# permitted parameters for servicer to destroy the record. _(defaults to: {#resource_params})_
|
321
|
+
# respond_with}
|
314
322
|
# @yield [format] block for
|
315
323
|
# {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
|
316
324
|
# respond_with}
|
317
325
|
# to customize response behaviour.
|
318
|
-
# @raise [
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
#
|
324
|
-
# If Wallaby cannot generate the correct strong parameters, it can be replaced, for example:
|
325
|
-
#
|
326
|
-
# ```
|
327
|
-
# def resource_params
|
328
|
-
# params.fetch(:product, {}).permit(:name, :sku)
|
329
|
-
# end
|
330
|
-
# ```
|
331
|
-
# @return [ActionController::Parameters] whitelisted params
|
332
|
-
|
333
|
-
included do # rubocop:disable Metrics/BlockLength
|
334
|
-
include ApplicationConcern
|
335
|
-
include AuthenticationConcern
|
336
|
-
|
337
|
-
extend Authorizable::ClassMethods
|
338
|
-
extend Baseable::ClassMethods
|
339
|
-
extend Decoratable::ClassMethods
|
340
|
-
extend Paginatable::ClassMethods
|
341
|
-
extend Servicable::ClassMethods
|
342
|
-
|
343
|
-
include View
|
344
|
-
prepend Prefixable
|
345
|
-
|
346
|
-
include Authorizable
|
347
|
-
include Decoratable
|
348
|
-
include Defaultable
|
349
|
-
include Paginatable
|
350
|
-
include Resourcable
|
351
|
-
include Servicable
|
352
|
-
|
353
|
-
base_class!
|
354
|
-
|
355
|
-
# NOTE: to ensure Wallaby's layout
|
356
|
-
# is not inheriting from/impacted by parent controller's layout.
|
357
|
-
public_send(
|
358
|
-
# inherit? or include?
|
359
|
-
self == ResourcesController ? :layout : :theme_name=,
|
360
|
-
ResourcesController.controller_path
|
361
|
-
)
|
362
|
-
|
363
|
-
self.responder = ResourcesResponder
|
364
|
-
respond_to :html
|
365
|
-
respond_to :json
|
366
|
-
respond_to :csv
|
367
|
-
helper ResourcesHelper
|
368
|
-
before_action :authenticate_wallaby_user!
|
369
|
-
|
370
|
-
def home
|
371
|
-
# do nothing
|
372
|
-
end
|
373
|
-
|
374
|
-
def index(options = {}, &block)
|
375
|
-
current_authorizer.authorize :index, current_model_class
|
376
|
-
respond_with collection, options, &block
|
377
|
-
end
|
378
|
-
|
379
|
-
alias_method :index!, :index
|
380
|
-
|
381
|
-
def new(options = {}, &block)
|
382
|
-
current_authorizer.authorize :new, resource
|
383
|
-
respond_with resource, options, &block
|
384
|
-
end
|
385
|
-
|
386
|
-
alias_method :new!, :new
|
387
|
-
|
388
|
-
def create(options = {}, &block)
|
389
|
-
set_defaults_for :create, options
|
390
|
-
current_authorizer.authorize :create, resource
|
391
|
-
current_servicer.create resource, options.delete(:params)
|
392
|
-
respond_with resource, options, &block
|
393
|
-
end
|
394
|
-
|
395
|
-
alias_method :create!, :create
|
396
|
-
|
397
|
-
def show(options = {}, &block)
|
398
|
-
current_authorizer.authorize :show, resource
|
399
|
-
respond_with resource, options, &block
|
400
|
-
end
|
401
|
-
|
402
|
-
alias_method :show!, :show
|
403
|
-
|
404
|
-
def edit(options = {}, &block)
|
405
|
-
current_authorizer.authorize :edit, resource
|
406
|
-
respond_with resource, options, &block
|
407
|
-
end
|
408
|
-
|
409
|
-
alias_method :edit!, :edit
|
410
|
-
|
411
|
-
def update(options = {}, &block)
|
412
|
-
set_defaults_for :update, options
|
413
|
-
current_authorizer.authorize :update, resource
|
414
|
-
current_servicer.update resource, options.delete(:params)
|
415
|
-
respond_with resource, options, &block
|
416
|
-
end
|
417
|
-
|
418
|
-
alias_method :update!, :update
|
419
|
-
|
420
|
-
def destroy(options = {}, &block)
|
421
|
-
set_defaults_for :destroy, options
|
422
|
-
current_authorizer.authorize :destroy, resource
|
423
|
-
current_servicer.destroy resource, options.delete(:params)
|
424
|
-
respond_with resource, options, &block
|
425
|
-
end
|
426
|
-
|
427
|
-
alias_method :destroy!, :destroy
|
428
|
-
|
429
|
-
def resource_params
|
430
|
-
@resource_params ||= current_servicer.permit params, action_name
|
431
|
-
end
|
326
|
+
# @raise [Forbidden] if user has no access
|
327
|
+
def destroy(location: -> { { action: resource_id ? :index : :show } }, **responder_options, &block)
|
328
|
+
current_authorizer.authorize :destroy, resource
|
329
|
+
current_servicer.destroy resource, {}
|
330
|
+
respond_with resource, responder_options.merge(location: location), &block
|
432
331
|
end
|
332
|
+
alias_method :destroy!, :destroy
|
433
333
|
end
|
434
334
|
end
|