From 8529ea43c99765b228af520866edf0b4718889ac Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Fri, 9 Oct 2015 01:30:11 -0500 Subject: [PATCH] Handle no serializer source file to digest. output warning Closes #1176 --- lib/active_model/serializer.rb | 23 ++++++++++++++++------- test/serializers/cache_test.rb | 12 ++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index db2a05562..27068b621 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -27,6 +27,20 @@ class Serializer ) /x + # Hashes contents of file for +_cache_digest+ + def self.digest_caller_file(caller_line) + serializer_file_path = caller_line[CALLER_FILE] + serializer_file_contents = IO.read(serializer_file_path) + Digest::MD5.hexdigest(serializer_file_contents) + rescue TypeError, Errno::ENOENT + warn <<-EOF.strip_heredoc + Cannot digest non-existent file: '#{caller_line}'. + Please set `::_cache_digest` of the serializer + if you'd like to cache it. + EOF + ''.freeze + end + with_options instance_writer: false, instance_reader: false do |serializer| class_attribute :_type, instance_reader: true class_attribute :_attributes @@ -43,9 +57,10 @@ class Serializer end def self.inherited(base) + caller_line = caller.first base._attributes = _attributes.dup base._attributes_keys = _attributes_keys.dup - base._cache_digest = digest_caller_file(caller.first) + base._cache_digest = digest_caller_file(caller_line) super end @@ -105,12 +120,6 @@ def self.serializers_cache @serializers_cache ||= ThreadSafe::Cache.new end - def self.digest_caller_file(caller_line) - serializer_file_path = caller_line[CALLER_FILE] - serializer_file_contents = IO.read(serializer_file_path) - Digest::MD5.hexdigest(serializer_file_contents) - end - # @api private def self.serializer_lookup_chain_for(klass) chain = [] diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index 374e1f61c..284c7b2ea 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -3,6 +3,8 @@ module ActiveModel class Serializer class CacheTest < Minitest::Test + include ActiveSupport::Testing::Stream + def setup ActionController::Base.cache_store.clear @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') @@ -170,6 +172,16 @@ def test_digest_caller_file file.unlink end + def test_warn_on_serializer_not_defined_in_file + called = false + serializer = Class.new(ActiveModel::Serializer) + assert_match(/_cache_digest/, (capture(:stderr) do + serializer.digest_caller_file('') + called = true + end)) + assert called + end + private def render_object_with_cache(obj)