Sha256: 9d316288fb65db45ab637aefcb9e7b1d94a482655fe1fe6598d07a1ded69faba

Contents?: true

Size: 889 Bytes

Versions: 9

Compression:

Stored size: 889 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

9 entries across 9 versions & 1 rubygems

Version Path
identity_cache-1.5.3 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.2 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.1 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.0 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.4.1 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.4.0 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.3.1 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.3.0 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.2.0 lib/identity_cache/belongs_to_caching.rb