Class: BlueprinterActiveRecord::Preloader
- Inherits:
-
Blueprinter::Extension
- Object
- Blueprinter::Extension
- BlueprinterActiveRecord::Preloader
- Includes:
- Helpers
- Defined in:
- lib/blueprinter-activerecord/preloader.rb
Overview
A Blueprinter extension to automatically preload a Blueprint view’s ActiveRecord associations during render
Constant Summary collapse
- DEFAULT_MAX_RECURSION =
10
Instance Attribute Summary collapse
-
#auto ⇒ Object
readonly
Returns the value of attribute auto.
-
#auto_proc ⇒ Object
readonly
Returns the value of attribute auto_proc.
-
#use ⇒ Object
readonly
Returns the value of attribute use.
Instance Method Summary collapse
-
#initialize(auto: false, use: :preload) {|Object, Class, Symbol, Hash| ... } ⇒ Preloader
constructor
Initialize and configure the extension.
-
#pre_render(object, blueprint, view, options) ⇒ Object
Implements the “pre_render” Blueprinter Extension to preload associations from a view.
Methods included from Helpers
#count_preloads, #diff_preloads, #extract_preloads, #merge_values
Constructor Details
#initialize(auto: false, use: :preload) {|Object, Class, Symbol, Hash| ... } ⇒ Preloader
Initialize and configure the extension.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/blueprinter-activerecord/preloader.rb', line 18 def initialize(auto: false, use: :preload, &auto_proc) @auto = auto @auto_proc = auto_proc @use = case use when :preload, :includes use else raise ArgumentError, "Unknown value '#{use.inspect}' for `BlueprinterActiveRecord::Preloader` argument 'use'. Valid values are :preload, :includes." end end |
Instance Attribute Details
#auto ⇒ Object (readonly)
Returns the value of attribute auto.
9 10 11 |
# File 'lib/blueprinter-activerecord/preloader.rb', line 9 def auto @auto end |
#auto_proc ⇒ Object (readonly)
Returns the value of attribute auto_proc.
9 10 11 |
# File 'lib/blueprinter-activerecord/preloader.rb', line 9 def auto_proc @auto_proc end |
#use ⇒ Object (readonly)
Returns the value of attribute use.
9 10 11 |
# File 'lib/blueprinter-activerecord/preloader.rb', line 9 def use @use end |
Instance Method Details
#pre_render(object, blueprint, view, options) ⇒ Object
Implements the “pre_render” Blueprinter Extension to preload associations from a view. If auto is true, all ActiveRecord::Relation and ActiveRecord::AssociationRelation objects will be preloaded. If auto is false, only queries that have called ‘.preload_blueprint` will be preloaded.
NOTE: If auto is on, *don’t* be concerned that you’ll end up with duplicate preloads. Even if the query ends up with overlapping members in ‘preload’ and ‘includes’, ActiveRecord intelligently handles them. There are several unit tests which confirm this behavior.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/blueprinter-activerecord/preloader.rb', line 40 def pre_render(object, blueprint, view, ) if object.is_a?(ActiveRecord::Relation) && !object.loaded? if object.preload_blueprint_method || auto || auto_proc&.call(object, blueprint, view, ) == true object.before_preload_blueprint = extract_preloads object blueprint_preloads = self.class.preloads(blueprint, view, model: object.model) loader = object.preload_blueprint_method || use object.public_send(loader, blueprint_preloads) else object end else object end end |