wallaby-core 0.2.11 → 0.3.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/wallaby/resources_controller.rb +20 -7
- data/app/security/ability.rb +1 -1
- data/config/routes.rb +21 -14
- data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_decorator.rb +5 -17
- data/lib/adaptors/wallaby/custom/model_finder.rb +3 -3
- data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -1
- data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +12 -5
- data/lib/authorizers/wallaby/default_authorization_provider.rb +10 -1
- data/lib/authorizers/wallaby/model_authorizer.rb +41 -16
- data/lib/authorizers/wallaby/pundit_authorization_provider.rb +22 -8
- data/lib/concerns/wallaby/application_concern.rb +41 -71
- data/lib/concerns/wallaby/authentication_concern.rb +29 -127
- data/lib/concerns/wallaby/authorizable.rb +14 -57
- data/lib/concerns/wallaby/baseable.rb +24 -57
- data/lib/concerns/wallaby/configurable.rb +416 -0
- data/lib/concerns/wallaby/decoratable.rb +29 -60
- data/lib/concerns/wallaby/engineable.rb +29 -46
- data/lib/concerns/wallaby/fieldable.rb +45 -56
- data/lib/concerns/wallaby/paginatable.rb +20 -51
- data/lib/concerns/wallaby/prefixable.rb +24 -4
- data/lib/concerns/wallaby/resourcable.rb +130 -72
- data/lib/concerns/wallaby/resources_concern.rb +205 -305
- data/lib/concerns/wallaby/servicable.rb +8 -48
- data/lib/concerns/wallaby/urlable.rb +69 -0
- data/lib/decorators/wallaby/resource_decorator.rb +91 -34
- data/lib/errors/wallaby/class_not_found.rb +1 -2
- data/lib/errors/wallaby/forbidden.rb +1 -2
- data/lib/errors/wallaby/general_error.rb +1 -1
- data/lib/errors/wallaby/invalid_error.rb +1 -2
- data/lib/errors/wallaby/method_removed.rb +5 -0
- data/lib/errors/wallaby/model_not_found.rb +1 -2
- data/lib/errors/wallaby/not_authenticated.rb +1 -2
- data/lib/errors/wallaby/not_found.rb +1 -2
- data/lib/errors/wallaby/not_implemented.rb +1 -2
- data/lib/errors/wallaby/resource_not_found.rb +1 -2
- data/lib/errors/wallaby/unprocessable_entity.rb +1 -2
- data/lib/fields/wallaby/all_fields.rb +63 -0
- data/lib/forms/wallaby/form_builder.rb +1 -1
- data/lib/generators/wallaby/engine/application_generator.rb +33 -0
- data/lib/generators/wallaby/engine/authorizer/USAGE +20 -0
- data/lib/generators/wallaby/engine/authorizer/authorizer_generator.rb +19 -0
- data/lib/generators/wallaby/engine/authorizer/templates/authorizer.rb.erb +35 -0
- data/lib/generators/wallaby/engine/controller/USAGE +20 -0
- data/lib/generators/wallaby/engine/controller/controller_generator.rb +23 -0
- data/lib/generators/wallaby/engine/controller/templates/controller.rb.erb +130 -0
- data/lib/generators/wallaby/engine/decorator/USAGE +20 -0
- data/lib/generators/wallaby/engine/decorator/decorator_generator.rb +19 -0
- data/lib/generators/wallaby/engine/decorator/templates/decorator.rb.erb +5 -0
- data/lib/generators/wallaby/engine/install/USAGE +19 -0
- data/lib/generators/wallaby/engine/install/install_generator.rb +91 -0
- data/lib/generators/wallaby/engine/install/templates/application_authorizer.rb.erb +37 -0
- data/lib/generators/wallaby/engine/install/templates/application_controller.rb.erb +173 -0
- data/lib/generators/wallaby/engine/install/templates/application_decorator.rb.erb +7 -0
- data/lib/generators/wallaby/engine/install/templates/application_paginator.rb.erb +27 -0
- data/lib/generators/wallaby/engine/install/templates/application_servicer.rb.erb +47 -0
- data/lib/generators/wallaby/engine/install/templates/initializer.rb.erb +16 -0
- data/lib/generators/wallaby/engine/paginator/USAGE +20 -0
- data/lib/generators/wallaby/engine/paginator/paginator_generator.rb +19 -0
- data/lib/generators/wallaby/engine/paginator/templates/paginator.rb.erb +25 -0
- data/lib/generators/wallaby/engine/servicer/USAGE +20 -0
- data/lib/generators/wallaby/engine/servicer/servicer_generator.rb +19 -0
- data/lib/generators/wallaby/engine/servicer/templates/servicer.rb.erb +45 -0
- data/lib/helpers/wallaby/application_helper.rb +10 -59
- data/lib/helpers/wallaby/base_helper.rb +11 -11
- data/lib/helpers/wallaby/configuration_helper.rb +36 -4
- data/lib/helpers/wallaby/form_helper.rb +1 -1
- data/lib/helpers/wallaby/index_helper.rb +19 -9
- data/lib/helpers/wallaby/links_helper.rb +18 -85
- data/lib/helpers/wallaby/resources_helper.rb +39 -7
- data/lib/helpers/wallaby/secure_helper.rb +20 -19
- data/lib/interfaces/wallaby/mode.rb +8 -8
- data/lib/interfaces/wallaby/model_authorization_provider.rb +23 -22
- data/lib/interfaces/wallaby/model_decorator.rb +36 -48
- data/lib/interfaces/wallaby/model_finder.rb +3 -3
- data/lib/interfaces/wallaby/model_pagination_provider.rb +2 -6
- data/lib/interfaces/wallaby/model_service_provider.rb +4 -4
- data/lib/paginators/wallaby/model_paginator.rb +1 -1
- data/lib/responders/wallaby/json_api_responder.rb +10 -5
- data/lib/responders/wallaby/resources_responder.rb +10 -3
- data/lib/routes/wallaby/engines/base_route.rb +78 -0
- data/lib/routes/wallaby/engines/custom_app_route.rb +92 -0
- data/lib/routes/wallaby/engines/engine_route.rb +77 -0
- data/lib/routes/wallaby/resources_router.rb +100 -45
- data/lib/servicers/wallaby/model_servicer.rb +13 -13
- data/lib/services/wallaby/authorizer_finder.rb +23 -0
- data/lib/services/wallaby/class_finder.rb +42 -0
- data/lib/services/wallaby/controller_finder.rb +29 -0
- data/lib/services/wallaby/decorator_finder.rb +34 -0
- data/lib/services/wallaby/default_models_excluder.rb +45 -0
- data/lib/services/wallaby/engine_name_finder.rb +14 -11
- data/lib/services/wallaby/engine_url_for.rb +82 -37
- data/lib/services/wallaby/fields_regulator.rb +34 -0
- data/lib/services/wallaby/map/mode_mapper.rb +5 -5
- data/lib/services/wallaby/map/model_class_mapper.rb +1 -1
- data/lib/services/wallaby/model_class_filter.rb +29 -0
- data/lib/services/wallaby/paginator_finder.rb +24 -0
- data/lib/services/wallaby/prefixes_builder.rb +49 -8
- data/lib/services/wallaby/servicer_finder.rb +31 -0
- data/lib/services/wallaby/sorting/hash_builder.rb +9 -0
- data/lib/services/wallaby/sorting/link_builder.rb +7 -10
- data/lib/services/wallaby/sorting/next_builder.rb +1 -12
- data/lib/services/wallaby/sorting/single_builder.rb +1 -1
- data/lib/support/action_dispatch/routing/mapper.rb +29 -4
- data/lib/utils/wallaby/field_utils.rb +9 -8
- data/lib/utils/wallaby/inflector.rb +94 -0
- data/lib/utils/wallaby/locale.rb +2 -2
- data/lib/utils/wallaby/module_utils.rb +3 -10
- data/lib/utils/wallaby/utils.rb +21 -14
- data/lib/wallaby/class_array.rb +18 -13
- data/lib/wallaby/class_hash.rb +16 -14
- data/lib/wallaby/classifier.rb +4 -2
- data/lib/wallaby/configuration/features.rb +8 -2
- data/lib/wallaby/configuration/mapping.rb +66 -112
- data/lib/wallaby/configuration/metadata.rb +15 -12
- data/lib/wallaby/configuration/models.rb +27 -25
- data/lib/wallaby/configuration/pagination.rb +15 -19
- data/lib/wallaby/configuration/security.rb +88 -80
- data/lib/wallaby/configuration/sorting.rb +15 -17
- data/lib/wallaby/configuration.rb +58 -23
- data/lib/wallaby/constants.rb +21 -13
- data/lib/wallaby/core/version.rb +1 -1
- data/lib/wallaby/core.rb +34 -10
- data/lib/wallaby/deprecator.rb +81 -0
- data/lib/wallaby/engine.rb +1 -18
- data/lib/wallaby/guesser.rb +45 -0
- data/lib/wallaby/logger.rb +35 -13
- data/lib/wallaby/map.rb +11 -88
- data/lib/wallaby/preloader.rb +8 -28
- metadata +113 -14
- data/config/locales/wallaby_class.en.yml +0 -9
- data/lib/concerns/wallaby/defaultable.rb +0 -38
- data/lib/concerns/wallaby/shared_helpers.rb +0 -22
- data/lib/services/wallaby/map/model_class_collector.rb +0 -49
- data/lib/services/wallaby/type_renderer.rb +0 -40
- data/lib/utils/wallaby/model_utils.rb +0 -52
- data/lib/utils/wallaby/test_utils.rb +0 -34
@@ -1,78 +1,67 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Wallaby
|
4
|
-
# Field helper
|
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
|
-
# @
|
8
|
-
|
9
|
-
|
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
|
-
# @
|
14
|
-
|
15
|
-
|
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
|
-
# @
|
20
|
-
|
21
|
-
|
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
|
25
|
-
# @return [
|
26
|
-
def
|
27
|
-
|
28
|
-
|
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
|
-
#
|
43
|
-
# @
|
44
|
-
|
45
|
-
|
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
|
49
|
-
# @return [String]
|
50
|
-
def
|
51
|
-
|
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
|
-
#
|
55
|
-
# @
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
#
|
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
|
-
(
|
55
|
-
|
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
|
68
|
-
def paginate(query, options)
|
69
|
-
options[:paginate]
|
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
|
28
|
+
def wallaby_prefixes
|
8
29
|
override_prefixes(
|
9
|
-
options: { mapping_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
|
-
|
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
|
-
#
|
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
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
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
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
# @param
|
49
|
-
# @
|
50
|
-
#
|
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(
|
99
|
+
def collection(params: self.params, paginate: true, **paginate_options, &block)
|
54
100
|
@collection ||=
|
55
101
|
ModuleUtils.yield_for(
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
69
|
-
#
|
70
|
-
#
|
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
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
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
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
# @
|
95
|
-
# @
|
96
|
-
|
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
|
-
|
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
|