diff --git a/CHANGELOG.md b/CHANGELOG.md index f2607e07b..6308a81d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Features: Fixes: - [#1239](https://github.com/rails-api/active_model_serializers/pull/1239) Fix duplicates in JSON API compound documents (@beauby) - [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8) +- [#1358](https://github.com/rails-api/active_model_serializers/pull/1358) Handle serializer file paths with spaces (@rwstauner, @bf4) Misc: - [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 3975391c8..a34136c9f 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -23,7 +23,7 @@ class Serializer # c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb CALLER_FILE = / \A # start of string - \S+ # one or more non-spaces + .+ # file path (one or more characters) (?= # stop previous match when :\d+ # a colon is followed by one or more digits :in # followed by a colon followed by in diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index 284c7b2ea..d6b33edca 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'tmpdir' require 'tempfile' module ActiveModel class Serializer @@ -160,9 +161,23 @@ def test_serializer_file_path_on_windows assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path end + def test_serializer_file_path_with_space + path = '/Users/git/ember js/ember-crm-backend/app/serializers/lead_serializer.rb' + caller_line = "#{path}:1:in `'" + assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path + end + + def test_serializer_file_path_with_submatch + # The submatch in the path ensures we're using a correctly greedy regexp. + path = '/Users/git/ember js/ember:123:in x/app/serializers/lead_serializer.rb' + caller_line = "#{path}:1:in `'" + assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path + end + def test_digest_caller_file contents = "puts 'AMS rocks'!" - file = Tempfile.new('some_ruby.rb') + dir = Dir.mktmpdir('space char') + file = Tempfile.new('some_ruby.rb', dir) file.write(contents) path = file.path caller_line = "#{path}:1:in `'" @@ -170,6 +185,7 @@ def test_digest_caller_file assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents) ensure file.unlink + FileUtils.remove_entry dir end def test_warn_on_serializer_not_defined_in_file