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.
Defined Under Namespace
Classes: ActiverecordArray, ActiverecordEnumerator, ActiverecordObject, ActiverecordRelation, Loader, Preloader
Class Method Summary collapse
-
.after_load_plugin(serializer_class, **_opts) ⇒ void
Registers the ActiveRecord preload handler.
-
.before_load_plugin(serializer_class, **opts) ⇒ void
Checks requirements to load plugin.
-
.load_plugin(serializer_class, **_opts) ⇒ void
Applies plugin code to specific serializer.
-
.plugin_name ⇒ Symbol
Plugin name.
-
.records(serializer_class, objects) ⇒ Array
The underlying records to preload onto.
Class Method Details
.after_load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Registers the ActiveRecord preload handler
84 85 86 87 88 |
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 84 def self.after_load_plugin(serializer_class, **_opts) serializer_class.preload_with do |objects, preloads| Preloader.preload(ActiverecordPreloads.records(serializer_class, objects), preloads) end end |
.before_load_plugin(serializer_class, **opts) ⇒ void
This method returns an undefined value.
Checks requirements to load plugin
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
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_name ⇒ Symbol
Returns Plugin name.
47 48 49 |
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 47 def self.plugin_name :activerecord_preloads end |
.records(serializer_class, objects) ⇒ Array
The underlying records to preload onto. The :presenter plugin wraps every serialized object in a SimpleDelegator, but ActiveRecord's Preloader needs the real records, so unwrap them via #getobj when presenter is used.
100 101 102 103 104 |
# File 'lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb', line 100 def self.records(serializer_class, objects) return objects unless serializer_class.plugin_used?(:presenter) objects.map(&:__getobj__) end |