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
@@ -1,78 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Field helper for model decorator
4
+ # Field helper method collection
5
+ # that takes care of `fields`, `field_names` related methods for model decorator
5
6
  module Fieldable
6
7
  # @param field_name [String, Symbol] field name
7
- # @return [Hash] metadata information for a given field
8
- def metadata_of(field_name)
9
- fields[field_name] || {}
8
+ # @param prefix [String]
9
+ # @return [ActiveSupport::HashWithIndifferentAccess] metadata
10
+ def metadata_of(field_name, prefix = '')
11
+ prefix_fields(prefix)[field_name] || {}
10
12
  end
13
+ alias_method :prefix_metadata_of, :metadata_of
11
14
 
12
15
  # @param field_name [String, Symbol] field name
13
- # @return [String] label for a given field
14
- def label_of(field_name)
15
- metadata_of(field_name)[:label] || field_name.to_s.humanize
16
+ # @param prefix [String]
17
+ # @return [String] label for the given field
18
+ def label_of(field_name, prefix = '')
19
+ metadata_of(field_name, prefix)[:label] || field_name.to_s.humanize
16
20
  end
21
+ alias_method :prefix_label_of, :label_of
17
22
 
18
23
  # @param field_name [String, Symbol] field name
19
- # @return [String, Symbol] type for a given field
20
- def type_of(field_name)
21
- ensure_type_is_present field_name, metadata_of(field_name)[:type]
24
+ # @param prefix [String]
25
+ # @return [String, Symbol] type for the given field
26
+ def type_of(field_name, prefix = '')
27
+ ensure_type_is_present field_name, metadata_of(field_name, prefix)[:type], prefix
22
28
  end
29
+ alias_method :prefix_type_of, :type_of
23
30
 
24
- # @param field_name [String, Symbol] field name
25
- # @return [Hash] index metadata information for a given field
26
- def index_metadata_of(field_name)
27
- index_fields[field_name] || {}
28
- end
29
-
30
- # @param field_name [String, Symbol] field name
31
- # @return [String] index label for a given field
32
- def index_label_of(field_name)
33
- index_metadata_of(field_name)[:label] || field_name.to_s.humanize
34
- end
35
-
36
- # @param field_name [String, Symbol] field name
37
- # @return [String, Symbol] index type for a given field
38
- def index_type_of(field_name)
39
- ensure_type_is_present field_name, index_metadata_of(field_name)[:type], 'index_'
31
+ # @param prefix [String]
32
+ # @return [ActiveSupport::HashWithIndifferentAccess] metadata
33
+ def prefix_fields(prefix)
34
+ variable = "@#{prefix}fields"
35
+ instance_variable_get(variable) || instance_variable_set(variable, Utils.clone(fields))
40
36
  end
41
37
 
42
- # @param field_name [String, Symbol] field name
43
- # @return [Hash] show metadata information for a given field
44
- def show_metadata_of(field_name)
45
- show_fields[field_name] || {}
38
+ # Set metadata for the given prefix
39
+ # @param fields [Hash] fields metadata
40
+ # @param prefix [String]
41
+ # @return [ActiveSupport::HashWithIndifferentAccess] metadata
42
+ def prefix_fields=(fields, prefix)
43
+ variable = "@#{prefix}fields"
44
+ instance_variable_set(variable, FieldsRegulator.new(fields).execute)
46
45
  end
47
46
 
48
- # @param field_name [String, Symbol] field name
49
- # @return [String] show label for a given field
50
- def show_label_of(field_name)
51
- show_metadata_of(field_name)[:label] || field_name.to_s.humanize
47
+ # @param prefix [String]
48
+ # @return [Array<String>] a list of field names
49
+ def prefix_field_names(prefix)
50
+ variable = "@#{prefix}field_names"
51
+ instance_variable_get(variable) || \
52
+ instance_variable_set(variable, begin
53
+ fields = prefix_fields(prefix).reject { |_k, metadata| metadata[:hidden] }
54
+ reposition(fields.keys, primary_key)
55
+ end)
52
56
  end
53
57
 
54
- # @param field_name [String, Symbol] field name
55
- # @return [String, Symbol] show type for a given field
56
- def show_type_of(field_name)
57
- ensure_type_is_present field_name, show_metadata_of(field_name)[:type], 'show_'
58
- end
59
-
60
- # @param field_name [String, Symbol] field name
61
- # @return [Hash] form metadata information for a given field
62
- def form_metadata_of(field_name)
63
- form_fields[field_name] || {}
64
- end
65
-
66
- # @param field_name [String, Symbol] field name
67
- # @return [String] form label for a given field
68
- def form_label_of(field_name)
69
- form_metadata_of(field_name)[:label] || field_name.to_s.humanize
70
- end
71
-
72
- # @param field_name [String, Symbol] field name
73
- # @return [String, Symbol] form type for a given field
74
- def form_type_of(field_name)
75
- ensure_type_is_present field_name, form_metadata_of(field_name)[:type], 'form_'
58
+ # Set field names for the given prefix
59
+ # @param field_names [Array<String>]
60
+ # @param prefix [String]
61
+ # @return [Array<String>] a list of field names
62
+ def prefix_field_names=(field_names, prefix)
63
+ variable = "@#{prefix}field_names"
64
+ instance_variable_set(variable, Array.wrap(field_names).flatten)
76
65
  end
77
66
  end
78
67
  end
@@ -3,58 +3,18 @@
3
3
  module Wallaby
4
4
  # Paginator related attributes
5
5
  module Paginatable
6
- # Configurable attribute
7
- module ClassMethods
8
- # @!attribute [w] model_paginator
9
- def model_paginator=(model_paginator)
10
- ModuleUtils.inheritance_check model_paginator, application_paginator
11
- @model_paginator = model_paginator
12
- end
13
-
14
- # @!attribute [r] model_paginator
15
- # If Wallaby doesn't get it right, please specify the **model_paginator**.
16
- # @example To set model paginator
17
- # class Admin::ProductionsController < Admin::ApplicationController
18
- # self.model_paginator = ProductPaginator
19
- # end
20
- # @return [Class] model paginator
21
- # @raise [ArgumentError] when **model_paginator** doesn't inherit from **application_paginator**
22
- # @see Wallaby::ModelPaginator
23
- # @since wallaby-5.2.0
24
- attr_reader :model_paginator
25
-
26
- # @!attribute [w] application_paginator
27
- def application_paginator=(application_paginator)
28
- ModuleUtils.inheritance_check model_paginator, application_paginator
29
- @application_paginator = application_paginator
30
- end
31
-
32
- # @!attribute [r] application_paginator
33
- # The **application_paginator** is as the base class of {#model_paginator}.
34
- # @example To set application decorator:
35
- # class Admin::ApplicationController < Wallaby::ResourcesController
36
- # self.application_paginator = AnotherApplicationPaginator
37
- # end
38
- # @return [Class] application decorator
39
- # @raise [ArgumentError] when **model_paginator** doesn't inherit from **application_paginator**
40
- # @see Wallaby::ModelPaginator
41
- # @since wallaby-5.2.0
42
- def application_paginator
43
- @application_paginator ||= ModuleUtils.try_to superclass, :application_paginator
44
- end
45
- end
46
-
47
- # Model paginator for current modal class. It comes from:
48
- #
49
- # - controller configuration {Wallaby::Paginatable::ClassMethods#model_paginator .model_paginator}
50
- # - a generic paginator based on {Wallaby::Paginatable::ClassMethods#application_paginator .application_paginator}
6
+ # Model paginator for current modal class.
51
7
  # @return [Class] model paginator class
8
+ # @see PaginatorFinder#execute
52
9
  def current_paginator
53
10
  @current_paginator ||=
54
- (controller_to_get(:model_paginator) \
55
- || Map.paginator_map(current_model_class, controller_to_get(:application_paginator))).try do |klass|
11
+ PaginatorFinder.new(
12
+ script_name: script_name,
13
+ model_class: current_model_class,
14
+ current_controller_class: wallaby_controller
15
+ ).execute.try do |klass|
56
16
  Logger.debug %(Current paginator: #{klass}), sourcing: false
57
- klass.new current_model_class, collection, params
17
+ klass.new current_model_class, collection, pagination_params_for(params)
58
18
  end
59
19
  end
60
20
 
@@ -64,9 +24,18 @@ module Wallaby
64
24
  # @param options [Hash]
65
25
  # @option options [Boolean] :paginate whether collection should be paginated
66
26
  # @return [#each]
67
- # @see Wallaby::ModelServicer#paginate
68
- def paginate(query, options)
69
- options[:paginate] ? current_servicer.paginate(query, params) : query
27
+ # @see ModelServicer#paginate
28
+ def paginate(query, options = { paginate: true })
29
+ return query unless options[:paginate]
30
+
31
+ current_servicer.paginate(query, pagination_params_for(params))
32
+ end
33
+
34
+ # @param params [Hash, ActionController::Parameters]
35
+ # @return [Hash, ActionController::Parameters]
36
+ def pagination_params_for(params)
37
+ params[:per] ||= wallaby_controller.page_size
38
+ params
70
39
  end
71
40
  end
72
41
  end
@@ -3,18 +3,38 @@
3
3
  module Wallaby
4
4
  # Field helper for model decorator
5
5
  module Prefixable
6
+ extend ActiveSupport::Concern
7
+
8
+ MAPPING_ACTIONS = {
9
+ new: 'form',
10
+ create: 'form',
11
+ edit: 'form',
12
+ update: 'form'
13
+ }.freeze
14
+
15
+ module ClassMethods
16
+ attr_writer :mapping_actions
17
+
18
+ def mapping_actions
19
+ @mapping_actions || superclass.try(:mapping_actions) || MAPPING_ACTIONS
20
+ end
21
+
22
+ def add_mapping_actions(options)
23
+ @mapping_actions = mapping_actions.merge(options)
24
+ end
25
+ end
26
+
6
27
  # @return [Array<String>] prefixes
7
- def _prefixes
28
+ def wallaby_prefixes
8
29
  override_prefixes(
9
- options: { mapping_actions: FORM_ACTIONS }
30
+ options: { mapping_actions: self.class.mapping_actions }
10
31
  ) do |prefixes|
11
32
  PrefixesBuilder.new(
33
+ controller_class: self.class,
12
34
  prefixes: prefixes,
13
35
  resources_name: current_resources_name,
14
36
  script_name: request.env[SCRIPT_NAME]
15
37
  ).execute
16
-
17
- prefixes[0..prefixes.index(ResourcesController.controller_path)]
18
38
  end
19
39
  end
20
40
  end
@@ -1,17 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Resources related attributes
4
+ # Resources related attributes & helper methods
5
5
  module Resourcable
6
6
  # @return [String] resources name for current request
7
7
  def current_resources_name
8
8
  @current_resources_name ||= params[:resources]
9
9
  end
10
10
 
11
+ # Model class for current request.
12
+ #
13
+ # It comes from two places:
14
+ #
15
+ # - configured {Baseable::ClassMethods#model_class .model_class}
16
+ # - fall back to the model class converted from either the {#current_resources_name}
17
+ # or
18
+ # {https://edgeapi.rubyonrails.org/classes/AbstractController/Base.html#method-c-controller_path .controller_path}
11
19
  # @return [Class] model class for current request
12
20
  def current_model_class
13
21
  @current_model_class ||=
14
- controller_to_get(__callee__, :model_class) || Map.model_class_map(current_resources_name || controller_path)
22
+ wallaby_controller.model_class || Map.model_class_map(current_resources_name || controller_path)
15
23
  end
16
24
 
17
25
  # Shorthand of params[:id]
@@ -21,98 +29,148 @@ module Wallaby
21
29
  end
22
30
 
23
31
  # @note This is a template method that can be overridden by subclasses.
24
- # This is a method to return collection for index page.
32
+ # It's used by {#create_params} and {#update_params}.
33
+ #
34
+ # It can be replaced completely in subclasses
35
+ # and will impact both {#create_params} and {#update_params}:
36
+ #
37
+ # def resource_params
38
+ # params.fetch(:product, {}).permit(:name, :sku)
39
+ # end
40
+ # @return [ActionController::Parameters] allowlisted params
41
+ def resource_params
42
+ @resource_params ||= current_servicer.permit params, action_name
43
+ end
44
+
45
+ # @note This is a template method that can be overridden by subclasses.
46
+ # To allowlist the request params for {ResourcesConcern#create create} action.
47
+ #
48
+ # It can be replaced completely in subclasses:
49
+ #
50
+ # def create_params
51
+ # params.fetch(:product, {}).permit(:name, :sku)
52
+ # end
53
+ # @return [ActionController::Parameters] allowlisted params
54
+ def create_params
55
+ resource_params
56
+ end
57
+
58
+ # @note This is a template method that can be overridden by subclasses.
59
+ # To allowlist the request params for {ResourcesConcern#update update} action.
60
+ #
61
+ # It can be replaced completely in subclasses:
62
+ #
63
+ # def update_params
64
+ # params.fetch(:product, {}).permit(:name, :sku)
65
+ # end
66
+ # @return [ActionController::Parameters] allowlisted params
67
+ def update_params
68
+ resource_params
69
+ end
70
+
71
+ # @note This is a template method that can be overridden by subclasses.
72
+ # This is a method to return collection for {ResourcesConcern#index index} action and page.
25
73
  #
26
74
  # It can be customized as below in subclasses:
27
75
  #
28
- # ```
29
- # def collection
30
- # # do something before the origin action
31
- # options = {} # NOTE: see `options` parameter for more details
32
- # collection! options do |query| # NOTE: this is better than using `super`
33
- # # NOTE: make sure a collection is returned
34
- # query.where(active: true)
35
- # end
36
- # end
37
- # ```
76
+ # def collection
77
+ # # do something before the original action
78
+ # options = {} # @see arguments for more details
79
+ # # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
80
+ # collection!(options) do |query|
81
+ # # NOTE: make sure to return a collection in the block
82
+ # query.where(active: true)
83
+ # end
84
+ # end
38
85
  #
39
86
  # Otherwise, it can be replaced completely in subclasses:
40
87
  #
41
- # ```
42
- # def collection
43
- # # NOTE: pagination should happen here if needed
44
- # # NOTE: make sure `@collection` and conditional assignment (the OR EQUAL) operator are used
45
- # @collection ||= paginate Product.active
46
- # end
47
- # ```
48
- # @param options [Hash] (since 5.2.0)
49
- # @option options [ActionController::Parameters, Hash] :params parameters for collection query
50
- # @option options [Boolean] :paginate see {Wallaby::Paginatable#paginate}
51
- # @yield [collection] (since 5.2.0) a block to run to extend collection, e.g. call chain with more queries
88
+ # def collection
89
+ # # NOTE: pagination should happen here if needed
90
+ # # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
91
+ # @collection ||= paginate Product.active
92
+ # end
93
+ # @param params [Hash, ActionController::Parameters] parameters for collection query, default to request parameters
94
+ # @param paginate [Boolean] see {Paginatable#paginate #paginate}
95
+ # @param paginate_options [Hash] options accepted by {Paginatable#paginate #paginate} (since 0.3.0)
96
+ # @yield [collection] (since wallaby-5.2.0) a block to run to extend/convert the original collection,
97
+ # e.g. call chain with more queries
52
98
  # @return [#each] a collection of records
53
- def collection(options = {}, &block)
99
+ def collection(params: self.params, paginate: true, **paginate_options, &block)
54
100
  @collection ||=
55
101
  ModuleUtils.yield_for(
56
- begin
57
- options[:paginate] = true unless options.key?(:paginate)
58
- options[:params] ||= params
59
- paginate current_servicer.collection(options.delete(:params)), options
60
- end,
102
+ paginate(
103
+ current_servicer.collection(params),
104
+ paginate_options.merge(paginate: paginate)
105
+ ),
61
106
  &block
62
107
  )
63
108
  end
64
-
65
- alias collection! collection
109
+ alias_method :collection!, :collection
66
110
 
67
111
  # @note This is a template method that can be overridden by subclasses.
68
- # This is a method to return resource for pages except `index`.
69
- #
70
- # `WARN: It does not do mass assignment since 5.2.0.`
112
+ # This is a method to return resource for
113
+ # {ResourcesConcern#show show}, {ResourcesConcern#edit edit},
114
+ # {ResourcesConcern#update update} and {ResourcesConcern#destroy destroy} actions.
71
115
  #
72
116
  # It can be customized as below in subclasses:
73
117
  #
74
- # ```
75
- # def resource
76
- # # do something before the origin action
77
- # options = {} # NOTE: see `options` parameter for more details
78
- # resource! options do |object| # NOTE: this is better than using `super`
79
- # object.preload_status_from_api
80
- # # NOTE: make sure object is returned
81
- # object
82
- # end
83
- # end
84
- # ```
118
+ # def resource
119
+ # # do something before the original action
120
+ # # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
121
+ # resource! do |object|
122
+ # object.preload_status_from_api
123
+ # # NOTE: make sure to return an object in the block
124
+ # object
125
+ # end
126
+ # end
85
127
  #
86
128
  # Otherwise, it can be replaced completely in subclasses:
87
129
  #
88
- # ```
89
- # def resource
90
- # # NOTE: make sure `@resource` and conditional assignment (the OR EQUAL) operator are used
91
- # @resource ||= resource_id.present? ? Product.find_by_slug(resource_id) : Product.new(arrival: true)
92
- # end
93
- # ```
94
- # @param options [Hash] (since 5.2.0)
95
- # @option options [Array<String>] :non_find_actions action names that shouldn't use resource find.
96
- # (Default to `%w(index new create)`)
97
- # @option options [ActionController::Parameters, Hash] :find_params parameters/options for resource finding
98
- # @option options [ActionController::Parameters, Hash] :new_params parameters/options for new resource
99
- # @yield [resource] (since 5.2.0) a block to run to extend resource, e.g. making change to the resource.
100
- # Please make sure to return the resource at the end of block
101
- # @return [Object] either persisted or unpersisted resource instance
102
- # @raise [ResourceNotFound] if resource is nil
103
- def resource(options = {}, &block)
130
+ # def resource
131
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
132
+ # @resource ||= resource_id.present? ? Product.find_by_slug(resource_id) : Product.new(arrival: true)
133
+ # end
134
+ # @yield [resource] (since wallaby-5.2.0) a block to run to extend resource, e.g. making change to the resource.
135
+ # Please make sure to return the expected resource at the end of block
136
+ # @return [Object] persisted resource instance
137
+ # @raise [ResourceNotFound] if resource is not found
138
+ def resource(&block)
104
139
  @resource ||=
105
- ModuleUtils.yield_for(
106
- # this will testify both resource and resources
107
- if resource_id.present? || !(options[:non_find_actions] || NON_FIND_ACTIONS).include?(action_name)
108
- current_servicer.find resource_id, options[:find_params]
109
- else
110
- current_servicer.new options[:new_params]
111
- end,
112
- &block
113
- )
140
+ ModuleUtils.yield_for(current_servicer.find(resource_id, {}), &block)
114
141
  end
142
+ alias_method :resource!, :resource
115
143
 
116
- alias resource! resource
144
+ # @note This is a template method that can be overridden by subclasses.
145
+ # This is a method to return resource for
146
+ # {ResourcesConcern#new new} and {ResourcesConcern#create create} actions.
147
+ #
148
+ # It can be customized as below in subclasses:
149
+ #
150
+ # def new_resource
151
+ # # do something before the original action
152
+ # # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
153
+ # new_resource! do |object|
154
+ # object.preload_status_from_api
155
+ # # NOTE: make sure to return an object in the block
156
+ # object
157
+ # end
158
+ # end
159
+ #
160
+ # Otherwise, it can be replaced completely in subclasses:
161
+ #
162
+ # def new_resource
163
+ # # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
164
+ # @resource ||= Product.new(arrival: true)
165
+ # end
166
+ # @yield [resource] (since wallaby-5.2.0) a block to run to extend resource, e.g. making change to the resource.
167
+ # Please make sure to return the expected resource at the end of block
168
+ # @return [Object] unpersisted resource instance
169
+ # @since 0.3.0
170
+ def new_resource(&block)
171
+ @resource ||=
172
+ ModuleUtils.yield_for(current_servicer.new({}), &block)
173
+ end
174
+ alias_method :new_resource!, :new_resource
117
175
  end
118
176
  end