Module: Preloadable::ClassMethods

Defined in:
lib/jsonapi-preloadable.rb

Instance Method Summary collapse

Instance Method Details

#preloadable(&block) ⇒ Object



42
43
44
# File 'lib/jsonapi-preloadable.rb', line 42

def preloadable(&block)
  block.call(preloadable_config)
end

#preloadable_configObject



38
39
40
# File 'lib/jsonapi-preloadable.rb', line 38

def preloadable_config
  @preloadable_config ||= PreloadableAttributeConfig.new
end

#records(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jsonapi-preloadable.rb', line 46

def records(options = {})
  attrs = preloadable_config.defaults.dup

  context_args = options.dig(:context, :preload)
  if context_args.present?
    attrs.merge(context_args.split(",").map(&:to_sym))
  end

  return super if attrs.empty?

  includable = preloadable_config.attrs.map do |key, value|
    value if attrs.include?(key)
  end.compact

  super.includes(includable)
end