Module: ContentfulRails::Caching::Timestamps

Defined in:
lib/contentful_rails/caching/timestamps.rb

Overview

A module to prepend into ContentfulModel::Base which will allow the model instance to check the cache for its timestamp before making an expensive API call. Also includes a module method to remove an existing timestamp.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/contentful_rails/caching/timestamps.rb', line 7

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    alias_method_chain :updated_at, :caching
    alias_method_chain :cache_key, :preview
  end
end

Instance Method Details

#cache_key_with_previewObject



45
46
47
48
49
50
51
# File 'lib/contentful_rails/caching/timestamps.rb', line 45

def cache_key_with_preview
  if ContentfulModel.use_preview_api
    "preview/#{cache_key_without_preview}"
  else
    cache_key_without_preview
  end
end

#timestamp_cache_keyObject



41
42
43
# File 'lib/contentful_rails/caching/timestamps.rb', line 41

def timestamp_cache_key
  self.class.timestamp_cache_key(id)
end

#updated_at_with_cachingObject

A replacement method for updated_at(), called when this module is included in ContentfulModel::Base



31
32
33
34
35
36
37
38
39
# File 'lib/contentful_rails/caching/timestamps.rb', line 31

def updated_at_with_caching
  if ContentfulRails.configuration.perform_caching && !ContentfulModel.use_preview_api
    Rails.cache.fetch(self.timestamp_cache_key) do
      updated_at_without_caching
    end
  else
    updated_at_without_caching
  end
end