Last active
December 20, 2015 02:39
-
-
Save kellysutton/6058382 to your computer and use it in GitHub Desktop.
ActiveModel::Serializer cache_digest-esque behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonSerializer < ActiveModel::Serializer | |
def model_version | |
Digest::MD5.hexdigest(File.read(File.join(Rails.root, "app/serializers", "#{self.class.to_s.underscore}.rb")).gsub(/\s/,'')) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you're serializing data for an API, you probably want your caches to get cleared when you add/remove/change payloads. It would be nice to have functionality similar to cache_digests, such that if the template changes, so does the cached data. Including this code in your cache key gives you that.
This code obviously makes a lot of assumptions. You will need to twerk it as necessary.