Sha256: 86af02e71c97a00ce9b895de1f1ce49c988066bfe9acf12ed5d6a3f996706a37

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Batch
      # Extensions (mini-plugins) that are enabled when Batch plugin used with other plugins
      module PluginsExtensions
        #
        # Extension that is used when batch plugin used with :active_record_preloads plugin
        #
        module ActiveRecordPreloads
          #
          # BatchLoader additional/patched instance methods
          #
          # @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
          #
          module BatchLoaderInstanceMethods
            private

            # Preloads required associations to batch-loaded records
            def keys_values(*)
              data = super

              if point.has_nested_points?
                associations = point.preloads
                return data if associations.empty?

                ActiverecordPreloads::Preloader.preload(data.values.flatten(1), associations)
              end

              data
            end
          end
        end

        #
        # Extension that is used when batch plugin used with :formatters plugin
        #
        module Formatters
          #
          # BatchLoader additional/patched instance methods
          #
          # @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
          #
          module BatchLoaderInstanceMethods
            private

            # Format values after they are prepared
            def keys_values(*)
              data = super

              formatter = point.attribute.formatter
              data.transform_values! { |value| formatter.call(value) } if formatter

              data
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
serega-0.8.1 lib/serega/plugins/batch/lib/plugins_extensions.rb
serega-0.8.0 lib/serega/plugins/batch/lib/plugins_extensions.rb
serega-0.7.0 lib/serega/plugins/batch/lib/plugins_extensions.rb