Sha256: afb6b464088d4a55aa2f64df72841f8fb9e522ae077cfbf9afe192ff233e2307

Contents?: true

Size: 883 Bytes

Versions: 7

Compression:

Stored size: 883 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}"
        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

7 entries across 7 versions & 1 rubygems

Version Path
identity_cache-1.6.3 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.6.2 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.6.1 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.6.0 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.6 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.5 lib/identity_cache/belongs_to_caching.rb
identity_cache-1.5.4 lib/identity_cache/belongs_to_caching.rb