mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Merge pull request #1271 from bf4/fix_digest_failure
Handle no serializer source file to digest.
This commit is contained in:
commit
b2cd7bb51a
@ -27,6 +27,20 @@ module ActiveModel
|
|||||||
)
|
)
|
||||||
/x
|
/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|
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
||||||
class_attribute :_type, instance_reader: true
|
class_attribute :_type, instance_reader: true
|
||||||
class_attribute :_attributes
|
class_attribute :_attributes
|
||||||
@ -43,9 +57,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.inherited(base)
|
def self.inherited(base)
|
||||||
|
caller_line = caller.first
|
||||||
base._attributes = _attributes.dup
|
base._attributes = _attributes.dup
|
||||||
base._attributes_keys = _attributes_keys.dup
|
base._attributes_keys = _attributes_keys.dup
|
||||||
base._cache_digest = digest_caller_file(caller.first)
|
base._cache_digest = digest_caller_file(caller_line)
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -105,12 +120,6 @@ module ActiveModel
|
|||||||
@serializers_cache ||= ThreadSafe::Cache.new
|
@serializers_cache ||= ThreadSafe::Cache.new
|
||||||
end
|
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
|
# @api private
|
||||||
def self.serializer_lookup_chain_for(klass)
|
def self.serializer_lookup_chain_for(klass)
|
||||||
chain = []
|
chain = []
|
||||||
|
|||||||
@ -3,6 +3,8 @@ require 'tempfile'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class CacheTest < Minitest::Test
|
class CacheTest < Minitest::Test
|
||||||
|
include ActiveSupport::Testing::Stream
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@ -170,6 +172,16 @@ module ActiveModel
|
|||||||
file.unlink
|
file.unlink
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def render_object_with_cache(obj)
|
def render_object_with_cache(obj)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user