Sha256: 5e6ececb28c090a584bb4a5b8dfa4b16256944eab49ba8f91b2248a44e369d9c

Contents?: true

Size: 952 Bytes

Versions: 2

Compression:

Stored size: 952 Bytes

Contents

# -*- encoding : utf-8 -*-
module TinyCache
  module ActiveRecord
    module Associations
      module HasOneAssociation
        extend ::ActiveSupport::Concern
        included do
          class_eval do
            alias_method_chain :find_target, :tiny_cache
          end
        end

        def association_class
          @reflection.klass
        end

        def find_target_with_tiny_cache
          return find_target_without_tiny_cache unless association_class.tiny_cache_enabled?

          cache_record = association_class.tiny_cache_find_by(
            reflection.foreign_key => owner[reflection.active_record_primary_key]
          )

          return cache_record.tap{|record| set_inverse_instance(record)} if cache_record

          record = find_target_without_tiny_cache

          record.tap do |r|
            set_inverse_instance(r)
            r.write_tiny_cache
          end if record
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
tiny-cache-0.0.3 lib/tiny_cache/active_record/has_one_association.rb
tiny_cache-0.0.3 lib/tiny_cache/active_record/has_one_association.rb