From 94db09b3f6aef48229a5120ad3b4983df4426c0a Mon Sep 17 00:00:00 2001 From: Noah Silas Date: Thu, 26 May 2016 09:58:05 -0700 Subject: [PATCH] Fix RuboCop 0.40 linter errors (#1722) These errors are breaking the build, which seems to use RuboCop 0.40 [1] despite the Gemfile.lock pinning rubocop to 0.38. New lints that I am updating the code style to reflect: - Style/EmptyCaseCondition: Do not use empty case condition, instead use an if expression. - Style/MultilineArrayBraceLayout: Closing array brace must be on the same line as the last array element when opening brace is on the same line as the first array element. - Style/MultilineHashBraceLayout: Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element. - Style/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument. [1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0 --- lib/active_model/serializer/include_tree.rb | 7 +- lib/active_model_serializers/deprecate.rb | 3 +- .../explicit_serializer_test.rb | 3 +- .../action_controller/json_api/errors_test.rb | 4 +- test/action_controller/serialization_test.rb | 9 +- test/adapter/json_api/collection_test.rb | 7 +- test/adapter/json_api/errors_test.rb | 28 ++-- test/adapter/json_api/linked_test.rb | 13 +- test/adapter/json_api/links_test.rb | 3 +- .../adapter/json_api/pagination_links_test.rb | 3 +- test/adapter/json_api/transform_test.rb | 151 +++++++++--------- test/adapter/json_test.rb | 7 +- test/serializable_resource_test.rb | 24 +-- test/serializers/meta_test.rb | 18 ++- 14 files changed, 147 insertions(+), 133 deletions(-) diff --git a/lib/active_model/serializer/include_tree.rb b/lib/active_model/serializer/include_tree.rb index d5cbe464..582188f1 100644 --- a/lib/active_model/serializer/include_tree.rb +++ b/lib/active_model/serializer/include_tree.rb @@ -95,12 +95,11 @@ module ActiveModel def [](key) # TODO(beauby): Adopt a lazy caching strategy for generating subtrees. - case - when @hash.key?(key) + if @hash.key?(key) self.class.new(@hash[key]) - when @hash.key?(:*) + elsif @hash.key?(:*) self.class.new(@hash[:*]) - when @hash.key?(:**) + elsif @hash.key?(:**) self.class.new(:** => {}) else nil diff --git a/lib/active_model_serializers/deprecate.rb b/lib/active_model_serializers/deprecate.rb index a3109d28..7683ed43 100644 --- a/lib/active_model_serializers/deprecate.rb +++ b/lib/active_model_serializers/deprecate.rb @@ -36,8 +36,7 @@ module ActiveModelSerializers target = is_a?(Module) ? "#{self}." : "#{self.class}#" msg = ["NOTE: #{target}#{name} is deprecated", replacement == :none ? ' with no replacement' : "; use #{replacement} instead", - "\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(":")}" - ] + "\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(":")}"] warn "#{msg.join}." send old, *args, &block end diff --git a/test/action_controller/explicit_serializer_test.rb b/test/action_controller/explicit_serializer_test.rb index 17c70350..00b4db81 100644 --- a/test/action_controller/explicit_serializer_test.rb +++ b/test/action_controller/explicit_serializer_test.rb @@ -100,7 +100,8 @@ module ActionController get :render_array_using_explicit_serializer_and_custom_serializers expected = [ - { 'title' => 'New Post', + { + 'title' => 'New Post', 'body' => 'Body', 'id' => assigns(:post).id, 'comments' => [{ 'id' => 1 }, { 'id' => 2 }], diff --git a/test/action_controller/json_api/errors_test.rb b/test/action_controller/json_api/errors_test.rb index d5890110..bdc8643f 100644 --- a/test/action_controller/json_api/errors_test.rb +++ b/test/action_controller/json_api/errors_test.rb @@ -7,8 +7,8 @@ module ActionController def test_active_model_with_multiple_errors get :render_resource_with_errors - expected_errors_object = - { :errors => + expected_errors_object = { + :errors => [ { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' }, { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' }, diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index cf757b1c..196d8493 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -294,7 +294,8 @@ module ActionController comments: [ { id: 1, - body: 'ZOMG A COMMENT' } + body: 'ZOMG A COMMENT' + } ], blog: { id: 999, @@ -333,7 +334,8 @@ module ActionController comments: [ { id: 1, - body: 'ZOMG A COMMENT' } + body: 'ZOMG A COMMENT' + } ], blog: { id: 999, @@ -407,7 +409,8 @@ module ActionController comments: [ { id: 1, - body: 'ZOMG A COMMENT' } + body: 'ZOMG A COMMENT' + } ], blog: { id: 999, diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index 7c2ef284..e60a824e 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -58,9 +58,10 @@ module ActiveModelSerializers def test_limiting_fields actual = ActiveModelSerializers::SerializableResource.new( - [@first_post, @second_post], adapter: :json_api, - fields: { posts: %w(title comments blog author) }) - .serializable_hash + [@first_post, @second_post], + adapter: :json_api, + fields: { posts: %w(title comments blog author) } + ).serializable_hash expected = [ { id: '1', diff --git a/test/adapter/json_api/errors_test.rb b/test/adapter/json_api/errors_test.rb index a863124a..d20dc784 100644 --- a/test/adapter/json_api/errors_test.rb +++ b/test/adapter/json_api/errors_test.rb @@ -22,14 +22,13 @@ module ActiveModelSerializers assert_equal serializable_resource.serializer_instance.attributes, {} assert_equal serializable_resource.serializer_instance.object, @resource - expected_errors_object = - { :errors => - [ - { - source: { pointer: '/data/attributes/name' }, - detail: 'cannot be nil' - } - ] + expected_errors_object = { + :errors => [ + { + source: { pointer: '/data/attributes/name' }, + detail: 'cannot be nil' + } + ] } assert_equal serializable_resource.as_json, expected_errors_object end @@ -48,13 +47,12 @@ module ActiveModelSerializers assert_equal serializable_resource.serializer_instance.attributes, {} assert_equal serializable_resource.serializer_instance.object, @resource - expected_errors_object = - { :errors => - [ - { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' }, - { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' }, - { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' } - ] + expected_errors_object = { + :errors => [ + { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' }, + { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' }, + { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' } + ] } assert_equal serializable_resource.as_json, expected_errors_object end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index 02ca27d7..b5d79ba0 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -341,9 +341,10 @@ module ActiveModelSerializers def test_no_duplicates_collection hash = ActiveModelSerializers::SerializableResource.new( - [@post1, @post2], adapter: :json_api, - include: '*.*') - .serializable_hash + [@post1, @post2], + adapter: :json_api, + include: '*.*' + ).serializable_hash expected = [ { type: 'authors', id: '1', @@ -364,7 +365,8 @@ module ActiveModelSerializers hash = ActiveModelSerializers::SerializableResource.new( @nestedpost1, adapter: :json_api, - include: '*').serializable_hash + include: '*' + ).serializable_hash expected = [ type: 'nested-posts', id: '2', relationships: { @@ -383,7 +385,8 @@ module ActiveModelSerializers hash = ActiveModelSerializers::SerializableResource.new( [@nestedpost1, @nestedpost2], adapter: :json_api, - include: '*').serializable_hash + include: '*' + ).serializable_hash assert_nil(hash[:included]) end end diff --git a/test/adapter/json_api/links_test.rb b/test/adapter/json_api/links_test.rb index 8f26f34a..981e32b5 100644 --- a/test/adapter/json_api/links_test.rb +++ b/test/adapter/json_api/links_test.rb @@ -40,7 +40,8 @@ module ActiveModelSerializers stuff: 'value' } } - }).serializable_hash + } + ).serializable_hash expected = { self: { href: 'http://example.com/posts', diff --git a/test/adapter/json_api/pagination_links_test.rb b/test/adapter/json_api/pagination_links_test.rb index e6f74ebe..071956ea 100644 --- a/test/adapter/json_api/pagination_links_test.rb +++ b/test/adapter/json_api/pagination_links_test.rb @@ -43,7 +43,8 @@ module ActiveModelSerializers end def data - { data: [ + { + data: [ { id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } }, { id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } }, { id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } }, diff --git a/test/adapter/json_api/transform_test.rb b/test/adapter/json_api/transform_test.rb index 26f4b005..2eb2e5ae 100644 --- a/test/adapter/json_api/transform_test.rb +++ b/test/adapter/json_api/transform_test.rb @@ -86,7 +86,8 @@ module ActiveModelSerializers data: [ { id: '7', type: 'comments' }, { id: '12', type: 'comments' } - ] } + ] + } }, links: { self: 'http://example.com/posts/1337', @@ -122,7 +123,8 @@ module ActiveModelSerializers data: [ { id: '7', type: 'comments' }, { id: '12', type: 'comments' } - ] } + ] + } }, links: { self: 'http://example.com/posts/1337', @@ -158,7 +160,8 @@ module ActiveModelSerializers Data: [ { Id: '7', Type: 'Comments' }, { Id: '12', Type: 'Comments' } - ] } + ] + } }, Links: { Self: 'http://example.com/posts/1337', @@ -192,7 +195,8 @@ module ActiveModelSerializers data: [ { id: '7', type: 'comments' }, { id: '12', type: 'comments' } - ] } + ] + } }, links: { self: 'http://example.com/posts/1337', @@ -226,7 +230,8 @@ module ActiveModelSerializers data: [ { id: '7', type: 'comments' }, { id: '12', type: 'comments' } - ] } + ] + } }, links: { self: 'http://example.com/posts/1337', @@ -270,7 +275,8 @@ module ActiveModelSerializers Data: [ { Id: '7', Type: 'Comments' }, { Id: '12', Type: 'Comments' } - ] } + ] + } }, Links: { Self: 'http://example.com/posts/1337', @@ -304,7 +310,8 @@ module ActiveModelSerializers data: [ { id: '7', type: 'comments' }, { id: '12', type: 'comments' } - ] } + ] + } }, links: { self: 'http://example.com/posts/1337', @@ -324,18 +331,18 @@ module ActiveModelSerializers serializer = ActiveModel::Serializer::ErrorSerializer.new(resource) adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) result = adapter.serializable_hash - expected_errors_object = - { :errors => - [ - { - :source => { :pointer => '/data/attributes/published-at' }, - :detail => 'must be in the future' }, - { - :source => { :pointer => '/data/attributes/title' }, - :detail => 'must be longer' - } - ] - } + expected_errors_object = { + :errors => [ + { + :source => { :pointer => '/data/attributes/published-at' }, + :detail => 'must be in the future' + }, + { + :source => { :pointer => '/data/attributes/title' }, + :detail => 'must be longer' + } + ] + } assert_equal expected_errors_object, result end @@ -349,19 +356,18 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) adapter.serializable_hash end - expected_errors_object = - { :Errors => - [ - { - :Source => { :Pointer => '/data/attributes/PublishedAt' }, - :Detail => 'must be in the future' - }, - { - :Source => { :Pointer => '/data/attributes/Title' }, - :Detail => 'must be longer' - } - ] - } + expected_errors_object = { + :Errors => [ + { + :Source => { :Pointer => '/data/attributes/PublishedAt' }, + :Detail => 'must be in the future' + }, + { + :Source => { :Pointer => '/data/attributes/Title' }, + :Detail => 'must be longer' + } + ] + } assert_equal expected_errors_object, result end @@ -375,19 +381,18 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) adapter.serializable_hash end - expected_errors_object = - { :Errors => - [ - { - :Source => { :Pointer => '/data/attributes/PublishedAt' }, - :Detail => 'must be in the future' - }, - { - :Source => { :Pointer => '/data/attributes/Title' }, - :Detail => 'must be longer' - } - ] - } + expected_errors_object = { + :Errors => [ + { + :Source => { :Pointer => '/data/attributes/PublishedAt' }, + :Detail => 'must be in the future' + }, + { + :Source => { :Pointer => '/data/attributes/Title' }, + :Detail => 'must be longer' + } + ] + } assert_equal expected_errors_object, result end @@ -402,18 +407,17 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) result = adapter.serializable_hash - expected_errors_object = - { :errors => - [ - { - :source => { :pointer => '/data/attributes/published-at' }, - :detail => 'must be in the future' - }, - { - :source => { :pointer => '/data/attributes/title' }, - :detail => 'must be longer' - } - ] + expected_errors_object = { + :errors => [ + { + :source => { :pointer => '/data/attributes/published-at' }, + :detail => 'must be in the future' + }, + { + :source => { :pointer => '/data/attributes/title' }, + :detail => 'must be longer' + } + ] } assert_equal expected_errors_object, result end @@ -429,12 +433,11 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) result = adapter.serializable_hash - expected_errors_object = - { :errors => - [ - { :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' }, - { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } - ] + expected_errors_object = { + :errors => [ + { :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' }, + { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } + ] } assert_equal expected_errors_object, result end @@ -466,12 +469,11 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) result = adapter.serializable_hash - expected_errors_object = - { :Errors => - [ - { :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' }, - { :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' } - ] + expected_errors_object = { + :Errors => [ + { :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' }, + { :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' } + ] } assert_equal expected_errors_object, result end @@ -487,12 +489,11 @@ module ActiveModelSerializers adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options) result = adapter.serializable_hash - expected_errors_object = - { :errors => - [ - { :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' }, - { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } - ] + expected_errors_object = { + :errors => [ + { :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' }, + { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } + ] } assert_equal expected_errors_object, result end diff --git a/test/adapter/json_test.rb b/test/adapter/json_test.rb index a7d3bc83..e5a5974c 100644 --- a/test/adapter/json_test.rb +++ b/test/adapter/json_test.rb @@ -33,9 +33,10 @@ module ActiveModelSerializers assert_equal({ id: 1, - reviews: [{ id: 1, body: 'ZOMG A COMMENT' }, - { id: 2, body: 'ZOMG ANOTHER COMMENT' } - ], + reviews: [ + { id: 1, body: 'ZOMG A COMMENT' }, + { id: 2, body: 'ZOMG ANOTHER COMMENT' } + ], writer: { id: 1, name: 'Steve K.' }, site: { id: 1, name: 'My Blog!!' } }, adapter.serializable_hash[:post]) diff --git a/test/serializable_resource_test.rb b/test/serializable_resource_test.rb index abef37e3..69849ac5 100644 --- a/test/serializable_resource_test.rb +++ b/test/serializable_resource_test.rb @@ -48,12 +48,12 @@ module ActiveModelSerializers resource, { serializer: ActiveModel::Serializer::ErrorSerializer, adapter: :json_api - }) - expected_response_document = - { :errors => - [ - { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' } - ] + } + ) + expected_response_document = { + :errors => [ + { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' } + ] } assert_equal serializable_resource.as_json(options), expected_response_document end @@ -69,12 +69,12 @@ module ActiveModelSerializers serializer: ActiveModel::Serializer::ErrorsSerializer, each_serializer: ActiveModel::Serializer::ErrorSerializer, adapter: :json_api - }) - expected_response_document = - { :errors => - [ - { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' } - ] + } + ) + expected_response_document = { + :errors => [ + { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' } + ] } assert_equal serializable_resource.as_json(options), expected_response_document end diff --git a/test/serializers/meta_test.rb b/test/serializers/meta_test.rb index ade2e81e..64209321 100644 --- a/test/serializers/meta_test.rb +++ b/test/serializers/meta_test.rb @@ -15,7 +15,8 @@ module ActiveModel @blog, adapter: :json, serializer: AlternateBlogSerializer, - meta: { total: 10 }).as_json + meta: { total: 10 } + ).as_json expected = { blog: { id: 1, @@ -65,7 +66,8 @@ module ActiveModel @blog, adapter: :attributes, serializer: AlternateBlogSerializer, - meta: { total: 10 }).as_json + meta: { total: 10 } + ).as_json expected = { id: 1, title: 'AMS Hints' @@ -79,7 +81,8 @@ module ActiveModel adapter: :json, serializer: AlternateBlogSerializer, meta: { total: 10 }, - meta_key: 'haha_meta').as_json + meta_key: 'haha_meta' + ).as_json expected = { blog: { id: 1, @@ -98,7 +101,8 @@ module ActiveModel adapter: :json_api, serializer: AlternateBlogSerializer, meta: { total: 10 }, - meta_key: 'haha_meta').as_json + meta_key: 'haha_meta' + ).as_json expected = { data: { id: '1', @@ -148,7 +152,8 @@ module ActiveModel actual = ActiveModelSerializers::SerializableResource.new( [@blog], adapter: :attributes, - meta: { total: 10 }).as_json + meta: { total: 10 } + ).as_json expected = [{ id: 1, name: 'AMS Hints', @@ -170,7 +175,8 @@ module ActiveModel [@blog], adapter: :json, meta: { total: 10 }, - meta_key: 'haha_meta').as_json + meta_key: 'haha_meta' + ).as_json expected = { blogs: [{ id: 1,