Module: Serega::SeregaPlugins::Preloads

Defined in:
lib/serega/plugins/preloads/preloads.rb,
lib/serega/plugins/preloads/lib/enum_deep_freeze.rb,
lib/serega/plugins/preloads/lib/main_preload_path.rb,
lib/serega/plugins/preloads/lib/format_user_preloads.rb,
lib/serega/plugins/preloads/lib/preloads_constructor.rb,
lib/serega/plugins/preloads/validations/check_opt_preload.rb,
lib/serega/plugins/preloads/validations/check_opt_preload_path.rb

Overview

Plugin :preloads

Allows to define :preloads to attributes and then allows to merge preloads from serialized attributes and return single associations hash.

Plugin accepts options:

  • auto_preload_attributes_with_delegate - default false
  • auto_preload_attributes_with_serializer - default false
  • auto_hide_attributes_with_preload - default false

This options are very handy if you want to forget about finding preloads manually.

Preloads can be disabled with preload: false attribute option option. Also automatically added preloads can be overwritten with manually specified preload: :another_value.

Some examples, please read comments in the code below

Examples:

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

class UserSerializer < AppSerializer
  # No preloads
  attribute :username

  # Specify `preload: :user_stats` manually
  attribute :followers_count, preload: :user_stats, value: proc { |user| user.user_stats.followers_count }

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

  # Automatically `preloads: :albums` as `auto_preload_attributes_with_serializer` option is true
  attribute :albums, serializer: 'AlbumSerializer'
end

class AlbumSerializer < AppSerializer
  attribute :images_count, delegate: { to: :album_stats }
end

# By default preloads are empty, as we specify `auto_hide_attributes_with_preload = true`,
# and attributes with preloads will be not serialized
UserSerializer.new.preloads # => {}
UserSerializer.new.to_h(OpenStruct.new(username: 'foo')) # => {:username=>"foo"}

UserSerializer.new(with: :followers_count).preloads # => {:user_stats=>{}}
UserSerializer.new(with: %i[followers_count comments_count]).preloads # => {:user_stats=>{}}
UserSerializer.new(with: [:followers_count, :comments_count, { albums: :images_count }]).preloads # => {:user_stats=>{}, :albums=>{:album_stats=>{}}}

Defined Under Namespace

Modules: AttributeInstanceMethods, CheckAttributeParamsInstanceMethods, ConfigInstanceMethods, InstanceMethods, MapPointInstanceMethods Classes: CheckOptPreload, CheckOptPreloadPath, EnumDeepFreeze, FormatUserPreloads, MainPreloadPath, PreloadsConfig, PreloadsConstructor

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Adds config options and runs other callbacks after plugin was loaded

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    loaded plugins opts



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/serega/plugins/preloads/preloads.rb', line 105

def self.after_load_plugin(serializer_class, **opts)
  config = serializer_class.config
  config.attribute_keys << :preload << :preload_path

  preloads_opts = DEFAULT_CONFIG.merge(opts.slice(*DEFAULT_CONFIG.keys))
  config.opts[:preloads] = {}
  preloads_config = config.preloads
  preloads_config.auto_preload_attributes_with_delegate = preloads_opts[:auto_preload_attributes_with_delegate]
  preloads_config.auto_preload_attributes_with_serializer = preloads_opts[:auto_preload_attributes_with_serializer]
  preloads_config.auto_hide_attributes_with_preload = preloads_opts[:auto_hide_attributes_with_preload]
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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/serega/plugins/preloads/preloads.rb', line 81

def self.load_plugin(serializer_class, **_opts)
  serializer_class.include(InstanceMethods)
  serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)
  serializer_class::SeregaMapPoint.include(MapPointInstanceMethods)

  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)

  require_relative "./lib/enum_deep_freeze"
  require_relative "./lib/format_user_preloads"
  require_relative "./lib/main_preload_path"
  require_relative "./lib/preloads_constructor"
  require_relative "./validations/check_opt_preload"
  require_relative "./validations/check_opt_preload_path"
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



69
70
71
# File 'lib/serega/plugins/preloads/preloads.rb', line 69

def self.plugin_name
  :preloads
end