wallaby-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +31 -0
  4. data/app/controllers/wallaby/application_controller.rb +84 -0
  5. data/app/controllers/wallaby/resources_controller.rb +381 -0
  6. data/app/controllers/wallaby/secure_controller.rb +81 -0
  7. data/app/security/ability.rb +13 -0
  8. data/config/locales/wallaby.en.yml +140 -0
  9. data/config/locales/wallaby_class.en.yml +30 -0
  10. data/config/routes.rb +39 -0
  11. data/lib/adaptors/wallaby/custom.rb +7 -0
  12. data/lib/adaptors/wallaby/custom/default_provider.rb +9 -0
  13. data/lib/adaptors/wallaby/custom/model_decorator.rb +71 -0
  14. data/lib/adaptors/wallaby/custom/model_finder.rb +13 -0
  15. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +14 -0
  16. data/lib/adaptors/wallaby/custom/model_service_provider.rb +48 -0
  17. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +72 -0
  18. data/lib/authorizers/wallaby/default_authorization_provider.rb +58 -0
  19. data/lib/authorizers/wallaby/model_authorizer.rb +100 -0
  20. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +89 -0
  21. data/lib/concerns/wallaby/authorizable.rb +103 -0
  22. data/lib/concerns/wallaby/baseable.rb +36 -0
  23. data/lib/concerns/wallaby/decoratable.rb +101 -0
  24. data/lib/concerns/wallaby/defaultable.rb +38 -0
  25. data/lib/concerns/wallaby/engineable.rb +61 -0
  26. data/lib/concerns/wallaby/fieldable.rb +78 -0
  27. data/lib/concerns/wallaby/paginatable.rb +72 -0
  28. data/lib/concerns/wallaby/rails_overridden_methods.rb +42 -0
  29. data/lib/concerns/wallaby/resourcable.rb +149 -0
  30. data/lib/concerns/wallaby/servicable.rb +68 -0
  31. data/lib/concerns/wallaby/shared_helpers.rb +22 -0
  32. data/lib/concerns/wallaby/themeable.rb +40 -0
  33. data/lib/decorators/wallaby/resource_decorator.rb +189 -0
  34. data/lib/errors/wallaby/cell_handling.rb +6 -0
  35. data/lib/errors/wallaby/forbidden.rb +6 -0
  36. data/lib/errors/wallaby/general_error.rb +6 -0
  37. data/lib/errors/wallaby/invalid_error.rb +6 -0
  38. data/lib/errors/wallaby/model_not_found.rb +11 -0
  39. data/lib/errors/wallaby/not_authenticated.rb +6 -0
  40. data/lib/errors/wallaby/not_found.rb +6 -0
  41. data/lib/errors/wallaby/not_implemented.rb +6 -0
  42. data/lib/errors/wallaby/resource_not_found.rb +11 -0
  43. data/lib/errors/wallaby/unprocessable_entity.rb +6 -0
  44. data/lib/forms/wallaby/form_builder.rb +60 -0
  45. data/lib/helpers/wallaby/application_helper.rb +79 -0
  46. data/lib/helpers/wallaby/base_helper.rb +65 -0
  47. data/lib/helpers/wallaby/configuration_helper.rb +18 -0
  48. data/lib/helpers/wallaby/form_helper.rb +62 -0
  49. data/lib/helpers/wallaby/index_helper.rb +84 -0
  50. data/lib/helpers/wallaby/links_helper.rb +213 -0
  51. data/lib/helpers/wallaby/resources_helper.rb +52 -0
  52. data/lib/helpers/wallaby/secure_helper.rb +54 -0
  53. data/lib/helpers/wallaby/styling_helper.rb +82 -0
  54. data/lib/interfaces/wallaby/mode.rb +72 -0
  55. data/lib/interfaces/wallaby/model_authorization_provider.rb +99 -0
  56. data/lib/interfaces/wallaby/model_decorator.rb +168 -0
  57. data/lib/interfaces/wallaby/model_finder.rb +12 -0
  58. data/lib/interfaces/wallaby/model_pagination_provider.rb +107 -0
  59. data/lib/interfaces/wallaby/model_service_provider.rb +84 -0
  60. data/lib/paginators/wallaby/model_paginator.rb +115 -0
  61. data/lib/paginators/wallaby/resource_paginator.rb +12 -0
  62. data/lib/parsers/wallaby/parser.rb +34 -0
  63. data/lib/renderers/wallaby/cell.rb +137 -0
  64. data/lib/renderers/wallaby/cell_resolver.rb +89 -0
  65. data/lib/renderers/wallaby/custom_lookup_context.rb +64 -0
  66. data/lib/renderers/wallaby/custom_partial_renderer.rb +33 -0
  67. data/lib/renderers/wallaby/custom_renderer.rb +16 -0
  68. data/lib/responders/wallaby/json_api_responder.rb +101 -0
  69. data/lib/responders/wallaby/resources_responder.rb +28 -0
  70. data/lib/routes/wallaby/resources_router.rb +72 -0
  71. data/lib/servicers/wallaby/model_servicer.rb +154 -0
  72. data/lib/services/wallaby/engine_name_finder.rb +22 -0
  73. data/lib/services/wallaby/engine_url_for.rb +46 -0
  74. data/lib/services/wallaby/link_options_normalizer.rb +19 -0
  75. data/lib/services/wallaby/map/mode_mapper.rb +27 -0
  76. data/lib/services/wallaby/map/model_class_collector.rb +49 -0
  77. data/lib/services/wallaby/map/model_class_mapper.rb +38 -0
  78. data/lib/services/wallaby/prefixes_builder.rb +66 -0
  79. data/lib/services/wallaby/sorting/hash_builder.rb +19 -0
  80. data/lib/services/wallaby/sorting/link_builder.rb +69 -0
  81. data/lib/services/wallaby/sorting/next_builder.rb +63 -0
  82. data/lib/services/wallaby/sorting/single_builder.rb +20 -0
  83. data/lib/services/wallaby/type_renderer.rb +50 -0
  84. data/lib/support/action_dispatch/routing/mapper.rb +75 -0
  85. data/lib/tree/wallaby/node.rb +25 -0
  86. data/lib/utils/wallaby/cell_utils.rb +34 -0
  87. data/lib/utils/wallaby/field_utils.rb +43 -0
  88. data/lib/utils/wallaby/filter_utils.rb +20 -0
  89. data/lib/utils/wallaby/model_utils.rb +51 -0
  90. data/lib/utils/wallaby/module_utils.rb +46 -0
  91. data/lib/utils/wallaby/params_utils.rb +14 -0
  92. data/lib/utils/wallaby/preload_utils.rb +44 -0
  93. data/lib/utils/wallaby/test_utils.rb +34 -0
  94. data/lib/utils/wallaby/utils.rb +27 -0
  95. data/lib/wallaby/configuration.rb +103 -0
  96. data/lib/wallaby/configuration/features.rb +24 -0
  97. data/lib/wallaby/configuration/mapping.rb +140 -0
  98. data/lib/wallaby/configuration/metadata.rb +23 -0
  99. data/lib/wallaby/configuration/models.rb +46 -0
  100. data/lib/wallaby/configuration/pagination.rb +30 -0
  101. data/lib/wallaby/configuration/security.rb +98 -0
  102. data/lib/wallaby/configuration/sorting.rb +28 -0
  103. data/lib/wallaby/constants.rb +45 -0
  104. data/lib/wallaby/core.rb +117 -0
  105. data/lib/wallaby/core/version.rb +7 -0
  106. data/lib/wallaby/engine.rb +43 -0
  107. data/lib/wallaby/map.rb +170 -0
  108. metadata +222 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54c8cdfb2441a930457233aa18eb94b203afba08731c9eff8574d3308bb979df
4
+ data.tar.gz: e738dbb089e23a0148e112b250a9aead31d40cc8147e4403c911038e5d1c7c08
5
+ SHA512:
6
+ metadata.gz: 0453d5a5de52cf2b6888a01e97658ee40155796156e7d09595f9249917baf036bce45a53d7c7ae180c9a184bd89e98c76df7fd960f49b4542d814f4bd8a725a3
7
+ data.tar.gz: ffe831732748530de95c35e7ed4ff91e9d9c9710b483c80e99048186636d36d24b3e432811c713b7e04c452bf91a190768970d827a3c402fdc6b91b4c374fd78
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010-2019 Google LLC. http://angular.io/license
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Wallaby::Core
2
+
3
+ This gem contains the core interface that Wallaby is built upon.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wallaby-core'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```shell
22
+ $ gem install wallaby-core
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wallaby-rails/wallaby-core.
28
+
29
+ ## License
30
+
31
+ This project is [MIT Licensed](LICENSE)
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Like ordinary Rails application, it's the base controller that
5
+ # other Wallaby controllers inherit from.
6
+ #
7
+ # However, the difference is that the controller class that {Wallaby::ApplicationController} inherits from
8
+ # can be configured via {Wallaby::Configuration#base_controller}
9
+ #
10
+ # Here, it provides the most basic functions e.g. error handling for common 4xx HTTP status, helpers method,
11
+ # and URL handling.
12
+ class ApplicationController < configuration.base_controller
13
+ extend Engineable::ClassMethods
14
+ include Engineable
15
+ include SharedHelpers
16
+ helper ApplicationHelper
17
+
18
+ rescue_from NotFound, with: :not_found
19
+ rescue_from ::ActionController::ParameterMissing, with: :bad_request
20
+ rescue_from ::ActiveRecord::StatementInvalid, with: :unprocessable_entity
21
+ rescue_from NotImplemented, with: :not_implemented
22
+ rescue_from UnprocessableEntity, with: :unprocessable_entity
23
+
24
+ delegate(*ConfigurationHelper.instance_methods(false), :url_for, to: :helpers)
25
+
26
+ # Health check page for e.g. NewRelic
27
+ def healthy
28
+ render plain: 'healthy'
29
+ end
30
+
31
+ # Not found page
32
+ # @param exception [Exception] comes from **rescue_from**
33
+ def not_found(exception = nil)
34
+ error_rendering exception, __callee__
35
+ end
36
+
37
+ # Bad request page
38
+ # @param exception [Exception] comes from **rescue_from**
39
+ def bad_request(exception = nil)
40
+ error_rendering exception, __callee__
41
+ end
42
+
43
+ # Unprocessable entity page
44
+ # @param exception [Exception] comes from **rescue_from**
45
+ def unprocessable_entity(exception = nil)
46
+ error_rendering exception, __callee__
47
+ end
48
+
49
+ # Internal server error page
50
+ # @param exception [Exception] comes from **rescue_from**
51
+ def internal_server_error(exception = nil)
52
+ error_rendering exception, __callee__
53
+ end
54
+
55
+ # Not implemented
56
+ # @param exception [Exception] comes from **rescue_from**
57
+ def not_implemented(exception = nil)
58
+ error_rendering exception, __callee__
59
+ end
60
+
61
+ # {https://api.rubyonrails.org/classes/ActionController/Helpers.html#method-i-helpers helpers}
62
+ # exists since Rails 5.0, need to mimic this to support Rails 4.2.
63
+ # @see https://api.rubyonrails.org/classes/ActionController/Helpers.html#method-i-helpers
64
+ # ActionController::Helpers#helpers
65
+ # @see https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_controller/metal/helpers.rb#L118
66
+ def helpers
67
+ @helpers ||= defined?(super) ? super : view_context
68
+ end
69
+
70
+ protected
71
+
72
+ # Capture exceptions and display the error using error template.
73
+ # @param exception [Exception]
74
+ # @param symbol [Symbol] http status symbol
75
+ def error_rendering(exception, symbol)
76
+ Rails.logger.error exception
77
+
78
+ @exception = exception
79
+ @symbol = symbol
80
+ @code = Rack::Utils::SYMBOL_TO_STATUS_CODE[symbol].to_i
81
+ respond_with @exception, status: @code, template: ERROR_PATH, prefixes: _prefixes
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,381 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Resources controller, superclass for all customization controllers.
5
+ # It contains CRUD template action methods (`index`/`new`/`create`/`edit`/`update`/`destroy`)
6
+ # that allow subclasses to override.
7
+ class ResourcesController < ::Wallaby::SecureController
8
+ extend Authorizable::ClassMethods
9
+ extend Baseable::ClassMethods
10
+ extend Decoratable::ClassMethods
11
+ extend Paginatable::ClassMethods
12
+ extend Resourcable::ClassMethods
13
+ extend Servicable::ClassMethods
14
+ extend Themeable::ClassMethods
15
+ include Authorizable
16
+ include Decoratable
17
+ include Defaultable
18
+ include Paginatable
19
+ include RailsOverriddenMethods
20
+ include Resourcable
21
+ include Servicable
22
+ include Themeable
23
+
24
+ self.responder = ResourcesResponder
25
+ respond_to :html
26
+ respond_to :json
27
+ respond_to :csv, only: :index
28
+ helper ResourcesHelper
29
+ before_action :authenticate_user!
30
+
31
+ # @note This is a template method that can be overridden by subclasses.
32
+ # This is an action for landing page display. It does nothing more than rendering `home` template.
33
+ #
34
+ # It can be replaced completely in subclasses as below:
35
+ #
36
+ # ```
37
+ # def home
38
+ # generate_dashboard_report
39
+ # end
40
+ # ```
41
+ def home
42
+ # do nothing
43
+ end
44
+
45
+ # @note This is a template method that can be overridden by subclasses.
46
+ # This is a resourceful action to list records that user can access.
47
+ #
48
+ # It can be customized as below in subclasses:
49
+ #
50
+ # `WARN: Please keep in mind that Wallaby User Interface requires **index**
51
+ # action to respond to **csv** and **json** format as well.`
52
+ #
53
+ # ```
54
+ # def index
55
+ # # do something before the origin action
56
+ # options = {} # NOTE: see `options` parameter for more details
57
+ # index!(options) do |format| # NOTE: this is better than using `super`
58
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
59
+ # # customize response behaviour, or do something before the request is rendered
60
+ # end
61
+ # end
62
+ # ```
63
+ #
64
+ # Otherwise, it can be replaced completely in subclasses:
65
+ #
66
+ # `WARN: Please keep in mind that Wallaby User Interface requires **index**
67
+ # action to respond to **csv** and **json** format as well.`
68
+ #
69
+ # ```
70
+ # def index
71
+ # # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
72
+ # @collection = Product.all
73
+ # respond_with @collection
74
+ # end
75
+ # ```
76
+ # @param options [Hash] (since 5.2.0) options for
77
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
78
+ # respond_with}
79
+ # @yield [format] block for
80
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
81
+ # respond_with}
82
+ # to customize response behaviour.
83
+ # @raise [Wallaby::Forbidden] if user has no access
84
+ def index(options = {}, &block)
85
+ current_authorizer.authorize :index, current_model_class
86
+ respond_with collection, options, &block
87
+ end
88
+
89
+ alias index! index
90
+
91
+ # @note This is a template method that can be overridden by subclasses.
92
+ # This is a resourceful action to show the form to create record that user is allowed to.
93
+ #
94
+ # It can be customized as below in subclasses:
95
+ #
96
+ # ```
97
+ # def new
98
+ # # do something before the origin action
99
+ # options = {} # NOTE: see `options` parameter for more details
100
+ # new!(options) do |format| # NOTE: this is better than using `super`
101
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
102
+ # # customize response behaviour, or do something before the request is rendered
103
+ # end
104
+ # end
105
+ # ```
106
+ #
107
+ # Otherwise, it can be replaced completely in subclasses:
108
+ #
109
+ # ```
110
+ # def new
111
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
112
+ # @resource = Product.new new_arrival: true
113
+ # end
114
+ # ```
115
+ # @param options [Hash] (since 5.2.0) options for
116
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
117
+ # respond_with}
118
+ # @yield [format] block for
119
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
120
+ # respond_with}
121
+ # to customize response behaviour.
122
+ # @raise [Wallaby::Forbidden] if user has no access
123
+ def new(options = {}, &block)
124
+ current_authorizer.authorize :new, resource
125
+ respond_with resource, options, &block
126
+ end
127
+
128
+ alias new! new
129
+
130
+ # @note This is a template method that can be overridden by subclasses.
131
+ # This is a resourceful action to create a record that user is allowed to.
132
+ #
133
+ # If record is created successfully, user will be navigated to the record show page.
134
+ # Otherwise, the form will be shown again with error messages.
135
+ #
136
+ # It can be customized as below in subclasses:
137
+ #
138
+ # ```
139
+ # def create
140
+ # # do something before the origin action
141
+ # options = {} # NOTE: see `options` parameter for more details
142
+ # create!(options) do |format| # NOTE: this is better than using `super`
143
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
144
+ # # customize response behaviour, or do something before the request is rendered
145
+ # end
146
+ # end
147
+ # ```
148
+ #
149
+ # Otherwise, it can be replaced completely in subclasses:
150
+ #
151
+ # ```
152
+ # def create
153
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
154
+ # @resource = Product.new resource_params.merge(new_arrival: true)
155
+ # if @resource.save
156
+ # redirect_to helper.index_path(current_model_class)
157
+ # else
158
+ # render :new
159
+ # end
160
+ # end
161
+ # ```
162
+ # @param options [Hash] (since 5.2.0) options for
163
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
164
+ # respond_with}. In addition, options `:params` is supported, see below
165
+ # @option options [ActionController::Parameters, Hash] :params
166
+ # permitted parameters for servicer to create the record. _(defaults to: {#resource_params})_
167
+ # @yield [format] block for
168
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
169
+ # respond_with}
170
+ # to customize response behaviour.
171
+ # @raise [Wallaby::Forbidden] if user has no access
172
+ def create(options = {}, &block)
173
+ set_defaults_for :create, options
174
+ current_authorizer.authorize :create, resource
175
+ current_servicer.create resource, options.delete(:params)
176
+ respond_with resource, options, &block
177
+ end
178
+
179
+ alias create! create
180
+
181
+ # @note This is a template method that can be overridden by subclasses.
182
+ # This is a resourceful action to display the record details that user is allowed to.
183
+ #
184
+ # It can be customized as below in subclasses:
185
+ #
186
+ # ```
187
+ # def show
188
+ # # do something before the origin action
189
+ # options = {} # NOTE: see `options` parameter for more details
190
+ # show!(options) do |format| # NOTE: this is better than using `super`
191
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
192
+ # # customize response behaviour, or do something before the request is rendered
193
+ # end
194
+ # end
195
+ # ```
196
+ #
197
+ # Otherwise, it can be replaced completely in subclasses:
198
+ #
199
+ # ```
200
+ # def show
201
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
202
+ # @resource = Product.find_by_slug params[:id]
203
+ # end
204
+ # ```
205
+ # @param options [Hash] (since 5.2.0) options for
206
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
207
+ # respond_with}
208
+ # @yield [format] block for
209
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
210
+ # respond_with}
211
+ # to customize response behaviour.
212
+ # @raise [Wallaby::Forbidden] if user has no access
213
+ def show(options = {}, &block)
214
+ current_authorizer.authorize :show, resource
215
+ respond_with resource, options, &block
216
+ end
217
+
218
+ alias show! show
219
+
220
+ # @note This is a template method that can be overridden by subclasses.
221
+ # This is a resourceful action to show the form to edit record that user is allowed to.
222
+ #
223
+ # It can be customized as below in subclasses:
224
+ #
225
+ # ```
226
+ # def edit
227
+ # # do something before the origin action
228
+ # options = {} # NOTE: see `options` parameter for more details
229
+ # edit!(options) do |format| # NOTE: this is better than using `super`
230
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
231
+ # # customize response behaviour, or do something before the request is rendered
232
+ # end
233
+ # end
234
+ # ```
235
+ #
236
+ # Otherwise, it can be replaced completely in subclasses:
237
+ #
238
+ # ```
239
+ # def edit
240
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
241
+ # @resource = Product.find_by_slug params[:id]
242
+ # end
243
+ # ```
244
+ # @param options [Hash] (since 5.2.0) options for
245
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
246
+ # respond_with}
247
+ # @yield [format] block for
248
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
249
+ # respond_with}
250
+ # to customize response behaviour.
251
+ # @raise [Wallaby::Forbidden] if user has no access
252
+ def edit(options = {}, &block)
253
+ current_authorizer.authorize :edit, resource
254
+ respond_with resource, options, &block
255
+ end
256
+
257
+ alias edit! edit
258
+
259
+ # @note This is a template method that can be overridden by subclasses.
260
+ # This is a resourceful action to update the record that user is allowed to.
261
+ #
262
+ # If record is updated successfully, user will be navigated to the record show page.
263
+ # Otherwise, the form will be shown again with error messages.
264
+ #
265
+ # It can be customized as below in subclasses:
266
+ #
267
+ # ```
268
+ # def update
269
+ # # do something before the origin action
270
+ # options = {} # NOTE: see `options` parameter for more details
271
+ # update!(options) do |format| # NOTE: this is better than using `super`
272
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
273
+ # # customize response behaviour, or do something before the request is rendered
274
+ # end
275
+ # end
276
+ # ```
277
+ #
278
+ # Otherwise, it can be replaced completely in subclasses:
279
+ #
280
+ # ```
281
+ # def update
282
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
283
+ # @resource = Product.find_by_slug params[:id]
284
+ # @resource.assign_attributes resource_params.merge(new_arrival: true)
285
+ # if @resource.save
286
+ # redirect_to helper.index_path(current_model_class)
287
+ # else
288
+ # render :new
289
+ # end
290
+ # end
291
+ # ```
292
+ # @param options [Hash] (since 5.2.0) options for
293
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
294
+ # respond_with}. In addition, options `:params` is supported, see below
295
+ # @option options [ActionController::Parameters, Hash] :params
296
+ # permitted parameters for servicer to update the record. _(defaults to: {#resource_params})_
297
+ # @yield [format] block for
298
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
299
+ # respond_with}
300
+ # to customize response behaviour.
301
+ # @raise [Wallaby::Forbidden] if user has no access
302
+ def update(options = {}, &block)
303
+ set_defaults_for :update, options
304
+ current_authorizer.authorize :update, resource
305
+ current_servicer.update resource, options.delete(:params)
306
+ respond_with resource, options, &block
307
+ end
308
+
309
+ alias update! update
310
+
311
+ # @note This is a template method that can be overridden by subclasses.
312
+ # This is a resourceful action to delete the record that user is allowed to.
313
+ #
314
+ # It can be customized as below in subclasses:
315
+ #
316
+ # ```
317
+ # def destroy
318
+ # # do something before the origin action
319
+ # options = {} # NOTE: see `options` parameter for more details
320
+ # destroy!(options) do |format| # NOTE: this is better than using `super`
321
+ # # NOTE: this block is for `respond_with` which works similar to `respond_to`
322
+ # # customize response behaviour, or do something before the request is rendered
323
+ # end
324
+ # end
325
+ # ```
326
+ #
327
+ # Otherwise, it can be replaced completely in subclasses:
328
+ #
329
+ # ```
330
+ # def destroy
331
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
332
+ # @resource = Product.find_by_slug params[:id]
333
+ # @resource.destroy
334
+ # redirect_to helper.index_path(current_model_class)
335
+ # end
336
+ # ```
337
+ # @param options [Hash] (since 5.2.0) options for
338
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
339
+ # respond_with}. In addition, options `:params` is supported, see below
340
+ # @option options [ActionController::Parameters, Hash] :params
341
+ # permitted parameters for servicer to destroy the record. _(defaults to: {#resource_params})_
342
+ # @yield [format] block for
343
+ # {https://www.rubydoc.info/gems/responders/ActionController/RespondWith#respond_with-instance_method
344
+ # respond_with}
345
+ # to customize response behaviour.
346
+ # @raise [Wallaby::Forbidden] if user has no access
347
+ def destroy(options = {}, &block)
348
+ set_defaults_for :destroy, options
349
+ current_authorizer.authorize :destroy, resource
350
+ current_servicer.destroy resource, options.delete(:params)
351
+ respond_with resource, options, &block
352
+ end
353
+
354
+ alias destroy! destroy
355
+
356
+ # @note This is a template method that can be overridden by subclasses.
357
+ # To whitelist the params for {#create} and {#update} actions.
358
+ #
359
+ # If Wallaby cannot generate the correct strong parameters, it can be replaced, for example:
360
+ #
361
+ # ```
362
+ # def resource_params
363
+ # params.fetch(:product, {}).permit(:name, :sku)
364
+ # end
365
+ # ```
366
+ # @return [ActionController::Parameters] whitelisted params
367
+ def resource_params
368
+ @resource_params ||= current_servicer.permit params, action_name
369
+ end
370
+
371
+ # @!method collection(options = {}, &block)
372
+ # (see Wallaby::Resourcable#collection)
373
+ # @see Wallaby::Resourcable#collection
374
+ alias collection! collection
375
+
376
+ # @!method resource(options = {}, &block)
377
+ # (see Wallaby::Resourcable#resource)
378
+ # @see Wallaby::Resourcable#resource
379
+ alias resource! resource
380
+ end
381
+ end