mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Handle no serializer source file to digest.
output warning Closes #1176
This commit is contained in:
parent
526b56e9a6
commit
8529ea43c9
@ -27,6 +27,20 @@ module ActiveModel
|
||||
)
|
||||
/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 @@ module ActiveModel
|
||||
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 @@ module ActiveModel
|
||||
@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 = []
|
||||
|
||||
@ -3,6 +3,8 @@ require 'tempfile'
|
||||
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 @@ module ActiveModel
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user