wallaby-core 0.2.11 → 0.3.0.beta1

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 +24 -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 +72 -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 +2 -2
  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 +13 -80
  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 +7 -2
  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 +4 -4
  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 +2 -19
  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 +9 -31
  132. metadata +120 -15
  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
@@ -0,0 +1,416 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Configurable related
5
+ module Configurable
6
+ extend ActiveSupport::Concern
7
+
8
+ # Engine configurables
9
+ module ClassMethods
10
+ # @!attribute [w] engine_name
11
+ attr_writer :engine_name
12
+
13
+ # @!attribute [r] engine_name
14
+ # It is used to help with URLs handling (see {Engine})
15
+ #
16
+ # So when to set this engine name?
17
+ # When {Wallaby} doesn't know what is the correct engine helper to use for current path.
18
+ # @example To set an engine name:
19
+ # class Admin::ApplicationController < Wallaby::ResourcesController
20
+ # self.engine_name = 'admin_engine'
21
+ # end
22
+ # @return [String, Symbol, nil] engine name
23
+ # @since wallaby-5.2.0
24
+ def engine_name
25
+ @engine_name || superclass.try(:engine_name)
26
+ end
27
+ end
28
+
29
+ # Controller configurables
30
+ module ClassMethods
31
+ # @!attribute [r] application_controller
32
+ # @return [Class] application controller class
33
+ def application_controller
34
+ return self if base_class?
35
+
36
+ superclass.try(:application_controller) || ResourcesController
37
+ end
38
+ end
39
+
40
+ # Decorator configurables
41
+ module ClassMethods
42
+ # @!attribute [w] resource_decorator
43
+ attr_writer :resource_decorator
44
+
45
+ # @!attribute [r] resource_decorator
46
+ # @example To set resource decorator
47
+ # class Admin::ProductionsController < Admin::ApplicationController
48
+ # self.resource_decorator = ProductDecorator
49
+ # end
50
+ # @return [Class] resource decorator
51
+ # @see ResourceDecorator
52
+ # @since wallaby-5.2.0
53
+ def resource_decorator
54
+ @resource_decorator ||= Guesser.class_for(name, suffix: DECORATOR, &:model_class)
55
+ end
56
+
57
+ # @!attribute [w] application_decorator
58
+ attr_writer :application_decorator
59
+
60
+ # @!attribute [r] application_decorator
61
+ # It is the base class of {#resource_decorator}.
62
+ # @example To set application decorator:
63
+ # class Admin::ApplicationController < Wallaby::ResourcesController
64
+ # self.application_decorator = AnotherApplicationDecorator
65
+ # end
66
+ # @return [Class] application decorator
67
+ # @see ResourceDecorator
68
+ # @since wallaby-5.2.0
69
+ def application_decorator
70
+ @application_decorator ||= Guesser.class_for(name, suffix: DECORATOR, &:base_class?)
71
+ @application_decorator || superclass.try(:application_decorator) || ResourceDecorator
72
+ end
73
+ end
74
+
75
+ # Servicer configurables
76
+ module ClassMethods
77
+ # @!attribute [w] model_servicer
78
+ attr_writer :model_servicer
79
+
80
+ # @!attribute [r] model_servicer
81
+ # @example To set model servicer
82
+ # class Admin::ProductionsController < Admin::ApplicationController
83
+ # self.model_servicer = ProductServicer
84
+ # end
85
+ # @return [Class] model servicer
86
+ # @see ModelServicer
87
+ # @since wallaby-5.2.0
88
+ def model_servicer
89
+ @model_servicer ||= Guesser.class_for(name, suffix: SERVICER, &:model_class)
90
+ end
91
+
92
+ # @!attribute [w] application_servicer
93
+ attr_writer :application_servicer
94
+
95
+ # @!attribute [r] application_servicer
96
+ # It is the base class of {#model_servicer}.
97
+ # @example To set application servicer:
98
+ # class Admin::ApplicationController < Wallaby::ResourcesController
99
+ # self.application_servicer = AnotherApplicationServicer
100
+ # end
101
+ # @return [Class] application servicer
102
+ # @see ModelServicer
103
+ # @since wallaby-5.2.0
104
+ def application_servicer
105
+ @application_servicer ||= Guesser.class_for(name, suffix: SERVICER, &:base_class?)
106
+ @application_servicer || superclass.try(:application_servicer) || ModelServicer
107
+ end
108
+ end
109
+
110
+ # Authorizer configurables
111
+ module ClassMethods
112
+ # @!attribute [w] model_authorizer
113
+ attr_writer :model_authorizer
114
+
115
+ # @!attribute [r] model_authorizer
116
+ # @example To set model authorizer
117
+ # class Admin::ProductionsController < Admin::ApplicationController
118
+ # self.model_authorizer = ProductAuthorizer
119
+ # end
120
+ # @return [Class] model authorizer
121
+ # @see ModelAuthorizer
122
+ # @since wallaby-5.2.0
123
+ def model_authorizer
124
+ @model_authorizer ||= Guesser.class_for(name, suffix: AUTHORIZER, &:model_class)
125
+ end
126
+
127
+ # @!attribute [w] application_authorizer
128
+ attr_writer :application_authorizer
129
+
130
+ # @!attribute [r] application_authorizer
131
+ # It is the base class of {#model_authorizer}.
132
+ # @example To set application authorizer:
133
+ # class Admin::ApplicationController < Wallaby::ResourcesController
134
+ # self.application_authorizer = AnotherApplicationAuthorizer
135
+ # end
136
+ # @return [Class] application authorizer
137
+ # @see ModelAuthorizer
138
+ # @since wallaby-5.2.0
139
+ def application_authorizer
140
+ @application_authorizer ||= Guesser.class_for(name, suffix: AUTHORIZER, &:base_class?)
141
+ @application_authorizer || superclass.try(:application_authorizer) || ModelAuthorizer
142
+ end
143
+ end
144
+
145
+ # Paginator configurables
146
+ module ClassMethods
147
+ # @!attribute [w] model_paginator
148
+ attr_writer :model_paginator
149
+
150
+ # @!attribute [r] model_paginator
151
+ # @example To set model paginator
152
+ # class Admin::ProductionsController < Admin::ApplicationController
153
+ # self.model_paginator = ProductPaginator
154
+ # end
155
+ # @return [Class] model paginator
156
+ # @see ModelPaginator
157
+ # @since wallaby-5.2.0
158
+ def model_paginator
159
+ @model_paginator ||= Guesser.class_for(name, suffix: PAGINATOR, &:model_class)
160
+ end
161
+
162
+ # @!attribute [w] application_paginator
163
+ attr_writer :application_paginator
164
+
165
+ # @!attribute [r] application_paginator
166
+ # It is the base class of {#model_paginator}.
167
+ # @example To set application paginator:
168
+ # class Admin::ApplicationController < Wallaby::ResourcesController
169
+ # self.application_paginator = AnotherApplicationPaginator
170
+ # end
171
+ # @return [Class] application paginator
172
+ # @see ModelPaginator
173
+ # @since wallaby-5.2.0
174
+ def application_paginator
175
+ @application_paginator ||= Guesser.class_for(name, suffix: PAGINATOR, &:base_class?)
176
+ @application_paginator || superclass.try(:application_paginator) || ModelPaginator
177
+ end
178
+ end
179
+
180
+ # Models configurables
181
+ module ClassMethods
182
+ # @!attribute [r] models
183
+ # To configure the models that the controller should be handling.
184
+ # It takes both Class and Class String.
185
+ # @example To update the models in `Admin::ApplicationController`
186
+ # class Admin::ApplicationController < Wallaby::ResourcesController
187
+ # self.models = User, 'Product'
188
+ # end
189
+ # @since 0.2.3
190
+ def models
191
+ @models || superclass.try(:models) || ClassArray.new
192
+ end
193
+
194
+ # @!attribute [w] models
195
+ def models=(*models)
196
+ @models = ClassArray.new models.flatten
197
+ end
198
+
199
+ # @note If models are allowlisted using {#models}, models exclusion will NOT be applied.
200
+ # @!attribute [r] models_to_exclude
201
+ # To configure the models to exclude that the controller should be handling.
202
+ # It takes both Class and Class String.
203
+ # @example To update the models to exclude in `Admin::ApplicationController`
204
+ # class Admin::ApplicationController < Wallaby::ResourcesController
205
+ # self.models_to_exclude = User, 'Product'
206
+ # end
207
+ # @since 0.2.3
208
+ def models_to_exclude
209
+ @models_to_exclude ||
210
+ superclass.try(:models_to_exclude) ||
211
+ DefaultModelsExcluder.execute
212
+ end
213
+
214
+ # @!attribute [w] models_to_exclude
215
+ def models_to_exclude=(*models_to_exclude)
216
+ @models_to_exclude = ClassArray.new models_to_exclude.flatten
217
+ end
218
+
219
+ # @return [Array<Class>] all models
220
+ def all_models
221
+ ModelClassFilter.execute(
222
+ all: Map.mode_map.keys,
223
+ allowlisted: models.origin,
224
+ denylisted: models_to_exclude.origin
225
+ )
226
+ end
227
+ end
228
+
229
+ # Authentication configurables
230
+ module ClassMethods
231
+ # @!attribute [r] logout_path
232
+ # To configure the logout path.
233
+ #
234
+ # Wallaby does not implement any authentication (e.g. login/logout), therefore, logout path will be required
235
+ # so that Wallaby knows where to navigate the user to when user clicks the logout button.
236
+ #
237
+ # But once it detects `Devise`, it will use the path that Devise uses without the need of configuration.
238
+ # @example To update the logout path in `Admin::ApplicationController`
239
+ # class Admin::ApplicationController < Wallaby::ResourcesController
240
+ # self.logout_path = 'destroy_admin_user_session_path'
241
+ # end
242
+ # @since 0.2.3
243
+ def logout_path
244
+ @logout_path || superclass.try(:logout_path)
245
+ end
246
+
247
+ # @!attribute [w] logout_path
248
+ def logout_path=(logout_path)
249
+ case logout_path
250
+ when String, Symbol, nil
251
+ @logout_path = logout_path
252
+ else
253
+ raise ArgumentError, 'Please provide a String/Symbol value or nil'
254
+ end
255
+ end
256
+
257
+ # @!attribute [r] logout_method
258
+ # To configure the logout HTTP method.
259
+ #
260
+ # Wallaby does not implement any authentication (e.g. login/logout), therefore, logout method will be required
261
+ # so that Wallaby knows how navigate the user via what HTTP method when user clicks the logout button.
262
+ #
263
+ # But once it detects `Devise`, it will use the HTTP method that Devise uses without the need of configuration.
264
+ # @example To update the logout method in `Admin::ApplicationController`
265
+ # class Admin::ApplicationController < Wallaby::ResourcesController
266
+ # self.logout_method = 'put'
267
+ # end
268
+ # @since 0.2.3
269
+ def logout_method
270
+ @logout_method || superclass.try(:logout_method)
271
+ end
272
+
273
+ # @!attribute [w] logout_method
274
+ def logout_method=(logout_method)
275
+ normalized = logout_method.present? ? logout_method.to_s.downcase : nil
276
+ rfc2616 = ActionDispatch::Request::RFC2616.map(&:downcase)
277
+
278
+ if normalized.nil? || rfc2616.include?(normalized)
279
+ @logout_method = logout_method
280
+ return
281
+ end
282
+
283
+ raise ArgumentError, "Please provide valid RFC2616 HTTP method (e.g. #{rfc2616.join(', ')}) or nil"
284
+ end
285
+
286
+ # @!attribute [r] email_method
287
+ # To configure the method on
288
+ # {AuthenticationConcern#wallaby_user} to retrieve email address.
289
+ #
290
+ # If no configuration is given, it will attempt to call `email` on
291
+ # {AuthenticationConcern#wallaby_user}.
292
+ # @example To update the email method in `Admin::ApplicationController`
293
+ # class Admin::ApplicationController < Wallaby::ResourcesController
294
+ # self.email_method = 'email_address'
295
+ # end
296
+ # @since 0.2.3
297
+ def email_method
298
+ @email_method || superclass.try(:email_method)
299
+ end
300
+
301
+ # @!attribute [w] email_method
302
+ def email_method=(email_method)
303
+ case email_method
304
+ when String, Symbol, nil
305
+ @email_method = email_method
306
+ else
307
+ raise ArgumentError, 'Please provide a String/Symbol value or nil'
308
+ end
309
+ end
310
+ end
311
+
312
+ # Metadata configurables
313
+ module ClassMethods
314
+ # @!attribute [r] max_text_length
315
+ # To configure max number of characters to truncate for each text field on index page.
316
+ # @example To update the email method in `Admin::ApplicationController`
317
+ # class Admin::ApplicationController < Wallaby::ResourcesController
318
+ # self.max_text_length = 50
319
+ # end
320
+ # @return [Integer] max number of characters to truncate, default to 20
321
+ # @see DEFAULT_MAX
322
+ # @since 0.2.3
323
+ def max_text_length
324
+ @max_text_length || superclass.try(:max_text_length) || DEFAULT_MAX
325
+ end
326
+
327
+ # @!attribute [w] max_text_length
328
+ def max_text_length=(max_text_length)
329
+ case max_text_length
330
+ when Integer, nil
331
+ @max_text_length = max_text_length
332
+ else
333
+ raise ArgumentError, 'Please provide a Integer value or nil'
334
+ end
335
+ end
336
+ end
337
+
338
+ # Pagination configurables
339
+ module ClassMethods
340
+ # @!attribute [r] page_size
341
+ # To configure the page size for pagination on index page.
342
+ #
343
+ # Page size can be one of the following values:
344
+ #
345
+ # - 10
346
+ # - 20
347
+ # - 50
348
+ # - 100
349
+ # @example To update the email method in `Admin::ApplicationController`
350
+ # class Admin::ApplicationController < Wallaby::ResourcesController
351
+ # self.page_size = 50
352
+ # end
353
+ # @return [Integer] page size, default to 20
354
+ # @see PERS
355
+ # @see DEFAULT_PAGE_SIZE
356
+ # @since 0.2.3
357
+ def page_size
358
+ @page_size || superclass.try(:page_size) || DEFAULT_PAGE_SIZE
359
+ end
360
+
361
+ # @!attribute [w] page_size
362
+ def page_size=(page_size)
363
+ case page_size
364
+ when Integer, nil
365
+ @page_size = page_size
366
+ else
367
+ raise ArgumentError, 'Please provide a Integer value or nil'
368
+ end
369
+ end
370
+ end
371
+
372
+ # Sorting configurables
373
+ module ClassMethods
374
+ # @!attribute [r] sorting_strategy
375
+ # To configure which strategy to use for sorting on index page. Options are
376
+ #
377
+ # - `:multiple`: support multiple columns sorting
378
+ # - `:single`: support single column sorting
379
+ # @example To update the email method in `Admin::ApplicationController`
380
+ # class Admin::ApplicationController < Wallaby::ResourcesController
381
+ # self.sorting_strategy = :single
382
+ # end
383
+ # @return [Integer] sorting strategy, default to `:multiple`
384
+ # @see PERS
385
+ # @since 0.2.3
386
+ def sorting_strategy
387
+ @sorting_strategy || superclass.try(:sorting_strategy) || :multiple
388
+ end
389
+
390
+ # @!attribute [w] sorting_strategy
391
+ def sorting_strategy=(sorting_strategy)
392
+ case sorting_strategy
393
+ when :multiple, :single, nil
394
+ @sorting_strategy = sorting_strategy
395
+ else
396
+ raise ArgumentError, 'Please provide a value of :multiple, :single or nil'
397
+ end
398
+ end
399
+ end
400
+
401
+ # Clear configurables
402
+ module ClassMethods
403
+ # Clear all configurations
404
+ def clear
405
+ ClassMethods.instance_methods.grep(/=/).each do |name|
406
+ instance_variable_set "@#{name[0...-1]}", nil
407
+ end
408
+ end
409
+ end
410
+
411
+ # @return [Class] its controller class
412
+ def wallaby_controller
413
+ @wallaby_controller ||= try(:controller).try(:class) || self.class
414
+ end
415
+ end
416
+ end
@@ -1,73 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Decorator related attributes
4
+ # Decorator related
5
5
  module Decoratable
6
- # Configurable attribute
7
- module ClassMethods
8
- # @!attribute [w] resource_decorator
9
- def resource_decorator=(resource_decorator)
10
- ModuleUtils.inheritance_check resource_decorator, application_decorator
11
- @resource_decorator = resource_decorator
12
- end
13
-
14
- # @!attribute [r] resource_decorator
15
- # Resource decorator will be used for its metadata info and decoration methods.
16
- #
17
- # If Wallaby doesn't get it right, please specify the **resource_decorator**.
18
- # @example To set resource decorator
19
- # class Admin::ProductionsController < Admin::ApplicationController
20
- # self.resource_decorator = ProductDecorator
21
- # end
22
- # @return [Class] resource decorator
23
- # @raise [ArgumentError] when **resource_decorator** doesn't inherit from **application_decorator**
24
- # @see Wallaby::ResourceDecorator
25
- # @since wallaby-5.2.0
26
- attr_reader :resource_decorator
27
-
28
- # @!attribute [w] application_decorator
29
- def application_decorator=(application_decorator)
30
- ModuleUtils.inheritance_check resource_decorator, application_decorator
31
- @application_decorator = application_decorator
32
- end
33
-
34
- # @!attribute [r] application_decorator
35
- # The **application_decorator** is as the base class of {#resource_decorator}.
36
- # @example To set application decorator:
37
- # class Admin::ApplicationController < Wallaby::ResourcesController
38
- # self.application_decorator = AnotherApplicationDecorator
39
- # end
40
- # @raise [ArgumentError] when **resource_decorator** doesn't inherit from **application_decorator**
41
- # @return [Class] application decorator
42
- # @see Wallaby::ResourceDecorator
43
- # @since wallaby-5.2.0
44
- def application_decorator
45
- @application_decorator ||= ModuleUtils.try_to superclass, :application_decorator
46
- end
47
- end
48
-
49
6
  # Get current model decorator. It comes from
50
7
  #
51
- # - model decorator for {Wallaby::Decoratable::ClassMethods#resource_decorator resource_decorator}
52
- # - otherwise, model decorator for {Wallaby::Decoratable::ClassMethods#application_decorator application_decorator}
8
+ # - model decorator for {Configurable::ClassMethods#resource_decorator resource_decorator}
9
+ # - otherwise, model decorator for {Configurable::ClassMethods#application_decorator application_decorator}
53
10
  #
54
11
  # Model decorator stores the information of **metadata** and **field_names** for **index**/**show**/**form** action.
55
- # @return [Wallaby::ModelDecorator] current model decorator for this request
12
+ # @return [ModelDecorator] current model decorator for this request
56
13
  def current_model_decorator
57
14
  @current_model_decorator ||=
58
15
  current_decorator.try(:model_decorator) || \
59
- Map.model_decorator_map(current_model_class, controller_to_get(:application_decorator))
16
+ Map.model_decorator_map(current_model_class, wallaby_controller.application_decorator)
60
17
  end
61
18
 
62
19
  # Get current resource decorator. It comes from
63
20
  #
64
- # - {Wallaby::Decoratable::ClassMethods#resource_decorator resource_decorator}
65
- # - otherwise, {Wallaby::Decoratable::ClassMethods#application_decorator application_decorator}
66
- # @return [Wallaby::ResourceDecorator] current resource decorator for this request
21
+ # - {Configurable::ClassMethods#resource_decorator resource_decorator}
22
+ # - otherwise, {Configurable::ClassMethods#application_decorator application_decorator}
23
+ # @return [ResourceDecorator] current resource decorator for this request
67
24
  def current_decorator
68
25
  @current_decorator ||=
69
- (controller_to_get(:resource_decorator) || \
70
- Map.resource_decorator_map(current_model_class, controller_to_get(:application_decorator))).tap do |decorator|
26
+ DecoratorFinder.new(
27
+ script_name: script_name,
28
+ model_class: current_model_class,
29
+ current_controller_class: wallaby_controller
30
+ ).execute.tap do |decorator|
71
31
  Logger.debug %(Current decorator: #{decorator}), sourcing: false
72
32
  end
73
33
  end
@@ -76,24 +36,28 @@ module Wallaby
76
36
  # @return [Hash] current fields metadata
77
37
  def current_fields
78
38
  @current_fields ||=
79
- ModuleUtils.try_to current_model_decorator, :"#{FORM_ACTIONS[action_name] || action_name}_fields"
39
+ current_model_decorator.try(:"#{action_name}_fields")
80
40
  end
81
41
 
82
42
  # Wrap resource(s) with decorator(s).
83
43
  # @param resource [Object, Enumerable]
84
- # @return [Wallaby::ResourceDecorator, Enumerable<Wallaby::ResourceDecorator>] decorator(s)
44
+ # @return [ResourceDecorator, Enumerable<Wallaby::ResourceDecorator>] decorator(s)
85
45
  def decorate(resource)
86
- return resource if resource.is_a? ResourceDecorator
87
- return resource.map { |item| decorate item } if resource.respond_to? :map
46
+ return resource if resource.is_a?(ResourceDecorator)
47
+ return resource.map { |item| decorate(item) } if resource.respond_to?(:map)
48
+ return resource unless Map.mode_map[resource.class]
88
49
 
89
- decorator = Map.resource_decorator_map resource.class, controller_to_get(:application_decorator)
90
- decorator ? decorator.new(resource) : resource
50
+ DecoratorFinder.new(
51
+ script_name: script_name,
52
+ model_class: resource.class,
53
+ current_controller_class: wallaby_controller
54
+ ).execute.new(resource)
91
55
  end
92
56
 
93
57
  # @param resource [Object, Wallaby::ResourceDecorator]
94
58
  # @return [Object] the unwrapped resource object
95
59
  def extract(resource)
96
- return resource.resource if resource.is_a? ResourceDecorator
60
+ return resource.resource if resource.is_a?(ResourceDecorator)
97
61
 
98
62
  resource
99
63
  end
@@ -1,61 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Engine related helper methods for both controller and view
4
+ # {Engine} related helper methods to be included for both controller and view.
5
5
  module Engineable
6
- # Configurable attribute
7
- module ClassMethods
8
- # @!attribute [w] engine_name
9
- attr_writer :engine_name
10
-
11
- # @!attribute [r] engine_name
12
- # The engine name will be used to handle URLs.
13
- #
14
- # So when to set this engine name? When Wallaby doesn't know what is the correct engine helper to use.
15
- # @example To set an engine name:
16
- # class Admin::ApplicationController < Wallaby::ResourcesController
17
- # self.engine_name = 'admin_engine'
18
- # end
19
- # @return [String, Symbol, nil] engine name
20
- # @since wallaby-5.2.0
21
- def engine_name
22
- @engine_name ||= ModuleUtils.try_to superclass, :engine_name
23
- end
24
- end
25
-
26
- # This engine helper is used to access URL helpers of Wallaby engine.
6
+ # This helper method returns the current {Engine} routing proxy.
7
+ # For example, if {Wallaby} is mounted at different paths at the same time:
27
8
  #
28
- # Considering **Wallaby** is mounted at the following paths:
9
+ # mount Wallaby::Engine, at: '/admin'
10
+ # mount Wallaby::Engine, at: '/inner', as: :inner_engine,
11
+ # defaults: { resources_controller: InnerController }
29
12
  #
30
- # ```
31
- # mount Wallaby::Engine, at: '/admin'
32
- # mount Wallaby::Engine, at: '/inner', as: :inner_engine, defaults: { resources_controller: InnerController }
33
- # ```
34
- #
35
- # If `/inner` is current script name, `current_engine` is same as `inner_engine`.
36
- # Then it's possible to access URL helpers like this:
37
- #
38
- # ```
39
- # current_engine.resources_path resources: 'products'
40
- # ```
41
- # @return [ActionDispatch::Routing::RoutesProxy] engine for current request
13
+ # If `/inner` is current request path, it returns `inner_engine` engine proxy.
14
+ # @return [ActionDispatch::Routing::RoutesProxy] engine proxy for current request
42
15
  def current_engine
43
- @current_engine ||= ModuleUtils.try_to self, current_engine_name
16
+ @current_engine ||= try current_engine_name
44
17
  end
45
18
 
46
- # Find out the engine name under current script name.
47
- #
48
- # Considering **Wallaby** is mounted at the following paths:
19
+ # Find out the {Engine} routing proxy name for the current request, it comes from either:
49
20
  #
50
- # ```
51
- # mount Wallaby::Engine, at: '/admin'
52
- # mount Wallaby::Engine, at: '/inner', as: :inner_engine, defaults: { resources_controller: InnerController }
53
- # ```
54
- #
55
- # If `/inner` is current script name, then `current_engine_name` returns `'inner_engine'`.
21
+ # - Current controller's {Configurable::ClassMethods#engine_name engine_name}
22
+ # - Judge from the current request path (which contains the script and path info)
56
23
  # @return [String] engine name for current request
24
+ # @see EngineNameFinder.execute
57
25
  def current_engine_name
58
- @current_engine_name ||= controller_to_get(__callee__, :engine_name) || EngineNameFinder.find(request.env)
26
+ @current_engine_name ||= wallaby_controller.engine_name || EngineNameFinder.execute(request.path)
27
+ end
28
+
29
+ # @return [ActionDispatch::Journey::Route] engine route for current request
30
+ def current_engine_route
31
+ return if current_engine_name.blank?
32
+
33
+ Rails.application.routes.named_routes[current_engine_name]
34
+ end
35
+
36
+ # @note This script name prefix is required for Rails
37
+ # {https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for #url_for}
38
+ # to generate the correct URL.
39
+ # @return [String] current engine's script name
40
+ def script_name
41
+ current_engine_route.try { |route| route.path.spec.to_s }
59
42
  end
60
43
  end
61
44
  end