Module: IdentityCache::BelongsToCaching::ClassMethods
- Defined in:
- lib/identity_cache/belongs_to_caching.rb
Instance Method Summary collapse
- #build_normalized_belongs_to_cache(association, options) ⇒ Object
- #cache_belongs_to(association, options = {}) ⇒ Object
Instance Method Details
#build_normalized_belongs_to_cache(association, options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/identity_cache/belongs_to_caching.rb', line 26 def build_normalized_belongs_to_cache(association, ) self.class_eval(<<-CODE, __FILE__, __LINE__ + 1) def #{[:cached_accessor_name]} if IdentityCache.should_use_cache? && #{[:foreign_key]}.present? && !association(:#{association}).loaded? self.#{association} = #{[:association_class]}.fetch_by_id(#{[:foreign_key]}) else #{association} end end def #{[:prepopulate_method_name]}(record) self.#{association} = record end CODE end |
#cache_belongs_to(association, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/identity_cache/belongs_to_caching.rb', line 11 def cache_belongs_to(association, = {}) self.cached_belongs_tos[association] = [:embed] ||= false [:cached_accessor_name] ||= "fetch_#{association}" [:foreign_key] ||= reflect_on_association(association).foreign_key [:association_class] ||= reflect_on_association(association).klass [:prepopulate_method_name] ||= "prepopulate_fetched_#{association}" if [:embed] raise NotImplementedError else build_normalized_belongs_to_cache(association, ) end end |