Class: BlueprinterActiveRecord::PreloadInfo

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/blueprinter-activerecord/preload_info.rb

Overview

Info about preloads from a query that was run through a Blueprinter’s render method.

Used for logging by the BlueprinterActiveRecord::MissingPreloadsLogger and BlueprinterActiveRecord::AddedPreloadsLogger extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#count_preloads, #diff_preloads, #extract_preloads, #merge_values

Constructor Details

#initialize(query, from_code, from_blueprint, trace) ⇒ PreloadInfo

Returns a new instance of PreloadInfo.

Parameters:

  • query (ActiveRecord::Relation)

    The query passed to “render”

  • from_code (Hash)

    Nested Hash of preloads, includes, and eager_loads that were present in query when passed to “render”

  • from_blueprint (Hash)

    Nested Hash of associations pulled from the Blueprint view

  • trace (Array<String>)

    Stack trace to query



24
25
26
27
28
29
# File 'lib/blueprinter-activerecord/preload_info.rb', line 24

def initialize(query, from_code, from_blueprint, trace)
  @query = query
  @from_code = from_code
  @from_blueprint = from_blueprint
  @trace = trace
end

Instance Attribute Details

#queryActiveRecord::Relation (readonly)

Returns The base query.

Returns:

  • (ActiveRecord::Relation)

    The base query



13
14
15
# File 'lib/blueprinter-activerecord/preload_info.rb', line 13

def query
  @query
end

#traceArray<String> (readonly)

Returns Stack trace to the query.

Returns:

  • (Array<String>)

    Stack trace to the query



16
17
18
# File 'lib/blueprinter-activerecord/preload_info.rb', line 16

def trace
  @trace
end

Instance Method Details

#foundArray<Array<Symbol>>

Returns Array of “preload paths” (e.g. [[:project, :company]]) to missing preloads that could have been found & added by BlueprinterActiveRecord::Preloader.

Returns:

  • (Array<Array<Symbol>>)

    Array of “preload paths” (e.g. [[:project, :company]]) to missing preloads that could have been found & added by BlueprinterActiveRecord::Preloader



43
44
45
# File 'lib/blueprinter-activerecord/preload_info.rb', line 43

def found
  @found ||= diff_preloads(@from_code, hash)
end

#hashHash

Returns Nested hash of all preloads, both manually added and auto found.

Returns:

  • (Hash)

    Nested hash of all preloads, both manually added and auto found



53
54
55
# File 'lib/blueprinter-activerecord/preload_info.rb', line 53

def hash
  @hash ||= merge_values [@from_code, @from_blueprint]
end

#num_existingInteger

Returns The number of preloads, includes, and eager_loads that existed before BlueprinterActiveRecord was involved.

Returns:

  • (Integer)

    The number of preloads, includes, and eager_loads that existed before BlueprinterActiveRecord was involved



38
39
40
# File 'lib/blueprinter-activerecord/preload_info.rb', line 38

def num_existing
  @num_existing ||= count_preloads(hash)
end

#percent_foundInteger

Returns The percent of total preloads found by BlueprinterActiveRecord.

Returns:

  • (Integer)

    The percent of total preloads found by BlueprinterActiveRecord



32
33
34
35
# File 'lib/blueprinter-activerecord/preload_info.rb', line 32

def percent_found
  total = num_existing + found.size
  ((found.size / num_existing.to_f) * 100).round
end

#visibleArray<Array<Symbol>>

Returns Array of “preload paths” (e.g. [[:project, :company]]) from the blueprint that were visible to the preloader.

Returns:

  • (Array<Array<Symbol>>)

    Array of “preload paths” (e.g. [[:project, :company]]) from the blueprint that were visible to the preloader



48
49
50
# File 'lib/blueprinter-activerecord/preload_info.rb', line 48

def visible
  @visible ||= diff_preloads({}, @from_blueprint)
end