Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle no serializer source file to digest. #1271

Merged
merged 1 commit into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 very descriptive error message explains the problem, and says what to do about it! yay
🎉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why is the freeze on an empty string?

Or is this something special with how the <<-STRING syntax works?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just a premature optimization :)

B mobile phone

On Oct 15, 2015, at 6:25 AM, L. Preston Sego III [email protected] wrote:

In 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
    
    but why is the freeze on an empty string?

Or is this something special with how the <<-STRING syntax works?


Reply to this email directly or view it on GitHub.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we fixed the case where the error is spit because the file name contains a whitespace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so

B mobile phone

On Oct 21, 2015, at 3:53 PM, Lucas Hosseini [email protected] wrote:

In 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
    
    Have we fixed the case where the error is spit because the file name contains a whitespace?


Reply to this email directly or view it on GitHub.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this PR fix actually? Was the problem not all along that we don't handle whitespace in filenames?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there's no source file. More info in description and tests

B mobile phone

On Oct 21, 2015, at 6:06 PM, Lucas Hosseini [email protected] wrote:

What does this PR fix actually? Was the problem not all along that we don't handle whitespace in filenames?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but what I meant was that the bug actually arose in the case the source file was deemed non-existent because the regex would not allow for whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

B mobile phone

On Oct 21, 2015, at 6:20 PM, Lucas Hosseini [email protected] wrote:

In 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
    
    Yeah but what I meant was that the bug actually arose in the case the source file was deemed non-existent because the regex would not allow for whitespace.


Reply to this email directly or view it on GitHub.

end

with_options instance_writer: false, instance_reader: false do |serializer|
class_attribute :_type, instance_reader: true
class_attribute :_attributes
Expand All @@ -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

Expand Down Expand Up @@ -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 = []
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