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

Automatically preloads associations to serialized objects

Every association declared with :preload is loaded once during serialization using ActiveRecord::Associations::Preloader, so there are no N+1 queries.

Examples:

class AppSerializer < Serega
  config.auto_preload_attributes_with_delegate = true
  config.auto_preload_attributes_with_serializer = true
  config.hide_by_default = [:preload]

  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)

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Registers the ActiveRecord preload handler

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



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

def self.after_load_plugin(serializer_class, **_opts)
  serializer_class.preload_with { |objects, preloads| Preloader.preload(objects, preloads) }
end

.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



58
59
60
61
62
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 58

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
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



72
73
74
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 72

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

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



47
48
49
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 47

def self.plugin_name
  :activerecord_preloads
end