Skip to content

Commit

Permalink
Handle no serializer source file to digest.
Browse files Browse the repository at this point in the history
output warning

Closes rails-api#1176
  • Loading branch information
bf4 committed Oct 9, 2015
1 parent f2c0c6d commit 96bc87d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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

class << self
attr_accessor :_attributes # @api private : names of attribute methods, @see Serializer#attribute
attr_accessor :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
Expand All @@ -54,9 +68,10 @@ class << self
# Serializers inherit _attributes and _attributes_keys.
# Generates a unique digest for each serializer at load.
def self.inherited(base)
caller_line = caller.first
base._attributes = _attributes.try(:dup) || []
base._attributes_keys = _attributes_keys.try(:dup) || {}
base._cache_digest = digest_caller_file(caller.first)
base._cache_digest = digest_caller_file(caller_line)
super
end

Expand Down Expand Up @@ -163,13 +178,6 @@ def self.serializers_cache
@serializers_cache ||= ThreadSafe::Cache.new
end

# 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)
end

# Find a serializer from a class and caches the lookup.
# Preferentially retuns:
# 1. class name appended with "Serializer"
Expand Down
12 changes: 12 additions & 0 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 96bc87d

Please sign in to comment.