serega 0.20.1 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +113 -226
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +31 -3
- data/lib/serega/attribute_normalizer.rb +143 -42
- data/lib/serega/batch/attribute_loader.rb +70 -0
- data/lib/serega/batch/attribute_loaders.rb +59 -0
- data/lib/serega/batch/auto_resolver.rb +24 -0
- data/lib/serega/batch/auto_resolver_factory.rb +48 -0
- data/lib/serega/batch/loader.rb +67 -0
- data/lib/serega/config.rb +59 -1
- data/lib/serega/object_serializer.rb +11 -9
- data/lib/serega/plan.rb +16 -0
- data/lib/serega/plan_point.rb +31 -5
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +42 -23
- data/lib/serega/plugins/activerecord_preloads/lib/active_record_objects.rb +31 -0
- data/lib/serega/plugins/camel_case/camel_case.rb +1 -1
- data/lib/serega/plugins/formatters/formatters.rb +79 -22
- data/lib/serega/plugins/if/if.rb +41 -22
- data/lib/serega/plugins/if/validations/check_opt_if.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_if_value.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_unless.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +27 -6
- data/lib/serega/plugins/metadata/meta_attribute.rb +9 -10
- data/lib/serega/plugins/metadata/validations/check_block.rb +2 -4
- data/lib/serega/plugins/metadata/validations/check_opt_value.rb +2 -3
- data/lib/serega/plugins/presenter/presenter.rb +1 -1
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +43 -30
- data/lib/serega/utils/format_user_preloads.rb +58 -0
- data/lib/serega/utils/method_signature.rb +89 -0
- data/lib/serega/utils/preload_paths.rb +53 -0
- data/lib/serega/utils/preloads_constructor.rb +77 -0
- data/lib/serega/validations/attribute/check_block.rb +38 -6
- data/lib/serega/validations/attribute/check_opt_batch.rb +101 -0
- data/lib/serega/validations/attribute/check_opt_const.rb +1 -0
- data/lib/serega/validations/attribute/check_opt_delegate.rb +1 -0
- data/lib/serega/validations/attribute/check_opt_method.rb +1 -0
- data/lib/serega/{plugins/preloads/validations → validations/attribute}/check_opt_preload.rb +2 -2
- data/lib/serega/{plugins/preloads/validations → validations/attribute}/check_opt_preload_path.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_value.rb +35 -9
- data/lib/serega/validations/check_attribute_params.rb +3 -2
- data/lib/serega/validations/check_batch_loader_params.rb +80 -0
- data/lib/serega/validations/initiate/check_modifiers.rb +1 -1
- data/lib/serega.rb +110 -11
- metadata +17 -37
- data/lib/serega/plugins/batch/batch.rb +0 -168
- data/lib/serega/plugins/batch/lib/batch_config.rb +0 -77
- data/lib/serega/plugins/batch/lib/loader.rb +0 -113
- data/lib/serega/plugins/batch/lib/loaders.rb +0 -45
- data/lib/serega/plugins/batch/lib/modules/attribute.rb +0 -26
- data/lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb +0 -78
- data/lib/serega/plugins/batch/lib/modules/check_attribute_params.rb +0 -22
- data/lib/serega/plugins/batch/lib/modules/config.rb +0 -23
- data/lib/serega/plugins/batch/lib/modules/object_serializer.rb +0 -46
- data/lib/serega/plugins/batch/lib/modules/plan_point.rb +0 -22
- data/lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb +0 -43
- data/lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb +0 -54
- data/lib/serega/plugins/batch/lib/plugins_extensions/if.rb +0 -31
- data/lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb +0 -34
- data/lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb +0 -43
- data/lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb +0 -59
- data/lib/serega/plugins/batch/lib/validations/check_opt_batch.rb +0 -62
- data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +0 -60
- data/lib/serega/plugins/preloads/lib/modules/attribute.rb +0 -28
- data/lib/serega/plugins/preloads/lib/modules/attribute_normalizer.rb +0 -99
- data/lib/serega/plugins/preloads/lib/modules/check_attribute_params.rb +0 -22
- data/lib/serega/plugins/preloads/lib/modules/config.rb +0 -19
- data/lib/serega/plugins/preloads/lib/modules/plan_point.rb +0 -41
- data/lib/serega/plugins/preloads/lib/preload_paths.rb +0 -53
- data/lib/serega/plugins/preloads/lib/preloads_config.rb +0 -62
- data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +0 -79
- data/lib/serega/plugins/preloads/preloads.rb +0 -162
- data/lib/serega/utils/params_count.rb +0 -50
- data/lib/serega/validations/utils/check_extra_keyword_arg.rb +0 -33
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
-
#
|
9
|
-
module PluginsExtensions
|
10
|
-
#
|
11
|
-
# Extension that is used when batch plugin used with :formatters plugin
|
12
|
-
#
|
13
|
-
module Formatters
|
14
|
-
#
|
15
|
-
# BatchLoader additional/patched instance methods
|
16
|
-
#
|
17
|
-
# @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
|
18
|
-
#
|
19
|
-
module BatchLoaderInstanceMethods
|
20
|
-
private
|
21
|
-
|
22
|
-
# Format values after they are prepared
|
23
|
-
def keys_values
|
24
|
-
data = super
|
25
|
-
|
26
|
-
formatter = point.attribute.formatter
|
27
|
-
data.transform_values! { |value| formatter.call(value) } if formatter
|
28
|
-
|
29
|
-
data
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
#
|
34
|
-
# SeregaAttribute additional/patched instance methods
|
35
|
-
#
|
36
|
-
# @see Serega::SeregaAttribute
|
37
|
-
#
|
38
|
-
module SeregaAttributeInstanceMethods
|
39
|
-
# Normalized formatter block or callable instance
|
40
|
-
# @return [#call, nil] Normalized formatter block or callable instance
|
41
|
-
attr_reader :formatter
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def set_normalized_vars(normalizer)
|
46
|
-
super
|
47
|
-
@formatter = normalizer.formatter
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
-
#
|
9
|
-
module PluginsExtensions
|
10
|
-
#
|
11
|
-
# Extension that is used when :if plugin is loaded
|
12
|
-
#
|
13
|
-
module If
|
14
|
-
#
|
15
|
-
# SeregaObjectSerializer additional/patched class methods
|
16
|
-
#
|
17
|
-
# @see Serega::SeregaObjectSerializer
|
18
|
-
#
|
19
|
-
module ObjectSerializerInstanceMethods123
|
20
|
-
private
|
21
|
-
|
22
|
-
# Removes key added by `batch` plugin at the start of serialization to preserve attributes ordering
|
23
|
-
def attach_final_value(value, point, container)
|
24
|
-
container.delete(point.name) if super == SeregaPlugins::If::KEY_SKIPPED
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
-
#
|
9
|
-
module PluginsExtensions
|
10
|
-
#
|
11
|
-
# Extension that is used when :preloads plugin is loaded
|
12
|
-
#
|
13
|
-
module Preloads
|
14
|
-
#
|
15
|
-
# Attribute additional/patched instance methods
|
16
|
-
#
|
17
|
-
# @see Serega::SeregaPlugins::Preloads::AttributeNormalizerInstanceMethods
|
18
|
-
#
|
19
|
-
module AttributeNormalizerInstanceMethods
|
20
|
-
private
|
21
|
-
|
22
|
-
# Do not add any preloads automatically when batch option provided
|
23
|
-
def prepare_preloads
|
24
|
-
opts = init_opts
|
25
|
-
return if opts.key?(:batch) && !opts.key?(:preload)
|
26
|
-
|
27
|
-
super
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Validator for option :id_method in attribute :batch option
|
8
|
-
#
|
9
|
-
class CheckBatchOptIdMethod
|
10
|
-
class << self
|
11
|
-
#
|
12
|
-
# Checks option :id_method of attribute :batch option
|
13
|
-
#
|
14
|
-
# @param id [nil, #call] Attribute :batch option :id_method
|
15
|
-
#
|
16
|
-
# @raise [SeregaError] validation error
|
17
|
-
#
|
18
|
-
# @return [void]
|
19
|
-
#
|
20
|
-
def call(id)
|
21
|
-
return if id.is_a?(Symbol)
|
22
|
-
|
23
|
-
raise SeregaError, must_be_callable unless id.respond_to?(:call)
|
24
|
-
|
25
|
-
SeregaValidations::Utils::CheckExtraKeywordArg.call(id, "batch option :id_method")
|
26
|
-
params_count = SeregaUtils::ParamsCount.call(id, max_count: 2)
|
27
|
-
raise SeregaError, params_count_error if params_count > 2
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def params_count_error
|
33
|
-
"Invalid :batch option :id_method. It can accept maximum 2 parameters (object, context)"
|
34
|
-
end
|
35
|
-
|
36
|
-
def must_be_callable
|
37
|
-
"Invalid :batch option :id_method. It must be a Symbol, a Proc or respond to #call"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Validator for option :loader in attribute :batch option
|
8
|
-
#
|
9
|
-
class CheckBatchOptLoader
|
10
|
-
class << self
|
11
|
-
#
|
12
|
-
# Checks option :loader of attribute :batch option
|
13
|
-
#
|
14
|
-
# @param loader [nil, #call] Attribute :batch option :loader
|
15
|
-
#
|
16
|
-
# @raise [SeregaError] validation error
|
17
|
-
#
|
18
|
-
# @return [void]
|
19
|
-
#
|
20
|
-
def call(loader, serializer_class)
|
21
|
-
if loader.is_a?(Symbol)
|
22
|
-
check_symbol(loader, serializer_class)
|
23
|
-
else
|
24
|
-
check_callable(loader)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def check_symbol(loader_name, serializer_class)
|
31
|
-
defined_loaders = serializer_class.config.batch.loaders
|
32
|
-
return if defined_loaders[loader_name]
|
33
|
-
|
34
|
-
raise SeregaError, <<~ERR.strip
|
35
|
-
Please define loader before adding it to attribute.
|
36
|
-
Example: `config.batch.define(:#{loader_name}) { |ids| ... }`
|
37
|
-
ERR
|
38
|
-
end
|
39
|
-
|
40
|
-
def check_callable(loader)
|
41
|
-
raise SeregaError, must_be_callable unless loader.respond_to?(:call)
|
42
|
-
|
43
|
-
SeregaValidations::Utils::CheckExtraKeywordArg.call(loader, ":batch option :loader")
|
44
|
-
params_count = SeregaUtils::ParamsCount.call(loader, max_count: 3)
|
45
|
-
raise SeregaError, params_count_error if params_count > 3
|
46
|
-
end
|
47
|
-
|
48
|
-
def params_count_error
|
49
|
-
"Invalid :batch option :loader. It can accept maximum 3 parameters (ids, context, plan)"
|
50
|
-
end
|
51
|
-
|
52
|
-
def must_be_callable
|
53
|
-
"Invalid :batch option :loader. It must be a Symbol, a Proc or respond to :call"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
#
|
7
|
-
# Attribute `:batch` option validator
|
8
|
-
#
|
9
|
-
class CheckOptBatch
|
10
|
-
class << self
|
11
|
-
#
|
12
|
-
# Checks attribute :batch option
|
13
|
-
#
|
14
|
-
# @param opts [Hash] Attribute options
|
15
|
-
#
|
16
|
-
# @raise [SeregaError] Attribute validation error
|
17
|
-
#
|
18
|
-
# @return [void]
|
19
|
-
#
|
20
|
-
def call(opts, block, serializer_class)
|
21
|
-
return unless opts.key?(:batch)
|
22
|
-
|
23
|
-
SeregaValidations::Utils::CheckOptIsHash.call(opts, :batch)
|
24
|
-
|
25
|
-
batch = opts[:batch]
|
26
|
-
SeregaValidations::Utils::CheckAllowedKeys.call(batch, %i[id_method loader], :batch)
|
27
|
-
|
28
|
-
check_batch_opt_id_method(batch, serializer_class)
|
29
|
-
check_batch_opt_loader(batch, serializer_class)
|
30
|
-
check_usage_with_other_params(opts, block)
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def check_batch_opt_id_method(batch, serializer_class)
|
36
|
-
return if !batch.key?(:id_method) && serializer_class.config.batch.id_method
|
37
|
-
|
38
|
-
id_method = batch[:id_method]
|
39
|
-
raise SeregaError, "Option :id_method must present inside :batch option" unless id_method
|
40
|
-
|
41
|
-
CheckBatchOptIdMethod.call(id_method)
|
42
|
-
end
|
43
|
-
|
44
|
-
def check_batch_opt_loader(batch, serializer_class)
|
45
|
-
loader = batch[:loader]
|
46
|
-
raise SeregaError, "Option :loader must present inside :batch option" unless loader
|
47
|
-
|
48
|
-
CheckBatchOptLoader.call(loader, serializer_class)
|
49
|
-
end
|
50
|
-
|
51
|
-
def check_usage_with_other_params(opts, block)
|
52
|
-
raise SeregaError, "Option :batch can not be used together with option :method" if opts.key?(:method)
|
53
|
-
raise SeregaError, "Option :batch can not be used together with option :value" if opts.key?(:value)
|
54
|
-
raise SeregaError, "Option :batch can not be used together with option :const" if opts.key?(:const)
|
55
|
-
raise SeregaError, "Option :batch can not be used together with option :delegate" if opts.key?(:delegate)
|
56
|
-
raise SeregaError, "Option :batch can not be used together with block" if block
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Utility that helps to transform user provided preloads to hash
|
8
|
-
#
|
9
|
-
class FormatUserPreloads
|
10
|
-
class << self
|
11
|
-
#
|
12
|
-
# Transforms user provided preloads to hash
|
13
|
-
#
|
14
|
-
# @param value [Array,Hash,String,Symbol,nil,false] preloads
|
15
|
-
#
|
16
|
-
# @return [Hash] preloads transformed to hash
|
17
|
-
#
|
18
|
-
def call(value)
|
19
|
-
case value
|
20
|
-
when Array then array_to_hash(value)
|
21
|
-
when FalseClass then nil_to_hash(value)
|
22
|
-
when Hash then hash_to_hash(value)
|
23
|
-
when NilClass then nil_to_hash(value)
|
24
|
-
when String then string_to_hash(value)
|
25
|
-
when Symbol then symbol_to_hash(value)
|
26
|
-
else raise Serega::SeregaError,
|
27
|
-
"Preload option value can consist from Symbols, Arrays, Hashes (#{value.class} #{value.inspect} was provided)"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def array_to_hash(values)
|
34
|
-
values.each_with_object({}) do |value, obj|
|
35
|
-
obj.merge!(call(value))
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def hash_to_hash(values)
|
40
|
-
values.each_with_object({}) do |(key, value), obj|
|
41
|
-
obj[key.to_sym] = call(value)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def nil_to_hash(_value)
|
46
|
-
{}
|
47
|
-
end
|
48
|
-
|
49
|
-
def string_to_hash(value)
|
50
|
-
{value.to_sym => {}}
|
51
|
-
end
|
52
|
-
|
53
|
-
def symbol_to_hash(value)
|
54
|
-
{value => {}}
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Serega::SeregaAttribute additional/patched instance methods
|
8
|
-
#
|
9
|
-
# @see Serega::SeregaAttribute::AttributeInstanceMethods
|
10
|
-
#
|
11
|
-
module AttributeInstanceMethods
|
12
|
-
# @return [Hash, nil] normalized preloads of current attribute
|
13
|
-
attr_reader :preloads
|
14
|
-
|
15
|
-
# @return [Array] normalized preloads_path of current attribute
|
16
|
-
attr_reader :preloads_path
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def set_normalized_vars(normalizer)
|
21
|
-
super
|
22
|
-
@preloads = normalizer.preloads
|
23
|
-
@preloads_path = normalizer.preloads_path
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Serega::SeregaAttributeNormalizer additional/patched instance methods
|
8
|
-
#
|
9
|
-
# @see SeregaAttributeNormalizer::AttributeNormalizerInstanceMethods
|
10
|
-
#
|
11
|
-
module AttributeNormalizerInstanceMethods
|
12
|
-
# @return [Hash,nil] normalized attribute preloads
|
13
|
-
def preloads
|
14
|
-
return @preloads if instance_variable_defined?(:@preloads)
|
15
|
-
|
16
|
-
@preloads = prepare_preloads
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [Array, nil] normalized attribute preloads path
|
20
|
-
def preloads_path
|
21
|
-
return @preloads_path if instance_variable_defined?(:@preloads_path)
|
22
|
-
|
23
|
-
@preloads_path = prepare_preloads_path
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
#
|
29
|
-
# Patched in:
|
30
|
-
# - plugin :batch (extension :preloads - skips auto preloads when batch option provided)
|
31
|
-
#
|
32
|
-
def prepare_preloads
|
33
|
-
opts = init_opts
|
34
|
-
preloads_provided = opts.key?(:preload)
|
35
|
-
preloads =
|
36
|
-
if preloads_provided
|
37
|
-
opts[:preload]
|
38
|
-
elsif opts.key?(:serializer) && self.class.serializer_class.config.preloads.auto_preload_attributes_with_serializer
|
39
|
-
method_name
|
40
|
-
elsif opts.key?(:delegate) && self.class.serializer_class.config.preloads.auto_preload_attributes_with_delegate
|
41
|
-
opts[:delegate].fetch(:to)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Nil and empty hash differs as we can preload nested results to
|
45
|
-
# empty hash, but we will skip nested preloading if nil or false provided
|
46
|
-
return if preloads_provided && !preloads
|
47
|
-
|
48
|
-
FormatUserPreloads.call(preloads)
|
49
|
-
end
|
50
|
-
|
51
|
-
def prepare_preloads_path
|
52
|
-
path = init_opts.fetch(:preload_path) { default_preload_path(preloads) }
|
53
|
-
|
54
|
-
if path && path[0].is_a?(Array)
|
55
|
-
prepare_many_preload_paths(path)
|
56
|
-
else
|
57
|
-
prepare_one_preload_path(path)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def prepare_one_preload_path(path)
|
62
|
-
return unless path
|
63
|
-
|
64
|
-
case path
|
65
|
-
when Array
|
66
|
-
path.map(&:to_sym).freeze
|
67
|
-
else
|
68
|
-
[path.to_sym].freeze
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def prepare_many_preload_paths(paths)
|
73
|
-
paths.map { |path| prepare_one_preload_path(path) }.freeze
|
74
|
-
end
|
75
|
-
|
76
|
-
def default_preload_path(preloads)
|
77
|
-
return FROZEN_EMPTY_ARRAY if !preloads || preloads.empty?
|
78
|
-
|
79
|
-
[preloads.keys.first]
|
80
|
-
end
|
81
|
-
|
82
|
-
#
|
83
|
-
# Patch for original `prepare_hide` method
|
84
|
-
# @see
|
85
|
-
#
|
86
|
-
# Marks attribute hidden if auto_hide_attribute_with_preloads option was set and attribute has preloads
|
87
|
-
#
|
88
|
-
def prepare_hide
|
89
|
-
res = super
|
90
|
-
return res unless res.nil?
|
91
|
-
|
92
|
-
if preloads && !preloads.empty?
|
93
|
-
self.class.serializer_class.config.preloads.auto_hide_attributes_with_preload || nil
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
|
8
|
-
#
|
9
|
-
# @see Serega::SeregaValidations::CheckAttributeParams
|
10
|
-
#
|
11
|
-
module CheckAttributeParamsInstanceMethods
|
12
|
-
private
|
13
|
-
|
14
|
-
def check_opts
|
15
|
-
super
|
16
|
-
CheckOptPreload.call(opts)
|
17
|
-
CheckOptPreloadPath.call(opts)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Config class additional/patched instance methods
|
8
|
-
#
|
9
|
-
# @see Serega::SeregaConfig
|
10
|
-
#
|
11
|
-
module ConfigInstanceMethods
|
12
|
-
# @return [Serega::SeregaPlugins::Preloads::PreloadsConfig] `preloads` plugin config
|
13
|
-
def preloads
|
14
|
-
@preloads ||= PreloadsConfig.new(opts.fetch(:preloads))
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Serega::SeregaPlanPoint additional/patched instance methods
|
8
|
-
#
|
9
|
-
# @see Serega::SeregaPlanPoint::InstanceMethods
|
10
|
-
#
|
11
|
-
module PlanPointInstanceMethods
|
12
|
-
#
|
13
|
-
# @return [Hash] preloads for nested attributes
|
14
|
-
#
|
15
|
-
attr_reader :preloads
|
16
|
-
|
17
|
-
#
|
18
|
-
# @return [Array<Symbol>] preloads path for current attribute
|
19
|
-
#
|
20
|
-
attr_reader :preloads_path
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def set_normalized_vars
|
25
|
-
super
|
26
|
-
|
27
|
-
@preloads = prepare_preloads
|
28
|
-
@preloads_path = prepare_preloads_path
|
29
|
-
end
|
30
|
-
|
31
|
-
def prepare_preloads
|
32
|
-
PreloadsConstructor.call(child_plan)
|
33
|
-
end
|
34
|
-
|
35
|
-
def prepare_preloads_path
|
36
|
-
attribute.preloads_path
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Utility that helps to transform preloads to array of paths
|
8
|
-
#
|
9
|
-
# Example:
|
10
|
-
#
|
11
|
-
# call({ a: { b: { c: {}, d: {} } }, e: {} })
|
12
|
-
#
|
13
|
-
# => [
|
14
|
-
# [:a],
|
15
|
-
# [:a, :b],
|
16
|
-
# [:a, :b, :c],
|
17
|
-
# [:a, :b, :d],
|
18
|
-
# [:e]
|
19
|
-
# ]
|
20
|
-
class PreloadPaths
|
21
|
-
class << self
|
22
|
-
#
|
23
|
-
# Transforms user provided preloads to array of paths
|
24
|
-
#
|
25
|
-
# @param preloads [Array,Hash,String,Symbol,nil,false] association(s) to preload
|
26
|
-
#
|
27
|
-
# @return [Hash] preloads transformed to hash
|
28
|
-
#
|
29
|
-
def call(preloads)
|
30
|
-
formatted_preloads = FormatUserPreloads.call(preloads)
|
31
|
-
return FROZEN_EMPTY_ARRAY if formatted_preloads.empty?
|
32
|
-
|
33
|
-
paths(formatted_preloads, [], [])
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def paths(formatted_preloads, path, result)
|
39
|
-
formatted_preloads.each do |key, nested_preloads|
|
40
|
-
path << key
|
41
|
-
result << path.dup
|
42
|
-
|
43
|
-
paths(nested_preloads, path, result)
|
44
|
-
path.pop
|
45
|
-
end
|
46
|
-
|
47
|
-
result
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Preloads
|
6
|
-
#
|
7
|
-
# Config for `preloads` plugin
|
8
|
-
#
|
9
|
-
class PreloadsConfig
|
10
|
-
# @return [Hash] preloads plugin options
|
11
|
-
attr_reader :opts
|
12
|
-
|
13
|
-
#
|
14
|
-
# Initializes PreloadsConfig object
|
15
|
-
#
|
16
|
-
# @param opts [Hash] options
|
17
|
-
#
|
18
|
-
# @return [Serega::SeregaPlugins::Preloads::PreloadsConfig]
|
19
|
-
#
|
20
|
-
def initialize(opts)
|
21
|
-
@opts = opts
|
22
|
-
end
|
23
|
-
|
24
|
-
# @!method auto_preload_attributes_with_delegate
|
25
|
-
# @return [Boolean, nil] option value
|
26
|
-
#
|
27
|
-
# @!method auto_preload_attributes_with_delegate=(value)
|
28
|
-
# @param value [Boolean] New option value
|
29
|
-
# @return [Boolean] New option value
|
30
|
-
#
|
31
|
-
# @!method auto_preload_attributes_with_serializer
|
32
|
-
# @return [Boolean, nil] option value
|
33
|
-
#
|
34
|
-
# @!method auto_preload_attributes_with_serializer=(value)
|
35
|
-
# @param value [Boolean] New option value
|
36
|
-
# @return [Boolean] New option value
|
37
|
-
#
|
38
|
-
# @!method auto_hide_attributes_with_preload
|
39
|
-
# @return [Boolean, nil] option value
|
40
|
-
#
|
41
|
-
# @!method auto_hide_attributes_with_preload=(value)
|
42
|
-
# @param value [Boolean] New option value
|
43
|
-
# @return [Boolean] New option value
|
44
|
-
#
|
45
|
-
%i[
|
46
|
-
auto_preload_attributes_with_delegate
|
47
|
-
auto_preload_attributes_with_serializer
|
48
|
-
auto_hide_attributes_with_preload
|
49
|
-
].each do |method_name|
|
50
|
-
define_method(method_name) do
|
51
|
-
opts.fetch(method_name)
|
52
|
-
end
|
53
|
-
|
54
|
-
define_method(:"#{method_name}=") do |value|
|
55
|
-
raise SeregaError, "Must have boolean value, #{value.inspect} provided" if (value != true) && (value != false)
|
56
|
-
opts[method_name] = value
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|