Skip to content

Commit 39d6dab

Browse files
committed
Merge pull request #1067 from bf4/fix_warnings
Fix warnings
2 parents 49ea8bd + d315151 commit 39d6dab

20 files changed

+162
-132
lines changed

lib/active_model/serializable_resource.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "set"
1+
require 'set'
22
module ActiveModel
33
class SerializableResource
44

@@ -76,7 +76,9 @@ def serializer?
7676

7777
private
7878

79+
ActiveModelSerializers.silence_warnings do
7980
attr_reader :resource, :adapter_opts, :serializer_opts
81+
end
8082

8183
end
8284
end

lib/active_model/serializer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Serializer
99
autoload :Adapter
1010
autoload :Lint
1111
autoload :Associations
12+
autoload :Fieldset
1213
include Configuration
1314
include Associations
1415

@@ -66,10 +67,10 @@ def self.attribute(attr, options = {})
6667
@_attributes_keys[attr] = { key: key } if key != attr
6768
@_attributes << key unless @_attributes.include?(key)
6869

69-
unless respond_to?(key, false) || _fragmented.respond_to?(attr)
70+
ActiveModelSerializers.silence_warnings do
7071
define_method key do
7172
object.read_attribute_for_serialization(attr)
72-
end
73+
end unless respond_to?(key, false) || _fragmented.respond_to?(attr)
7374
end
7475
end
7576

lib/active_model/serializer/adapter.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
require 'active_model/serializer/adapter/fragment_cache'
2-
31
module ActiveModel
42
class Serializer
53
class Adapter
64
extend ActiveSupport::Autoload
7-
autoload :Json
5+
require 'active_model/serializer/adapter/json'
6+
require 'active_model/serializer/adapter/json_api'
87
autoload :FlattenJson
98
autoload :Null
10-
autoload :JsonApi
9+
autoload :FragmentCache
10+
11+
def self.create(resource, options = {})
12+
override = options.delete(:adapter)
13+
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
14+
klass.new(resource, options)
15+
end
16+
17+
def self.adapter_class(adapter)
18+
adapter_name = adapter.to_s.classify.sub("API", "Api")
19+
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
20+
end
1121

1222
attr_reader :serializer
1323

@@ -26,17 +36,6 @@ def as_json(options = nil)
2636
hash
2737
end
2838

29-
def self.create(resource, options = {})
30-
override = options.delete(:adapter)
31-
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
32-
klass.new(resource, options)
33-
end
34-
35-
def self.adapter_class(adapter)
36-
adapter_name = adapter.to_s.classify.sub("API", "Api")
37-
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
38-
end
39-
4039
def fragment_cache(*args)
4140
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
4241
end

lib/active_model/serializer/adapter/json/fragment_cache.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'active_model/serializer/adapter/fragment_cache'
12
module ActiveModel
23
class Serializer
34
class Adapter
@@ -12,4 +13,4 @@ def fragment_cache(cached_hash, non_cached_hash)
1213
end
1314
end
1415
end
15-
end
16+
end

lib/active_model/serializer/adapter/json_api/fragment_cache.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'active_model/serializer/adapter/fragment_cache'
12
module ActiveModel
23
class Serializer
34
class Adapter
@@ -20,4 +21,4 @@ def fragment_cache(root, cached_hash, non_cached_hash)
2021
end
2122
end
2223
end
23-
end
24+
end

lib/active_model/serializer/associations.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ def has_one(name, options = {})
7373
def associate(reflection)
7474
self._reflections = _reflections.dup
7575

76-
unless method_defined?(reflection.name)
77-
define_method reflection.name do
78-
object.send reflection.name
79-
end
80-
end
76+
define_method reflection.name do
77+
object.send reflection.name
78+
end unless method_defined?(reflection.name)
8179

8280
self._reflections << reflection
8381
end

lib/active_model/serializer/fieldset.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def fields_for(serializer)
1818

1919
private
2020

21+
ActiveModelSerializers.silence_warnings do
2122
attr_reader :raw_fields, :root
23+
end
2224

2325
def parsed_fields
2426
if raw_fields.is_a?(Hash)
@@ -37,4 +39,4 @@ def parsed_fields
3739

3840
end
3941
end
40-
end
42+
end

lib/active_model_serializers.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
module ActiveModelSerializers
2+
module_function
3+
4+
def silence_warnings
5+
verbose = $VERBOSE
6+
$VERBOSE = nil
7+
yield
8+
ensure
9+
$VERBOSE = verbose
10+
end
11+
12+
end
113
require 'active_model'
214
require 'active_model/serializer/version'
315
require 'active_model/serializer'
4-
require 'active_model/serializer/fieldset'
516
require 'active_model/serializable_resource'
617

718
begin

test/action_controller/serialization_scope_name_test.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
class DefaultScopeNameTest < ActionController::TestCase
55
class UserSerializer < ActiveModel::Serializer
66
attributes :admin?
7-
def admin?
8-
current_user.admin
7+
ActiveModelSerializers.silence_warnings do
8+
def admin?
9+
current_user.admin
10+
end
911
end
1012
end
1113

@@ -34,8 +36,10 @@ def test_default_scope_name
3436
class SerializationScopeNameTest < ActionController::TestCase
3537
class AdminUserSerializer < ActiveModel::Serializer
3638
attributes :admin?
37-
def admin?
38-
current_admin.admin
39+
ActiveModelSerializers.silence_warnings do
40+
def admin?
41+
current_admin.admin
42+
end
3943
end
4044
end
4145

@@ -60,4 +64,4 @@ def test_override_scope_name_with_controller
6064
get :render_new_user
6165
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
6266
end
63-
end
67+
end

test/action_controller/serialization_test.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ def render_fragment_changed_object_with_except_cache_enabled
129129
def render_fragment_changed_object_with_relationship
130130
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
131131
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
132-
author = Author.new(id: 1, name: 'Joao Moura.')
133132
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
134133

135134
generate_cached_serializer(like)
@@ -215,14 +214,16 @@ def test_render_json_object_without_serializer
215214
get :render_json_object_without_serializer
216215

217216
assert_equal 'application/json', @response.content_type
218-
assert_equal ({error: 'Result is Invalid'}).to_json, @response.body
217+
expected_body = {error: 'Result is Invalid'}
218+
assert_equal expected_body.to_json, @response.body
219219
end
220220

221221
def test_render_json_array_object_without_serializer
222222
get :render_json_array_object_without_serializer
223223

224224
assert_equal 'application/json', @response.content_type
225-
assert_equal ([{error: 'Result is Invalid'}]).to_json, @response.body
225+
expected_body = [{error: 'Result is Invalid'}]
226+
assert_equal expected_body.to_json, @response.body
226227
end
227228

228229
def test_render_array_using_implicit_serializer
@@ -405,9 +406,9 @@ def use_adapter?
405406
false
406407
end
407408
}.new
408-
assert_match /adapter: false/, (capture(:stderr) {
409+
assert_match(/adapter: false/, (capture(:stderr) {
409410
controller.get_serializer(Profile.new)
410-
})
411+
}))
411412
end
412413

413414
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance

test/adapter/json/belongs_to_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def setup
1111
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
1212
@post.comments = [@comment]
1313
@anonymous_post.comments = []
14-
@post.author = @author
1514
@comment.post = @post
1615
@comment.author = nil
1716
@anonymous_post.author = nil

test/capture_warnings.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def before_tests
2020

2121
def after_tests
2222
stderr_file.rewind
23-
lines = stderr_file.read.split("\n").uniq
23+
lines = stderr_file.read.split("\n")
2424
stderr_file.close!
2525

2626
$stderr.reopen(STDERR)

test/fixtures/poro.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
verbose = $VERBOSE
2+
$VERBOSE = nil
13
class Model
24
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
35

@@ -260,9 +262,4 @@ def maker
260262
cache only: [:id]
261263
attributes :id
262264
end
263-
264-
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
265-
def json_key
266-
raise StandardError, 'Intentional error for rescue_from test'
267-
end
268-
end
265+
$VERBOSE = verbose

test/generators/serializer_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def test_with_no_attributes_does_not_add_extra_space
4747
run_generator ["account"]
4848
assert_file "app/serializers/account_serializer.rb" do |content|
4949
if RUBY_PLATFORM =~ /mingw/
50-
assert_no_match /\r\n\r\nend/, content
50+
assert_no_match(/\r\n\r\nend/, content)
5151
else
52-
assert_no_match /\n\nend/, content
52+
assert_no_match(/\n\nend/, content)
5353
end
5454
end
5555
end

test/serializers/associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_has_many_with_no_serializer
8181
def test_serializer_options_are_passed_into_associations_serializers
8282
association = @post_serializer
8383
.associations
84-
.detect { |association| association.key == :comments }
84+
.detect { |assoc| assoc.key == :comments }
8585

8686
assert association.serializer.first.custom_options[:custom_options]
8787
end

test/serializers/cache_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def test_cache_key_definition
4747
end
4848

4949
def test_cache_key_interpolation_with_updated_at
50-
author = render_object_with_cache(@author)
50+
render_object_with_cache(@author)
5151
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
5252
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
5353
end
5454

5555
def test_default_cache_key_fallback
56-
comment = render_object_with_cache(@comment)
56+
render_object_with_cache(@comment)
5757
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
5858
end
5959

@@ -74,7 +74,7 @@ def test_associations_separately_cache
7474
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
7575

7676
Timecop.freeze(Time.now) do
77-
post = render_object_with_cache(@post)
77+
render_object_with_cache(@post)
7878

7979
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
8080
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
@@ -122,7 +122,7 @@ def test_fragment_fetch_with_virtual_associations
122122
end
123123

124124
def test_uses_file_digest_in_cache_key
125-
blog = render_object_with_cache(@blog)
125+
render_object_with_cache(@blog)
126126
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
127127
end
128128

test/support/rails_app.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Foo < Rails::Application
2+
if Rails::VERSION::MAJOR >= 4
3+
config.eager_load = false
4+
config.secret_key_base = 'abc123'
5+
config.action_controller.perform_caching = true
6+
config.active_support.test_order = :random
7+
config.logger = Logger.new(nil)
8+
ActionController::Base.cache_store = :memory_store
9+
end
10+
end
11+
Foo.initialize!
12+
13+
module TestHelper
14+
Routes = ActionDispatch::Routing::RouteSet.new
15+
Routes.draw do
16+
get ':controller(/:action(/:id))'
17+
get ':controller(/:action)'
18+
end
19+
20+
ActionController::Base.send :include, Routes.url_helpers
21+
end

test/support/stream_capture.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Use cleaner stream testing interface from Rails 5 if available
2+
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
3+
begin
4+
require "active_support/testing/stream"
5+
rescue LoadError
6+
module ActiveSupport
7+
module Testing
8+
module Stream #:nodoc:
9+
private
10+
11+
def silence_stream(stream)
12+
old_stream = stream.dup
13+
stream.reopen(IO::NULL)
14+
stream.sync = true
15+
yield
16+
ensure
17+
stream.reopen(old_stream)
18+
old_stream.close
19+
end
20+
21+
def quietly
22+
silence_stream(STDOUT) do
23+
silence_stream(STDERR) do
24+
yield
25+
end
26+
end
27+
end
28+
29+
def capture(stream)
30+
stream = stream.to_s
31+
captured_stream = Tempfile.new(stream)
32+
stream_io = eval("$#{stream}")
33+
origin_stream = stream_io.dup
34+
stream_io.reopen(captured_stream)
35+
36+
yield
37+
38+
stream_io.rewind
39+
return captured_stream.read
40+
ensure
41+
captured_stream.close
42+
captured_stream.unlink
43+
stream_io.reopen(origin_stream)
44+
end
45+
end
46+
end
47+
end
48+
end
49+

test/support/test_case.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ActionController::TestCase.class_eval do
2+
def setup
3+
@routes = TestHelper::Routes
4+
end
5+
end

0 commit comments

Comments
 (0)