Module: Serega::SeregaPlugins::ActiverecordPreloads

Defined in:
lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb,
lib/serega/plugins/activerecord_preloads/lib/preloader.rb

Overview

Plugin :activerecord_preloads (depends on :preloads plugin, that must be loaded first)

Automatically preloads associations to serialized objects

It takes all defined preloads from serialized attributes (including attributes from serialized relations), merges them into single associations hash and then uses ActiveRecord::Associations::Preloader to preload all associations.

Examples:

class AppSerializer < Serega
  plugin :preloads,
    auto_preload_attributes_with_delegate: true,
    auto_preload_attributes_with_serializer: true,
    auto_hide_attributes_with_preload: true

  plugin :activerecord_preloads
end

class UserSerializer < AppSerializer
  # no preloads
  attribute :username

  # preloads `:user_stats` as auto_preload_attributes_with_delegate option is true
  attribute :comments_count, delegate: { to: :user_stats }

  # preloads `:albums` as auto_preload_attributes_with_serializer option is true
  attribute :albums, serializer: AlbumSerializer, hide: false
end

class AlbumSerializer < AppSerializer
  # no preloads
  attribute :title

  # preloads :downloads_count as manually specified
  attribute :downloads_count, preload: :downloads, value: proc { |album| album.downloads.count }
end

UserSerializer.to_h(user) # => preloads {users_stats: {}, albums: { downloads: {} }}

Defined Under Namespace

Modules: InstanceMethods Classes: ActiverecordArray, ActiverecordEnumerator, ActiverecordObject, ActiverecordRelation, Loader, Preloader

Class Method Summary collapse

Class Method Details

.before_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Checks requirements to load plugin

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    plugin options



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 61

def self.before_load_plugin(serializer_class, **opts)
  opts.each_key do |key|
    raise SeregaError, "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. No options are allowed"
  end

  unless serializer_class.plugin_used?(:preloads)
    raise SeregaError, "Plugin #{plugin_name.inspect} must be loaded after the :preloads plugin. Please load the :preloads plugin first"
  end

  if serializer_class.plugin_used?(:batch)
    raise SeregaError, "Plugin #{plugin_name.inspect} must be loaded before the :batch plugin"
  end
end

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



83
84
85
86
87
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 83

def self.load_plugin(serializer_class, **_opts)
  require_relative "lib/preloader"

  serializer_class.include(InstanceMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



50
51
52
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 50

def self.plugin_name
  :activerecord_preloads
end