Module: IdentityCache::BelongsToCaching::ClassMethods

Defined in:
lib/identity_cache/belongs_to_caching.rb

Instance Method Summary collapse

Instance Method Details

#cache_belongs_to(association) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/identity_cache/belongs_to_caching.rb', line 13

def cache_belongs_to(association)
  ensure_base_model

  unless (reflection = reflect_on_association(association))
    raise AssociationError, "Association named '#{association}' was not found on #{self}"
  end

  if reflection.scope
    raise(
      UnsupportedAssociationError,
      "caching association #{self}.#{association} is scoped which isn't supported"
    )
  end

  cached_belongs_to = Cached::BelongsTo.new(association, reflection: reflection)

  cached_belongs_tos[association] = cached_belongs_to.tap(&:build)
end