Sha256: c5e89d7a5286a67420734d91f0b719960a0cce3ff8fa916cdc47eab6a5fc8407

Contents?: true

Size: 838 Bytes

Versions: 1

Compression:

Stored size: 838 Bytes

Contents

module RailsCacheIt
  module RelationExtension
    extend ActiveSupport::Concern

    included do
      alias_method_chain :to_a, :cache_it
    end

    def to_a_with_cache_it
      options = @_cache_it_options
      if options
        cache_name = to_sql
        blk =  options.delete :blk
        blk_val = blk && blk.call
        result, pre_val = Rails.cache.fetch(cache_name, options)
        if result.nil? || (blk_val != pre_val)
          result, _ = Rails.cache.fetch cache_name, options.merge(force: true) do
            [self.to_a_without_cache_it, blk_val]
          end
        end
        result
      else
        self.to_a_without_cache_it
      end
    end

    def cache_it options = RailsCacheIt.default_options.dup, &blk
      options[:blk] = blk if blk
      @_cache_it_options = options
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_cache_it-0.0.2 lib/rails_cache_it/relation_extension.rb