From 7e53a3a0fc2666ef130499c19cfd3109d71bcd92 Mon Sep 17 00:00:00 2001 From: Ryan Townsend Date: Wed, 27 Apr 2016 18:02:33 +0100 Subject: [PATCH] Implements a #cache_key method on the base model with a sensible default --- lib/flex_commerce_api/api_base.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/flex_commerce_api/api_base.rb b/lib/flex_commerce_api/api_base.rb index ec27947..448a9ed 100644 --- a/lib/flex_commerce_api/api_base.rb +++ b/lib/flex_commerce_api/api_base.rb @@ -120,6 +120,14 @@ def template_attribute(key) end end + def cache_key + if respond_to?(:id) && respond_to?(:updated_at) && id && updated_at + [self.class.name, id, updated_at.to_datetime.utc.to_i.to_s].join("/") + else + raise NotImplementedError, "This model doesn't have an id/updated_at field, so requires custom #cache_key implementation" + end + end + def method_missing(method, *args) if relationships and relationships.has_attribute?(method) super