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.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/wallaby/resources_controller.rb +20 -7
  3. data/app/security/ability.rb +1 -1
  4. data/config/routes.rb +21 -14
  5. data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
  6. data/lib/adaptors/wallaby/custom/model_decorator.rb +5 -17
  7. data/lib/adaptors/wallaby/custom/model_finder.rb +3 -3
  8. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
  9. data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -1
  10. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +12 -5
  11. data/lib/authorizers/wallaby/default_authorization_provider.rb +10 -1
  12. data/lib/authorizers/wallaby/model_authorizer.rb +41 -16
  13. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +22 -8
  14. data/lib/concerns/wallaby/application_concern.rb +41 -71
  15. data/lib/concerns/wallaby/authentication_concern.rb +29 -127
  16. data/lib/concerns/wallaby/authorizable.rb +14 -57
  17. data/lib/concerns/wallaby/baseable.rb +24 -57
  18. data/lib/concerns/wallaby/configurable.rb +416 -0
  19. data/lib/concerns/wallaby/decoratable.rb +29 -60
  20. data/lib/concerns/wallaby/engineable.rb +29 -46
  21. data/lib/concerns/wallaby/fieldable.rb +45 -56
  22. data/lib/concerns/wallaby/paginatable.rb +20 -51
  23. data/lib/concerns/wallaby/prefixable.rb +24 -4
  24. data/lib/concerns/wallaby/resourcable.rb +130 -72
  25. data/lib/concerns/wallaby/resources_concern.rb +205 -305
  26. data/lib/concerns/wallaby/servicable.rb +8 -48
  27. data/lib/concerns/wallaby/urlable.rb +69 -0
  28. data/lib/decorators/wallaby/resource_decorator.rb +91 -34
  29. data/lib/errors/wallaby/class_not_found.rb +1 -2
  30. data/lib/errors/wallaby/forbidden.rb +1 -2
  31. data/lib/errors/wallaby/general_error.rb +1 -1
  32. data/lib/errors/wallaby/invalid_error.rb +1 -2
  33. data/lib/errors/wallaby/method_removed.rb +5 -0
  34. data/lib/errors/wallaby/model_not_found.rb +1 -2
  35. data/lib/errors/wallaby/not_authenticated.rb +1 -2
  36. data/lib/errors/wallaby/not_found.rb +1 -2
  37. data/lib/errors/wallaby/not_implemented.rb +1 -2
  38. data/lib/errors/wallaby/resource_not_found.rb +1 -2
  39. data/lib/errors/wallaby/unprocessable_entity.rb +1 -2
  40. data/lib/fields/wallaby/all_fields.rb +63 -0
  41. data/lib/forms/wallaby/form_builder.rb +1 -1
  42. data/lib/generators/wallaby/engine/application_generator.rb +33 -0
  43. data/lib/generators/wallaby/engine/authorizer/USAGE +20 -0
  44. data/lib/generators/wallaby/engine/authorizer/authorizer_generator.rb +19 -0
  45. data/lib/generators/wallaby/engine/authorizer/templates/authorizer.rb.erb +35 -0
  46. data/lib/generators/wallaby/engine/controller/USAGE +20 -0
  47. data/lib/generators/wallaby/engine/controller/controller_generator.rb +23 -0
  48. data/lib/generators/wallaby/engine/controller/templates/controller.rb.erb +130 -0
  49. data/lib/generators/wallaby/engine/decorator/USAGE +20 -0
  50. data/lib/generators/wallaby/engine/decorator/decorator_generator.rb +19 -0
  51. data/lib/generators/wallaby/engine/decorator/templates/decorator.rb.erb +5 -0
  52. data/lib/generators/wallaby/engine/install/USAGE +19 -0
  53. data/lib/generators/wallaby/engine/install/install_generator.rb +91 -0
  54. data/lib/generators/wallaby/engine/install/templates/application_authorizer.rb.erb +37 -0
  55. data/lib/generators/wallaby/engine/install/templates/application_controller.rb.erb +173 -0
  56. data/lib/generators/wallaby/engine/install/templates/application_decorator.rb.erb +7 -0
  57. data/lib/generators/wallaby/engine/install/templates/application_paginator.rb.erb +27 -0
  58. data/lib/generators/wallaby/engine/install/templates/application_servicer.rb.erb +47 -0
  59. data/lib/generators/wallaby/engine/install/templates/initializer.rb.erb +16 -0
  60. data/lib/generators/wallaby/engine/paginator/USAGE +20 -0
  61. data/lib/generators/wallaby/engine/paginator/paginator_generator.rb +19 -0
  62. data/lib/generators/wallaby/engine/paginator/templates/paginator.rb.erb +25 -0
  63. data/lib/generators/wallaby/engine/servicer/USAGE +20 -0
  64. data/lib/generators/wallaby/engine/servicer/servicer_generator.rb +19 -0
  65. data/lib/generators/wallaby/engine/servicer/templates/servicer.rb.erb +45 -0
  66. data/lib/helpers/wallaby/application_helper.rb +10 -59
  67. data/lib/helpers/wallaby/base_helper.rb +11 -11
  68. data/lib/helpers/wallaby/configuration_helper.rb +36 -4
  69. data/lib/helpers/wallaby/form_helper.rb +1 -1
  70. data/lib/helpers/wallaby/index_helper.rb +19 -9
  71. data/lib/helpers/wallaby/links_helper.rb +18 -85
  72. data/lib/helpers/wallaby/resources_helper.rb +39 -7
  73. data/lib/helpers/wallaby/secure_helper.rb +20 -19
  74. data/lib/interfaces/wallaby/mode.rb +8 -8
  75. data/lib/interfaces/wallaby/model_authorization_provider.rb +23 -22
  76. data/lib/interfaces/wallaby/model_decorator.rb +36 -48
  77. data/lib/interfaces/wallaby/model_finder.rb +3 -3
  78. data/lib/interfaces/wallaby/model_pagination_provider.rb +2 -6
  79. data/lib/interfaces/wallaby/model_service_provider.rb +4 -4
  80. data/lib/paginators/wallaby/model_paginator.rb +1 -1
  81. data/lib/responders/wallaby/json_api_responder.rb +10 -5
  82. data/lib/responders/wallaby/resources_responder.rb +10 -3
  83. data/lib/routes/wallaby/engines/base_route.rb +78 -0
  84. data/lib/routes/wallaby/engines/custom_app_route.rb +92 -0
  85. data/lib/routes/wallaby/engines/engine_route.rb +77 -0
  86. data/lib/routes/wallaby/resources_router.rb +100 -45
  87. data/lib/servicers/wallaby/model_servicer.rb +13 -13
  88. data/lib/services/wallaby/authorizer_finder.rb +23 -0
  89. data/lib/services/wallaby/class_finder.rb +42 -0
  90. data/lib/services/wallaby/controller_finder.rb +29 -0
  91. data/lib/services/wallaby/decorator_finder.rb +34 -0
  92. data/lib/services/wallaby/default_models_excluder.rb +45 -0
  93. data/lib/services/wallaby/engine_name_finder.rb +14 -11
  94. data/lib/services/wallaby/engine_url_for.rb +82 -37
  95. data/lib/services/wallaby/fields_regulator.rb +34 -0
  96. data/lib/services/wallaby/map/mode_mapper.rb +5 -5
  97. data/lib/services/wallaby/map/model_class_mapper.rb +1 -1
  98. data/lib/services/wallaby/model_class_filter.rb +29 -0
  99. data/lib/services/wallaby/paginator_finder.rb +24 -0
  100. data/lib/services/wallaby/prefixes_builder.rb +49 -8
  101. data/lib/services/wallaby/servicer_finder.rb +31 -0
  102. data/lib/services/wallaby/sorting/hash_builder.rb +9 -0
  103. data/lib/services/wallaby/sorting/link_builder.rb +7 -10
  104. data/lib/services/wallaby/sorting/next_builder.rb +1 -12
  105. data/lib/services/wallaby/sorting/single_builder.rb +1 -1
  106. data/lib/support/action_dispatch/routing/mapper.rb +29 -4
  107. data/lib/utils/wallaby/field_utils.rb +9 -8
  108. data/lib/utils/wallaby/inflector.rb +94 -0
  109. data/lib/utils/wallaby/locale.rb +2 -2
  110. data/lib/utils/wallaby/module_utils.rb +3 -10
  111. data/lib/utils/wallaby/utils.rb +21 -14
  112. data/lib/wallaby/class_array.rb +18 -13
  113. data/lib/wallaby/class_hash.rb +16 -14
  114. data/lib/wallaby/classifier.rb +4 -2
  115. data/lib/wallaby/configuration/features.rb +8 -2
  116. data/lib/wallaby/configuration/mapping.rb +66 -112
  117. data/lib/wallaby/configuration/metadata.rb +15 -12
  118. data/lib/wallaby/configuration/models.rb +27 -25
  119. data/lib/wallaby/configuration/pagination.rb +15 -19
  120. data/lib/wallaby/configuration/security.rb +88 -80
  121. data/lib/wallaby/configuration/sorting.rb +15 -17
  122. data/lib/wallaby/configuration.rb +58 -23
  123. data/lib/wallaby/constants.rb +21 -13
  124. data/lib/wallaby/core/version.rb +1 -1
  125. data/lib/wallaby/core.rb +34 -10
  126. data/lib/wallaby/deprecator.rb +81 -0
  127. data/lib/wallaby/engine.rb +1 -18
  128. data/lib/wallaby/guesser.rb +45 -0
  129. data/lib/wallaby/logger.rb +35 -13
  130. data/lib/wallaby/map.rb +11 -88
  131. data/lib/wallaby/preloader.rb +8 -28
  132. metadata +113 -14
  133. data/config/locales/wallaby_class.en.yml +0 -9
  134. data/lib/concerns/wallaby/defaultable.rb +0 -38
  135. data/lib/concerns/wallaby/shared_helpers.rb +0 -22
  136. data/lib/services/wallaby/map/model_class_collector.rb +0 -49
  137. data/lib/services/wallaby/type_renderer.rb +0 -40
  138. data/lib/utils/wallaby/model_utils.rb +0 -52
  139. 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
- # @!parse
9
- # include ApplicationConcern
10
- # include AuthenticationConcern
11
- # extend Authorizable::ClassMethods
12
- # extend Baseable::ClassMethods
13
- # extend Decoratable::ClassMethods
14
- # extend Paginatable::ClassMethods
15
- # extend Resourcable::ClassMethods
16
- # extend Servicable::ClassMethods
17
- #
18
- # include View
19
- # prepend Prefixable
20
- #
21
- # include Authorizable
22
- # include Decoratable
23
- # include Defaultable
24
- # include Paginatable
25
- # include Resourcable
26
- # include Servicable
27
- #
28
- # alias index! index
29
- # alias new! new
30
- # alias create! create
31
- # alias show! show
32
- # alias edit! edit
33
- # alias update! update
34
- # alias destroy! destroy
35
- # alias collection! collection
36
- # alias resource! resource
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
- # def home
46
- # generate_dashboard_report
47
- # end
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
- # def index
61
- # # do something before the origin action
62
- # options = {} # NOTE: see `options` parameter for more details
63
- # index!(options) do |format| # NOTE: this is better than using `super`
64
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
65
- # # customize response behaviour, or do something before the request is rendered
66
- # end
67
- # end
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
- # def index
77
- # # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
78
- # @collection = Product.all
79
- # respond_with @collection
80
- # end
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 [Wallaby::Forbidden] if user has no access
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
- # def new
99
- # # do something before the origin action
100
- # options = {} # NOTE: see `options` parameter for more details
101
- # new!(options) do |format| # NOTE: this is better than using `super`
102
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
103
- # # customize response behaviour, or do something before the request is rendered
104
- # end
105
- # end
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
- # def new
112
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
113
- # @resource = Product.new new_arrival: true
114
- # end
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 [Wallaby::Forbidden] if user has no access
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
- # def create
136
- # # do something before the origin action
137
- # options = {} # NOTE: see `options` parameter for more details
138
- # create!(options) do |format| # NOTE: this is better than using `super`
139
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
140
- # # customize response behaviour, or do something before the request is rendered
141
- # end
142
- # end
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
- # def create
149
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
150
- # @resource = Product.new resource_params.merge(new_arrival: true)
151
- # if @resource.save
152
- # redirect_to helper.index_path(current_model_class)
153
- # else
154
- # render :new
155
- # end
156
- # end
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}. In addition, options `:params` is supported, see below
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 [Wallaby::Forbidden] if user has no access
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
- # def show
177
- # # do something before the origin action
178
- # options = {} # NOTE: see `options` parameter for more details
179
- # show!(options) do |format| # NOTE: this is better than using `super`
180
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
181
- # # customize response behaviour, or do something before the request is rendered
182
- # end
183
- # end
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
- # def show
190
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
191
- # @resource = Product.find_by_slug params[:id]
192
- # end
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 [Wallaby::Forbidden] if user has no access
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
- # def edit
211
- # # do something before the origin action
212
- # options = {} # NOTE: see `options` parameter for more details
213
- # edit!(options) do |format| # NOTE: this is better than using `super`
214
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
215
- # # customize response behaviour, or do something before the request is rendered
216
- # end
217
- # end
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
- # def edit
224
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
225
- # @resource = Product.find_by_slug params[:id]
226
- # end
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 [Wallaby::Forbidden] if user has no access
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
- # def update
248
- # # do something before the origin action
249
- # options = {} # NOTE: see `options` parameter for more details
250
- # update!(options) do |format| # NOTE: this is better than using `super`
251
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
252
- # # customize response behaviour, or do something before the request is rendered
253
- # end
254
- # end
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
- # def update
261
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
262
- # @resource = Product.find_by_slug params[:id]
263
- # @resource.assign_attributes resource_params.merge(new_arrival: true)
264
- # if @resource.save
265
- # redirect_to helper.index_path(current_model_class)
266
- # else
267
- # render :new
268
- # end
269
- # end
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}. In addition, options `:params` is supported, see below
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 [Wallaby::Forbidden] if user has no access
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
- # def destroy
290
- # # do something before the origin action
291
- # options = {} # NOTE: see `options` parameter for more details
292
- # destroy!(options) do |format| # NOTE: this is better than using `super`
293
- # # NOTE: this block is for `respond_with` which works similar to `respond_to`
294
- # # customize response behaviour, or do something before the request is rendered
295
- # end
296
- # end
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
- # def destroy
303
- # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
304
- # @resource = Product.find_by_slug params[:id]
305
- # @resource.destroy
306
- # redirect_to helper.index_path(current_model_class)
307
- # end
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}. In addition, options `:params` is supported, see below
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 [Wallaby::Forbidden] if user has no access
319
-
320
- # @!method resource_params
321
- # @note This is a template method that can be overridden by subclasses.
322
- # To whitelist the params for {#create} and {#update} actions.
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