Sha256: df0eb202b82d746de890bdf1f08fd78612031bcfe6f825afd4d0303ddf199e60

Contents?: true

Size: 888 Bytes

Versions: 3

Compression:

Stored size: 888 Bytes

Contents

# frozen_string_literal: true
module IdentityCache
  module BelongsToCaching
    extend ActiveSupport::Concern

    included do |base|
      base.class_attribute(:cached_belongs_tos)
      base.cached_belongs_tos = {}
    end

    module ClassMethods
      def cache_belongs_to(association)
        ensure_base_model

        unless (reflection = reflect_on_association(association))
          raise AssociationError, "Association named '#{association}' was not found on #{self.class}"
        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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
identity_cache-1.1.0 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.0.1 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.0.0 lib/identity_cache/belongs_to_caching.rb