Module: BlueprinterActiveRecord::Helpers

Extended by:
Helpers
Included in:
AddedPreloadsLogger, Helpers, MissingPreloadsLogger, PreloadInfo, Preloader
Defined in:
lib/blueprinter-activerecord/helpers.rb

Instance Method Summary collapse

Instance Method Details

#count_preloads(preloads) ⇒ Integer

Count the number of preloads in a nested Hash.

Parameters:

  • preloads (Hash)

    Nested Hash of preloads

Returns:

  • (Integer)

    The number of associations in the Hash



23
24
25
26
27
# File 'lib/blueprinter-activerecord/helpers.rb', line 23

def count_preloads(preloads)
  preloads.reduce(0) { |acc, (_key, val)|
    acc + 1 + count_preloads(val)
  }
end

#diff_preloads(before, after, diff = [], path = []) ⇒ Array<Array<Symbol>>

Finds preloads from ‘after’ that are missing in ‘before’.

Parameters:

  • before (Hash)

    The extracted preloads from before Preloader ran

  • after (Hash)

    The extracted preloads from after Preloader ran

  • diff (Array<BlueprinterActiveRecord::MissingPreload>) (defaults to: [])

    internal use

  • path (Array<Symbol>) (defaults to: [])

    internal use

Returns:

  • (Array<Array<Symbol>>)

    the preloads missing from ‘before’ . They’re in a “path” structure, with the last element of each sub-array being the missing preload, e.g. ‘[[:widget], [:project, :company]]`



38
39
40
41
42
43
44
45
# File 'lib/blueprinter-activerecord/helpers.rb', line 38

def diff_preloads(before, after, diff = [], path = [])
  after.each_with_object(diff) do |(key, after_val), obj|
    sub_path = path + [key]
    before_val = before[key]
    obj << sub_path if before_val.nil?
    diff_preloads(before_val || {}, after_val, diff, sub_path)
  end
end

#extract_preloads(q) ⇒ Hash

Combines all types of preloads (preload, includes, eager_load) into a single nested hash

Parameters:

  • q (ActiveRecord::Relation)

Returns:

  • (Hash)

    Symbol keys with Hash values of arbitrary depth



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

def extract_preloads(q)
  merge_values [*q.values[:preload], *q.values[:includes], *q.values[:eager_load]]
end

#merge_values(value, result = {}) ⇒ Hash

Merges ‘values’, which may be any nested structure of arrays, hashes, strings, and symbols into a nested hash.

Parameters:

  • value (Array|Hash|String|Symbol)
  • result (Hash) (defaults to: {})

Returns:

  • (Hash)

    Symbol keys with Hash values of arbitrary depth



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/blueprinter-activerecord/helpers.rb', line 54

def merge_values(value, result = {})
  case value
  when Array
    value.each { |val| merge_values(val, result) }
  when Hash
    value.each { |key, val|
      key = key.to_sym
      result[key] ||= {}
      merge_values(val, result[key])
    }
  when Symbol
    result[value] ||= {}
  when String
    result[value.to_sym] ||= {}
  else
    raise ArgumentError, "Unexpected value of type '#{value.class.name}' (#{value.inspect})"
  end
  result
end