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)

    plugins options



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

def self.before_load_plugin(serializer_class, **_opts)
  unless serializer_class.plugin_used?(:preloads)
    raise SeregaError, "Please load `plugin :preloads` first"
  end

  if serializer_class.plugin_used?(:batch)
    raise SeregaError, "Plugin `activerecord_preloads` must be loaded before `batch`"
  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)

    Loaded plugins options



79
80
81
82
83
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 79

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