Module: Serega::SeregaPlugins::ActiverecordPreloads

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

Overview

Plugin :activerecord_preloads

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
  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) # => preloads {users_stats: {}, albums: { downloads: {} }}

Defined Under Namespace

Modules: BatchAttributeLoaderInstanceMethods, InstanceMethods Classes: ActiveRecordObjects, 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



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

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



73
74
75
76
77
78
79
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 73

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

  serializer_class.include(InstanceMethods)
  serializer_class::SeregaBatchAttributeLoader.include(BatchAttributeLoaderInstanceMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



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

def self.plugin_name
  :activerecord_preloads
end