Sha256: fcb1d8f3558a1eb659e9adc0d6d7bf19b98503f0b2b69ceea0da9d1ff8470698

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

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

        def find_target_with_second_level_cache
          return find_target_without_second_level_cache unless klass.second_level_cache_enabled?
          return find_target_without_second_level_cache if reflection.options[:through] || reflection.scope
          # TODO: implement cache with has_one through, scope
          if reflection.options[:as]
            cache_record = klass.fetch_by_uniq_keys({reflection.foreign_key => owner[reflection.active_record_primary_key], reflection.type => owner.class.base_class.name})
          else
            cache_record = klass.fetch_by_uniq_key(owner[reflection.active_record_primary_key], reflection.foreign_key)
          end
          return cache_record.tap{|record| set_inverse_instance(record)} if cache_record

          record = find_target_without_second_level_cache

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
second_level_cache-2.1.13 lib/second_level_cache/active_record/has_one_association.rb
second_level_cache-2.1.10 lib/second_level_cache/active_record/has_one_association.rb
second_level_cache-2.1.9 lib/second_level_cache/active_record/has_one_association.rb