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


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

def cache_belongs_to(association)
  ensure_base_model

  unless association_reflection = reflect_on_association(association)
    raise AssociationError, "Association named '#{association}' was not found on #{self.class}"
  end
  if association_reflection.scope
    raise UnsupportedAssociationError, "caching association #{self}.#{association} is scoped which isn't supported"
  end

  options = {}
  self.cached_belongs_tos[association] = options

  options[:embed]                   = false
  options[:cached_accessor_name]    = "fetch_#{association}"
  options[:records_variable_name]   = :"@cached_#{association}"
  options[:association_reflection]  = association_reflection
  options[:prepopulate_method_name] = "prepopulate_fetched_#{association}"

  build_normalized_belongs_to_cache(association, options)
end