From 09c97de90d3488b3d849e0e0373f41631204d946 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Aug 2015 23:17:25 -0500 Subject: [PATCH 1/6] Add Style enforcer (via Rubocop) It will fail the build, but as it is currently, most of the cops are 'todos'. Great for new contributors.. :) --- .rubocop.yml | 49 +++ .rubocop_todo.yml | 347 ++++++++++++++++++ .travis.yml | 8 + Gemfile | 4 + Rakefile | 17 +- .../serializer/serializer_generator.rb | 2 +- .../{serializer.rb => serializer.rb.erb} | 0 lib/tasks/rubocop.rake | 0 test/action_controller/serialization_test.rb | 7 +- test/fixtures/poro.rb | 2 +- test/serializers/serializer_for_test.rb | 2 +- test/support/stream_capture.rb | 2 +- 12 files changed, 430 insertions(+), 10 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml rename lib/generators/serializer/templates/{serializer.rb => serializer.rb.erb} (100%) create mode 100644 lib/tasks/rubocop.rake diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 000000000..d2d21b50f --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,49 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + Exclude: + - config/initializers/forbidden_yaml.rb + - !ruby/regexp /(vendor|bundle|bin|db)\/.*/ + RunRailsCops: true + DisplayCopNames: true + DisplayStyleGuide: true + +Lint/NestedMethodDefinition: + Enabled: false + Exclude: + - test/action_controller/serialization_test.rb + +Style/StringLiterals: + EnforcedStyle: single_quotes + +Metrics/AbcSize: + Max: 35 # TODO: Lower to 15 + +Metrics/ClassLength: + Max: 261 # TODO: Lower to 100 + Exclude: + - test/**/*.rb + +Metrics/CyclomaticComplexity: + Max: 7 # TODO: Lower to 6 + +Metrics/LineLength: + Max: 251 # TODO: Lower to 80 + +Metrics/MethodLength: + Max: 106 # TODO: Lower to 10 + +Metrics/PerceivedComplexity: + Max: 9 # TODO: Lower to 7 + +Style/AlignParameters: + EnforcedStyle: with_fixed_indentation + +Style/ClassAndModuleChildren: + EnforcedStyle: compact + +Style/Documentation: + Enabled: false + +Style/MultilineOperationIndentation: + EnforcedStyle: indented diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 000000000..4efa90623 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,347 @@ +# This configuration was generated by `rubocop --auto-gen-config` +# on 2015-08-30 23:03:50 -0500 using RuboCop version 0.31.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: AllowSafeAssignment. +Lint/AssignmentInCondition: + Enabled: false + +# Offense count: 1 +Lint/EmptyEnsure: + Enabled: false + +# Offense count: 1 +Lint/HandleExceptions: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Lint/UnusedBlockArgument: + Enabled: false + +# Offense count: 9 +# Cop supports --auto-correct. +Lint/UnusedMethodArgument: + Enabled: false + +# Offense count: 1 +Lint/UselessAccessModifier: + Enabled: false + +# Offense count: 3 +Lint/UselessAssignment: + Enabled: false + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, SupportedStyles. +Rails/Date: + Enabled: false + +# Offense count: 8 +# Configuration parameters: EnforcedStyle, SupportedStyles. +Rails/TimeZone: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/AccessModifierIndentation: + Enabled: false + +# Offense count: 16 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle, SupportedLastArgumentHashStyles. +Style/AlignHash: + Enabled: false + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/AlignParameters: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/AndOr: + Enabled: false + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods. +Style/BlockDelimiters: + Enabled: false + +# Offense count: 46 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/BracesAroundHashParameters: + Enabled: false + +# Offense count: 167 +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/ClassAndModuleChildren: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/CommentIndentation: + Enabled: false + +# Offense count: 1 +Style/DoubleNegation: + Enabled: false + +# Offense count: 1 +Style/EachWithObject: + Enabled: false + +# Offense count: 4 +# Cop supports --auto-correct. +Style/EmptyLines: + Enabled: false + +# Offense count: 3 +# Cop supports --auto-correct. +Style/EmptyLinesAroundAccessModifier: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundBlockBody: + Enabled: false + +# Offense count: 16 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundClassBody: + Enabled: false + +# Offense count: 9 +# Cop supports --auto-correct. +Style/EmptyLinesAroundMethodBody: + Enabled: false + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundModuleBody: + Enabled: false + +# Offense count: 3 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Enabled: false + +# Offense count: 12 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues. +Style/HashSyntax: + Enabled: false + +# Offense count: 9 +# Cop supports --auto-correct. +Style/IndentArray: + Enabled: false + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/IndentHash: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/IndentationConsistency: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: Width. +Style/IndentationWidth: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/Lambda: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Style/MethodCallParentheses: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/MethodDefParentheses: + Enabled: false + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/MultilineOperationIndentation: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/NegatedIf: + Enabled: false + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles. +Style/Next: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/NumericLiterals: + MinDigits: 7 + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Style/PerlBackrefs: + Enabled: false + +# Offense count: 6 +# Configuration parameters: NamePrefix, NamePrefixBlacklist. +Style/PredicateName: + Enabled: false + +# Offense count: 7 +# Cop supports --auto-correct. +Style/RedundantSelf: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: + Enabled: false + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/SignalException: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowIfMethodIsEmpty. +Style/SingleLineMethods: + Enabled: false + +# Offense count: 26 +# Cop supports --auto-correct. +Style/SpaceAfterColon: + Enabled: false + +# Offense count: 7 +# Cop supports --auto-correct. +Style/SpaceAfterComma: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/SpaceAfterNot: + Enabled: false + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/SpaceAroundEqualsInParameterDefault: + Enabled: false + +# Offense count: 39 +# Cop supports --auto-correct. +# Configuration parameters: MultiSpaceAllowedForOperators. +Style/SpaceAroundOperators: + Enabled: false + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/SpaceBeforeBlockBraces: + Enabled: false + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +Style/SpaceInsideBlockBraces: + Enabled: false + +# Offense count: 20 +# Cop supports --auto-correct. +Style/SpaceInsideBrackets: + Enabled: false + +# Offense count: 179 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. +Style/SpaceInsideHashLiteralBraces: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/SpaceInsideParens: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/StringLiteralsInInterpolation: + Enabled: false + +# Offense count: 1 +Style/StructInheritance: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: IgnoredMethods. +Style/SymbolProc: + Enabled: false + +# Offense count: 9 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/TrailingBlankLines: + Enabled: false + +# Offense count: 6 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline, SupportedStyles. +Style/TrailingComma: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Style/TrailingWhitespace: + Enabled: false + +# Offense count: 1 +Style/UnlessElse: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Style/UnneededPercentQ: + Enabled: false + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: WordRegex. +Style/WordArray: + MinSize: 2 diff --git a/.travis.yml b/.travis.yml index 6ebfd7032..9aaaafdd6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,9 @@ language: ruby sudo: false +cache: + bundler: true + rvm: - 1.9.3 - 2.0.0 @@ -14,6 +17,10 @@ rvm: install: - bundle install --retry=3 +script: + - bundle exec rake + - bundle exec rake rubocop + env: - "RAILS_VERSION=4.0" - "RAILS_VERSION=4.1" @@ -24,3 +31,4 @@ matrix: allow_failures: - rvm: ruby-head - env: "RAILS_VERSION=master" + fast_finish: true diff --git a/Gemfile b/Gemfile index e0a4364cb..a9846cb0b 100644 --- a/Gemfile +++ b/Gemfile @@ -33,3 +33,7 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +group :development, :test do + gem 'rubocop', '~> 0.33.0', require: false +end diff --git a/Rakefile b/Rakefile index 8a1f1e9e8..f7e165185 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,22 @@ -require "bundler/gem_tasks" +require 'bundler/gem_tasks' + +begin + require 'rubocop' + require 'rubocop/rake_task' +rescue LoadError +else + Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop) + desc 'Execute rubocop' + RuboCop::RakeTask.new(:rubocop) do |task| + task.options = ['--rails', '--display-cop-names', '--display-style-guide'] + task.fail_on_error = true + end +end require 'rake/testtask' Rake::TestTask.new do |t| - t.libs << "test" + t.libs << 'test' t.test_files = FileList['test/**/*_test.rb'] t.ruby_opts = ['-r./test/test_helper.rb'] t.verbose = true diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index 54db86e21..77ed442e9 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -9,7 +9,7 @@ class SerializerGenerator < NamedBase class_option :parent, :type => :string, :desc => "The parent class for the generated serializer" def create_serializer_file - template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") + template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") end private diff --git a/lib/generators/serializer/templates/serializer.rb b/lib/generators/serializer/templates/serializer.rb.erb similarity index 100% rename from lib/generators/serializer/templates/serializer.rb rename to lib/generators/serializer/templates/serializer.rb.erb diff --git a/lib/tasks/rubocop.rake b/lib/tasks/rubocop.rake new file mode 100644 index 000000000..e69de29bb diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index fab43af07..733500912 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -1,4 +1,3 @@ - require 'test_helper' module ActionController @@ -189,7 +188,7 @@ def test_render_using_default_root def test_render_array_using_custom_root get :render_array_using_custom_root - expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]} + expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]} assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -197,7 +196,7 @@ def test_render_array_using_custom_root def test_render_array_that_is_empty_using_custom_root get :render_array_that_is_empty_using_custom_root - expected = {custom_roots: []} + expected = {custom_roots: []} assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -205,7 +204,7 @@ def test_render_array_that_is_empty_using_custom_root def test_render_object_using_custom_root get :render_object_using_custom_root - expected = {custom_root: {name: "Name 1", description: "Description 1"}} + expected = {custom_root: {name: 'Name 1', description: 'Description 1'}} assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 815205c47..fa871d9ca 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -83,7 +83,7 @@ class ProfilePreviewSerializer < ActiveModel::Serializer Place = Class.new(Model) Tag = Class.new(Model) VirtualValue = Class.new(Model) -Comment = Class.new(Model) do +Comment = Class.new(Model) do # Uses a custom non-time-based cache key def cache_key "#{self.class.name.downcase}/#{self.id}" diff --git a/test/serializers/serializer_for_test.rb b/test/serializers/serializer_for_test.rb index 1fd4c10e1..a0dc0888e 100644 --- a/test/serializers/serializer_for_test.rb +++ b/test/serializers/serializer_for_test.rb @@ -26,7 +26,7 @@ def test_overwritten_serializer_for_array end end - class SerializerTest < Minitest::Test + class SerializerTest < Minitest::Test class MyProfile < Profile end class CustomProfile diff --git a/test/support/stream_capture.rb b/test/support/stream_capture.rb index 49e3a1454..20affb713 100644 --- a/test/support/stream_capture.rb +++ b/test/support/stream_capture.rb @@ -29,7 +29,7 @@ def quietly def capture(stream) stream = stream.to_s captured_stream = Tempfile.new(stream) - stream_io = eval("$#{stream}") + stream_io = eval("$#{stream}") # rubocop:disable Lint/Eval origin_stream = stream_io.dup stream_io.reopen(captured_stream) From bdfe13c5276640c2c91db4be09c3eec4cac2ad68 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 30 Aug 2015 23:20:25 -0500 Subject: [PATCH 2/6] Style/StringLiterals single quote all the things --- lib/action_controller/serialization.rb | 2 +- lib/active_model/serializer/adapter.rb | 8 +- lib/active_model/serializer/lint.rb | 4 +- lib/active_model/serializer/version.rb | 2 +- .../serializer/serializer_generator.rb | 12 +- .../adapter_selector_test.rb | 6 +- .../explicit_serializer_test.rb | 18 +-- .../action_controller/json_api/linked_test.rb | 52 +++---- .../json_api/pagination_test.rb | 48 +++--- test/action_controller/serialization_test.rb | 46 +++--- test/adapter/fragment_cache_test.rb | 2 +- test/adapter/json/belongs_to_test.rb | 6 +- test/adapter/json/collection_test.rb | 24 +-- test/adapter/json/has_many_test.rb | 6 +- test/adapter/json_api/belongs_to_test.rb | 68 ++++----- test/adapter/json_api/collection_test.rb | 44 +++--- .../json_api/has_many_embed_ids_test.rb | 4 +- .../has_many_explicit_serializer_test.rb | 6 +- test/adapter/json_api/has_many_test.rb | 42 +++--- test/adapter/json_api/has_one_test.rb | 16 +- test/adapter/json_api/json_api_test.rb | 10 +- test/adapter/json_api/linked_test.rb | 138 +++++++++--------- .../adapter/json_api/pagination_links_test.rb | 6 +- .../json_api/resource_type_config_test.rb | 2 +- test/adapter/json_test.rb | 10 +- test/array_serializer_test.rb | 6 +- test/capture_warnings.rb | 14 +- test/fixtures/poro.rb | 2 +- test/generators/serializer_generator_test.rb | 20 +-- test/serializers/associations_test.rb | 2 +- test/serializers/attribute_test.rb | 10 +- test/serializers/attributes_test.rb | 6 +- test/serializers/cache_test.rb | 8 +- test/serializers/meta_test.rb | 40 ++--- test/support/stream_capture.rb | 2 +- test/test_helper.rb | 2 +- 36 files changed, 347 insertions(+), 347 deletions(-) diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index 0850f7417..020261314 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -21,7 +21,7 @@ def serialization_scope def get_serializer(resource, options = {}) if ! use_adapter? - warn "ActionController::Serialization#use_adapter? has been removed. "\ + warn 'ActionController::Serialization#use_adapter? has been removed. '\ "Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize" options[:adapter] = false end diff --git a/lib/active_model/serializer/adapter.rb b/lib/active_model/serializer/adapter.rb index 69593828a..1cbeb9b76 100644 --- a/lib/active_model/serializer/adapter.rb +++ b/lib/active_model/serializer/adapter.rb @@ -15,7 +15,7 @@ def self.create(resource, options = {}) end def self.adapter_class(adapter) - adapter_name = adapter.to_s.classify.sub("API", "Api") + adapter_name = adapter.to_s.classify.sub('API', 'Api') "ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize end @@ -68,12 +68,12 @@ def cache_key parts = [] parts << object_cache_key parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest] - parts.join("/") + parts.join('/') end def object_cache_key object_time_safe = @cached_serializer.object.updated_at - object_time_safe = object_time_safe.strftime("%Y%m%d%H%M%S%9N") if object_time_safe.respond_to?(:strftime) + object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime) (@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key end @@ -82,7 +82,7 @@ def meta end def meta_key - serializer.meta_key || "meta" + serializer.meta_key || 'meta' end def root diff --git a/lib/active_model/serializer/lint.rb b/lib/active_model/serializer/lint.rb index bf3b7a371..811085f7e 100644 --- a/lib/active_model/serializer/lint.rb +++ b/lib/active_model/serializer/lint.rb @@ -23,7 +23,7 @@ module Tests # serializable_hash returns a hash representation of a object's attributes. # Typically, it is implemented by including ActiveModel::Serialization. def test_serializable_hash - assert_respond_to resource, :serializable_hash, "The resource should respond to serializable_hash" + assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash' resource.serializable_hash resource.serializable_hash(nil) end @@ -35,7 +35,7 @@ def test_serializable_hash # read_attribute_for_serialization gets the attribute value for serialization # Typically, it is implemented by including ActiveModel::Serialization. def test_read_attribute_for_serialization - assert_respond_to resource, :read_attribute_for_serialization, "The resource should respond to read_attribute_for_serialization" + assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization' actual_arity = resource.method(:read_attribute_for_serialization).arity if defined?(::Rubinius) # 1 for def read_attribute_for_serialization(name); end diff --git a/lib/active_model/serializer/version.rb b/lib/active_model/serializer/version.rb index 3e2ca6bc8..143d55daf 100644 --- a/lib/active_model/serializer/version.rb +++ b/lib/active_model/serializer/version.rb @@ -1,5 +1,5 @@ module ActiveModel class Serializer - VERSION = "0.10.0.rc2" + VERSION = '0.10.0.rc2' end end diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index 77ed442e9..7a65fe773 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -1,12 +1,12 @@ module Rails module Generators class SerializerGenerator < NamedBase - source_root File.expand_path("../templates", __FILE__) - check_class_collision :suffix => "Serializer" + source_root File.expand_path('../templates', __FILE__) + check_class_collision :suffix => 'Serializer' - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type' - class_option :parent, :type => :string, :desc => "The parent class for the generated serializer" + class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer' def create_serializer_file template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") @@ -26,9 +26,9 @@ def parent_class_name if options[:parent] options[:parent] elsif defined?(::ApplicationSerializer) - "ApplicationSerializer" + 'ApplicationSerializer' else - "ActiveModel::Serializer" + 'ActiveModel::Serializer' end end end diff --git a/test/action_controller/adapter_selector_test.rb b/test/action_controller/adapter_selector_test.rb index 88c0ce6b4..bb9a4c9eb 100644 --- a/test/action_controller/adapter_selector_test.rb +++ b/test/action_controller/adapter_selector_test.rb @@ -33,10 +33,10 @@ def test_render_using_adapter_override expected = { data: { id: assigns(:profile).id.to_s, - type: "profiles", + type: 'profiles', attributes: { - name: "Name 1", - description: "Description 1", + name: 'Name 1', + description: 'Description 1', } } } diff --git a/test/action_controller/explicit_serializer_test.rb b/test/action_controller/explicit_serializer_test.rb index 126db5c7b..1eafca168 100644 --- a/test/action_controller/explicit_serializer_test.rb +++ b/test/action_controller/explicit_serializer_test.rb @@ -100,11 +100,11 @@ def test_render_array_using_explicit_serializer_and_custom_serializers get :render_array_using_explicit_serializer_and_custom_serializers expected = [ - { "title" => "New Post", - "body" => "Body", - "id" => assigns(:post).id, - "comments" => [{"id" => 1}, {"id" => 2}], - "author" => { "id" => assigns(:author).id } + { 'title' => 'New Post', + 'body' => 'Body', + 'id' => assigns(:post).id, + 'comments' => [{'id' => 1}, {'id' => 2}], + 'author' => { 'id' => assigns(:author).id } } ] @@ -116,13 +116,13 @@ def test_render_using_explicit_each_serializer expected = { id: 1337, - name: "Amazing Place", + name: 'Amazing Place', locations: [ { id: 42, - lat: "-23.550520", - lng: "-46.633309", - place: "Nowhere" # is a virtual attribute on LocationSerializer + lat: '-23.550520', + lng: '-46.633309', + place: 'Nowhere' # is a virtual attribute on LocationSerializer } ] } diff --git a/test/action_controller/json_api/linked_test.rb b/test/action_controller/json_api/linked_test.rb index a3422fa58..ce299a6de 100644 --- a/test/action_controller/json_api/linked_test.rb +++ b/test/action_controller/json_api/linked_test.rb @@ -28,10 +28,10 @@ def setup_post @first_comment.author = @author2 @second_comment.post = @post @second_comment.author = nil - @post2 = Post.new(id: 2, title: "Another Post", body: "Body") + @post2 = Post.new(id: 2, title: 'Another Post', body: 'Body') @post2.author = @author @post2.comments = [] - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog @post2.blog = @blog end @@ -100,37 +100,37 @@ def test_render_resource_with_nested_has_many_include response = JSON.parse(@response.body) expected_linked = [ { - "id" => "1", - "type" => "authors", - "attributes" => { - "name" => "Steve K." + 'id' => '1', + 'type' => 'authors', + 'attributes' => { + 'name' => 'Steve K.' }, - "relationships" => { - "posts" => { "data" => [] }, - "roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, - "bio" => { "data" => nil } + 'relationships' => { + 'posts' => { 'data' => [] }, + 'roles' => { 'data' => [{ 'type' =>'roles', 'id' => '1' }, { 'type' =>'roles', 'id' => '2' }] }, + 'bio' => { 'data' => nil } } }, { - "id" => "1", - "type" => "roles", - "attributes" => { - "name" => "admin", - "description" => nil, - "slug" => "admin-1" + 'id' => '1', + 'type' => 'roles', + 'attributes' => { + 'name' => 'admin', + 'description' => nil, + 'slug' => 'admin-1' }, - "relationships" => { - "author" => { "data" => { "type" =>"authors", "id" => "1" } } + 'relationships' => { + 'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } } } }, { - "id" => "2", - "type" => "roles", - "attributes" => { - "name" => "colab", - "description" => nil, - "slug" => "colab-2" + 'id' => '2', + 'type' => 'roles', + 'attributes' => { + 'name' => 'colab', + 'description' => nil, + 'slug' => 'colab-2' }, - "relationships" => { - "author" => { "data" => { "type" =>"authors", "id" => "1" } } + 'relationships' => { + 'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } } } } ] diff --git a/test/action_controller/json_api/pagination_test.rb b/test/action_controller/json_api/pagination_test.rb index 55db95ef2..d78c04451 100644 --- a/test/action_controller/json_api/pagination_test.rb +++ b/test/action_controller/json_api/pagination_test.rb @@ -47,11 +47,11 @@ def render_array_without_pagination_links tests PaginationTestController def test_render_pagination_links_with_will_paginate - expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", - "first"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - "prev"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - "next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - "last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} + expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", + 'first'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} get :render_pagination_using_will_paginate, page: { number: 2, size: 1 } response = JSON.parse(@response.body) @@ -59,48 +59,48 @@ def test_render_pagination_links_with_will_paginate end def test_render_only_last_and_next_pagination_links - expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", - "next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", - "last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"} + expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", + 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", + 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"} get :render_pagination_using_will_paginate, page: { number: 1, size: 2 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_pagination_links_with_kaminari - expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", - "first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - "prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - "next"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - "last"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} + expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", + 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'next'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'last'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} get :render_pagination_using_kaminari, page: { number: 2, size: 1 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_prev_and_first_pagination_links - expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - "first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - "prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"} + expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"} get :render_pagination_using_kaminari, page: { number: 3, size: 1 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_last_and_next_pagination_links_with_additional_params - expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", - "next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", - "last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"} - get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: "additional" + expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", + 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", + 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"} + get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional' response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_prev_and_first_pagination_links_with_additional_params - expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", - "first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", - "prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"} - get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: "additional" + expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", + 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", + 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"} + get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional' response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index 733500912..e8bd77df2 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -21,20 +21,20 @@ def render_using_default_adapter_root def render_array_using_custom_root with_adapter ActiveModel::Serializer::Adapter::Json do @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: [@profile], root: "custom_root" + render json: [@profile], root: 'custom_root' end end def render_array_that_is_empty_using_custom_root with_adapter ActiveModel::Serializer::Adapter::Json do - render json: [], root: "custom_root" + render json: [], root: 'custom_root' end end def render_object_using_custom_root with_adapter ActiveModel::Serializer::Adapter::Json do @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile, root: "custom_root" + render json: @profile, root: 'custom_root' end end @@ -159,8 +159,8 @@ def test_render_using_implicit_serializer get :render_using_implicit_serializer expected = { - name: "Name 1", - description: "Description 1" + name: 'Name 1', + description: 'Description 1' } assert_equal 'application/json', @response.content_type @@ -173,10 +173,10 @@ def test_render_using_default_root expected = { data: { id: assigns(:profile).id.to_s, - type: "profiles", + type: 'profiles', attributes: { - name: "Name 1", - description: "Description 1" + name: 'Name 1', + description: 'Description 1' } } } @@ -188,7 +188,7 @@ def test_render_using_default_root def test_render_array_using_custom_root get :render_array_using_custom_root - expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]} + expected = {custom_roots: [{name: 'Name 1', description: 'Description 1'}]} assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -250,10 +250,10 @@ def test_render_array_using_implicit_serializer_and_meta data: [ { id: assigns(:profiles).first.id.to_s, - type: "profiles", + type: 'profiles', attributes: { - name: "Name 1", - description: "Description 1" + name: 'Name 1', + description: 'Description 1' } } ], @@ -335,8 +335,8 @@ def test_render_with_fragment_only_cache_enable response = JSON.parse(@response.body) assert_equal 'application/json', @response.content_type - assert_equal 'ZOMG A ROLE', response["name"] - assert_equal 'HUEHUEBRBR', response["description"] + assert_equal 'ZOMG A ROLE', response['name'] + assert_equal 'HUEHUEBRBR', response['description'] end def test_render_with_fragment_except_cache_enable @@ -345,8 +345,8 @@ def test_render_with_fragment_except_cache_enable response = JSON.parse(@response.body) assert_equal 'application/json', @response.content_type - assert_equal 5, response["rating"] - assert_equal 'lol', response["content"] + assert_equal 5, response['rating'] + assert_equal 'lol', response['content'] end def test_render_fragment_changed_object_with_relationship @@ -357,11 +357,11 @@ def test_render_fragment_changed_object_with_relationship response = JSON.parse(@response.body) expected_return = { - "id"=>1, - "time"=>Time.now.to_s, - "likeable" => { - "id"=>1, - "body"=>"ZOMG A COMMENT" + 'id'=>1, + 'time'=>Time.now.to_s, + 'likeable' => { + 'id'=>1, + 'body'=>'ZOMG A COMMENT' } } @@ -385,7 +385,7 @@ def test_cache_expiration_on_update ], blog: { id:999, - name: "Custom blog" + name: 'Custom blog' }, author: { id: 1, @@ -416,7 +416,7 @@ def use_adapter? true end }.new - assert_equal "", (capture(:stderr) { + assert_equal '', (capture(:stderr) { controller.get_serializer(Profile.new) }) end diff --git a/test/adapter/fragment_cache_test.rb b/test/adapter/fragment_cache_test.rb index e2e4e2f4c..aa39cd523 100644 --- a/test/adapter/fragment_cache_test.rb +++ b/test/adapter/fragment_cache_test.rb @@ -4,7 +4,7 @@ class Serializer class Adapter class FragmentCacheTest < Minitest::Test def setup - @spam = Spam::UnrelatedLink.new(id: "spam-id-1") + @spam = Spam::UnrelatedLink.new(id: 'spam-id-1') @author = Author.new(name: 'Joao M. D. Moura') @role = Role.new(name: 'Great Author', description:nil) @role.author = [@author] diff --git a/test/adapter/json/belongs_to_test.rb b/test/adapter/json/belongs_to_test.rb index f39080fd5..31b663bcb 100644 --- a/test/adapter/json/belongs_to_test.rb +++ b/test/adapter/json/belongs_to_test.rb @@ -14,7 +14,7 @@ def setup @comment.post = @post @comment.author = nil @anonymous_post.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog @anonymous_post.blog = nil @@ -31,14 +31,14 @@ def test_include_nil_author serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash) + assert_equal({post: {title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: {id: 999, name: 'Custom blog'}, author: nil}}, adapter.serializable_hash) end def test_include_nil_author_with_specified_serializer serializer = PostPreviewSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash) + assert_equal({post: {title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil}}, adapter.serializable_hash) end end end diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb index 295a0e3f4..900256dda 100644 --- a/test/adapter/json/collection_test.rb +++ b/test/adapter/json/collection_test.rb @@ -13,7 +13,7 @@ def setup @second_post.comments = [] @first_post.author = @author @second_post.author = @author - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @first_post.blog = @blog @second_post.blog = nil @@ -21,15 +21,15 @@ def setup end def test_with_serializer_option - @blog.special_attribute = "Special" + @blog.special_attribute = 'Special' @blog.articles = [@first_post, @second_post] serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = {blogs:[{ id: 1, - special_attribute: "Special", - articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}] + special_attribute: 'Special', + articles: [{id: 1,title: 'Hello!!', body: 'Hello, world!!'}, {id: 2, title: 'New Post', body: 'Body'}] }]} assert_equal expected, adapter.serializable_hash end @@ -39,30 +39,30 @@ def test_include_multiple_posts adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { posts: [{ - title: "Hello!!", - body: "Hello, world!!", + title: 'Hello!!', + body: 'Hello, world!!', id: 1, comments: [], author: { id: 1, - name: "Steve K." + name: 'Steve K.' }, blog: { id: 999, - name: "Custom blog" + name: 'Custom blog' } }, { - title: "New Post", - body: "Body", + title: 'New Post', + body: 'Body', id: 2, comments: [], author: { id: 1, - name: "Steve K." + name: 'Steve K.' }, blog: { id: 999, - name: "Custom blog" + name: 'Custom blog' } }]} assert_equal expected, adapter.serializable_hash diff --git a/test/adapter/json/has_many_test.rb b/test/adapter/json/has_many_test.rb index 14e27fc3b..00df72324 100644 --- a/test/adapter/json/has_many_test.rb +++ b/test/adapter/json/has_many_test.rb @@ -15,9 +15,9 @@ def setup @post.author = @author @first_comment.post = @post @second_comment.post = @post - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog - @tag = Tag.new(id: 1, name: "#hash_tag") + @tag = Tag.new(id: 1, name: '#hash_tag') @post.tags = [@tag] end @@ -36,7 +36,7 @@ def test_has_many_with_no_serializer assert_equal({ id: 42, tags: [ - {"attributes"=>{"id"=>1, "name"=>"#hash_tag"}} + {'attributes'=>{'id'=>1, 'name'=>'#hash_tag'}} ] }.to_json, adapter.serializable_hash[:post].to_json) end diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 967b53e9a..382c92901 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -21,7 +21,7 @@ def setup @comment.author = nil @post.author = @author @anonymous_post.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @blog.writer = @author @blog.articles = [@post, @anonymous_post] @author.posts = [] @@ -32,7 +32,7 @@ def setup end def test_includes_post_id - expected = { data: { type: "posts", id: "42" } } + expected = { data: { type: 'posts', id: '42' } } assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post]) end @@ -40,16 +40,16 @@ def test_includes_post_id def test_includes_linked_post @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post') expected = [{ - id: "42", - type: "posts", + id: '42', + type: 'posts', attributes: { title: 'New Post', body: 'Body', }, relationships: { - comments: { data: [ { type: "comments", id: "1" } ] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + comments: { data: [ { type: 'comments', id: '1' } ] }, + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }] assert_equal expected, @adapter.serializable_hash[:included] @@ -58,15 +58,15 @@ def test_includes_linked_post def test_limiting_linked_post_fields @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]}) expected = [{ - id: "42", - type: "posts", + id: '42', + type: 'posts', attributes: { title: 'New Post' }, relationships: { - comments: { data: [ { type: "comments", id: "1" } ] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + comments: { data: [ { type: 'comments', id: '1' } ] }, + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }] assert_equal expected, @adapter.serializable_hash[:included] @@ -76,7 +76,7 @@ def test_include_nil_author serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships]) + assert_equal({comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships]) end def test_include_type_for_association_when_different_than_name @@ -86,19 +86,19 @@ def test_include_type_for_association_when_different_than_name expected = { writer: { data: { - type: "authors", - id: "1" + type: 'authors', + id: '1' } }, articles: { data: [ { - type: "posts", - id: "42" + type: 'posts', + id: '42' }, { - type: "posts", - id: "43" + type: 'posts', + id: '43' } ] } @@ -112,10 +112,10 @@ def test_include_linked_resources_with_type_name linked = adapter.serializable_hash[:included] expected = [ { - id: "1", - type: "authors", + id: '1', + type: 'authors', attributes: { - name: "Steve K." + name: 'Steve K.' }, relationships: { posts: { data: [] }, @@ -123,27 +123,27 @@ def test_include_linked_resources_with_type_name bio: { data: nil } } },{ - id: "42", - type: "posts", + id: '42', + type: 'posts', attributes: { - title: "New Post", - body: "Body" + title: 'New Post', + body: 'Body' }, relationships: { - comments: { data: [ { type: "comments", id: "1" } ] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + comments: { data: [ { type: 'comments', id: '1' } ] }, + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }, { - id: "43", - type: "posts", + id: '43', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, + blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } } } diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index f0d4eff1d..db72edb67 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -27,29 +27,29 @@ def setup def test_include_multiple_posts expected = [ { - id: "1", - type: "posts", + id: '1', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }, { - id: "2", - type: "posts", + id: '2', + type: 'posts', attributes: { - title: "New Post", - body: "Body" + title: 'New Post', + body: 'Body' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } } ] @@ -62,27 +62,27 @@ def test_limiting_fields expected = [ { - id: "1", - type: "posts", + id: '1', + type: 'posts', attributes: { - title: "Hello!!" + title: 'Hello!!' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }, { - id: "2", - type: "posts", + id: '2', + type: 'posts', attributes: { - title: "New Post" + title: 'New Post' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } } ] diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 7dd132c7c..2adaa88e3 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -27,8 +27,8 @@ def setup def test_includes_comment_ids expected = { data: [ - { type: "posts", id: "1"}, - { type: "posts", id: "2"} + { type: 'posts', id: '1'}, + { type: 'posts', id: '2'} ] } diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index 2adb0eb53..aa8a926ed 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -58,9 +58,9 @@ def test_includes_linked_data }, { id: @author.id.to_s, - type: "authors", + type: 'authors', relationships: { - posts: { data: [ {type: "posts", id: @post.id.to_s } ] } + posts: { data: [ {type: 'posts', id: @post.id.to_s } ] } } } ] @@ -70,7 +70,7 @@ def test_includes_linked_data def test_includes_author_id expected = { - data: { type: "authors", id: @author.id.to_s } + data: { type: 'authors', id: @author.id.to_s } } assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author]) diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 5b14dda7e..5c2824434 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -22,12 +22,12 @@ def setup @second_comment.post = @post @post.author = @author @post_without_comments.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @blog.writer = @author @blog.articles = [@post] @post.blog = @blog @post_without_comments.blog = nil - @tag = Tag.new(id: 1, name: "#hash_tag") + @tag = Tag.new(id: 1, name: '#hash_tag') @post.tags = [@tag] @serializer = PostSerializer.new(@post) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) @@ -36,7 +36,7 @@ def setup end def test_includes_comment_ids - expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] } + expected = { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] } assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments]) end @@ -44,23 +44,23 @@ def test_includes_comment_ids def test_includes_linked_comments @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments') expected = [{ - id: "1", - type: "comments", + id: '1', + type: 'comments', attributes: { body: 'ZOMG A COMMENT' }, relationships: { - post: { data: { type: "posts", id: "1" } }, + post: { data: { type: 'posts', id: '1' } }, author: { data: nil } } }, { - id: "2", - type: "comments", + id: '2', + type: 'comments', attributes: { body: 'ZOMG ANOTHER COMMENT' }, relationships: { - post: { data: { type: "posts", id: "1" } }, + post: { data: { type: 'posts', id: '1' } }, author: { data: nil } } }] @@ -70,17 +70,17 @@ def test_includes_linked_comments def test_limit_fields_of_linked_comments @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]}) expected = [{ - id: "1", - type: "comments", + id: '1', + type: 'comments', relationships: { - post: { data: { type: "posts", id: "1" } }, + post: { data: { type: 'posts', id: '1' } }, author: { data: nil } } }, { - id: "2", - type: "comments", + id: '2', + type: 'comments', relationships: { - post: { data: { type: "posts", id: "1" } }, + post: { data: { type: 'posts', id: '1' } }, author: { data: nil } } }] @@ -101,8 +101,8 @@ def test_include_type_for_association_when_different_than_name expected = { data: [{ - type: "posts", - id: "1" + type: 'posts', + id: '1' }] } assert_equal expected, actual @@ -114,8 +114,8 @@ def test_has_many_with_no_serializer assert_equal({ data: { - id: "1", - type: "posts", + id: '1', + type: 'posts', relationships: { tags: { data: [@tag.as_json]} } @@ -129,8 +129,8 @@ def test_has_many_with_virtual_value assert_equal({ data: { - id: "1", - type: "virtual_values", + id: '1', + type: 'virtual_values', relationships: { maker: {data: {id: 1}}, reviews: {data: [{id: 1}, {id: 2}]} diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index f71977cf5..baf0c4e63 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -19,7 +19,7 @@ def setup @comment.author = nil @post.author = @author @anonymous_post.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @blog.writer = @author @blog.articles = [@post, @anonymous_post] @author.posts = [] @@ -32,7 +32,7 @@ def setup end def test_includes_bio_id - expected = { data: { type: "bios", id: "43" } } + expected = { data: { type: 'bios', id: '43' } } assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio]) end @@ -42,14 +42,14 @@ def test_includes_linked_bio expected = [ { - id: "43", - type: "bios", + id: '43', + type: 'bios', attributes: { - content:"AMS Contributor", + content:'AMS Contributor', rating: nil }, relationships: { - author: { data: { type: "authors", id: "1" } } + author: { data: { type: 'authors', id: '1' } } } } ] @@ -63,8 +63,8 @@ def test_has_one_with_virtual_value expected = { data: { - id: "1", - type: "virtual_values", + id: '1', + type: 'virtual_values', relationships: { maker: {data: {id: 1}}, reviews: {data: [{id: 1}, {id: 2}]} diff --git a/test/adapter/json_api/json_api_test.rb b/test/adapter/json_api/json_api_test.rb index 5440811a7..6867aab0a 100644 --- a/test/adapter/json_api/json_api_test.rb +++ b/test/adapter/json_api/json_api_test.rb @@ -14,7 +14,7 @@ def setup @first_comment.post = @post @second_comment.post = @post @post.author = @author - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog end @@ -25,11 +25,11 @@ def test_custom_keys assert_equal({ reviews: { data: [ - {type: "comments", id: "1"}, - {type: "comments", id: "2"} + {type: 'comments', id: '1'}, + {type: 'comments', id: '2'} ]}, - writer: { data: {type: "authors", id: "1"} }, - site: { data: {type: "blogs", id: "1" } } + writer: { data: {type: 'authors', id: '1'} }, + site: { data: {type: 'blogs', id: '1' } } }, adapter.serializable_hash[:data][:relationships]) end end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index da0389597..6d49cc8f7 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -53,94 +53,94 @@ def test_include_multiple_posts_and_linked_array expected = { data: [ { - id: "10", - type: "posts", + id: '10', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + comments: { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] }, + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }, { - id: "20", - type: "posts", + id: '20', + type: 'posts', attributes: { - title: "New Post", - body: "Body" + title: 'New Post', + body: 'Body' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "2" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '2' } } } } ], included: [ { - id: "1", - type: "comments", + id: '1', + type: 'comments', attributes: { - body: "ZOMG A COMMENT" + body: 'ZOMG A COMMENT' }, relationships: { - post: { data: { type: "posts", id: "10" } }, + post: { data: { type: 'posts', id: '10' } }, author: { data: nil } } }, { - id: "2", - type: "comments", + id: '2', + type: 'comments', attributes: { - body: "ZOMG ANOTHER COMMENT", + body: 'ZOMG ANOTHER COMMENT', }, relationships: { - post: { data: { type: "posts", id: "10" } }, + post: { data: { type: 'posts', id: '10' } }, author: { data: nil } } }, { - id: "1", - type: "authors", + id: '1', + type: 'authors', attributes: { - name: "Steve K." + name: 'Steve K.' }, relationships: { - posts: { data: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] }, + posts: { data: [ { type: 'posts', id: '10' }, { type: 'posts', id: '30' } ] }, roles: { data: [] }, - bio: { data: { type: "bios", id: "1" } } + bio: { data: { type: 'bios', id: '1' } } } }, { - id: "1", - type: "bios", + id: '1', + type: 'bios', attributes: { - content: "AMS Contributor", + content: 'AMS Contributor', rating: nil }, relationships: { - author: { data: { type: "authors", id: "1" } } + author: { data: { type: 'authors', id: '1' } } } }, { - id: "2", - type: "authors", + id: '2', + type: 'authors', attributes: { - name: "Tenderlove" + name: 'Tenderlove' }, relationships: { - posts: { data: [ { type: "posts", id:"20" } ] }, + posts: { data: [ { type: 'posts', id:'20' } ] }, roles: { data: [] }, - bio: { data: { type: "bios", id: "2" } } + bio: { data: { type: 'bios', id: '2' } } } }, { - id: "2", - type: "bios", + id: '2', + type: 'bios', attributes: { rating: nil, - content: "Rails Contributor", + content: 'Rails Contributor', }, relationships: { - author: { data: { type: "authors", id: "2" } } + author: { data: { type: 'authors', id: '2' } } } } ] @@ -162,39 +162,39 @@ def test_include_multiple_posts_and_linked expected = [ { - id: "1", - type: "authors", + id: '1', + type: 'authors', attributes: { - name: "Steve K." + name: 'Steve K.' }, relationships: { - posts: { data: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, + posts: { data: [ { type: 'posts', id: '10'}, { type: 'posts', id: '30' }] }, roles: { data: [] }, - bio: { data: { type: "bios", id: "1" }} + bio: { data: { type: 'bios', id: '1' }} } }, { - id: "10", - type: "posts", + id: '10', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + comments: { data: [ { type: 'comments', id: '1'}, { type: 'comments', id: '2' }] }, + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } }, { - id: "30", - type: "posts", + id: '30', + type: 'posts', attributes: { - title: "Yet Another Post", - body: "Body" + title: 'Yet Another Post', + body: 'Body' }, relationships: { comments: { data: [] }, - blog: { data: { type: "blogs", id: "999" } }, - author: { data: { type: "authors", id: "1" } } + blog: { data: { type: 'blogs', id: '999' } }, + author: { data: { type: 'authors', id: '1' } } } } ] @@ -229,21 +229,21 @@ def test_multiple_references_to_same_resource expected = [ { - id: "10", - type: "posts", + id: '10', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { comments: { - data: [{type: "comments", id: "1"}, {type: "comments", id: "2"}] + data: [{type: 'comments', id: '1'}, {type: 'comments', id: '2'}] }, blog: { - data: {type: "blogs", id: "999"} + data: {type: 'blogs', id: '999'} }, author: { - data: {type: "authors", id: "1"} + data: {type: 'authors', id: '1'} } } } @@ -262,14 +262,14 @@ def test_nil_link_with_specified_serializer expected = { data: { - id: "10", - type: "posts", + id: '10', + type: 'posts', attributes: { - title: "Hello!!", - body: "Hello, world!!" + title: 'Hello!!', + body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, + comments: { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] }, author: { data: nil } } } diff --git a/test/adapter/json_api/pagination_links_test.rb b/test/adapter/json_api/pagination_links_test.rb index 58a291205..1a7410451 100644 --- a/test/adapter/json_api/pagination_links_test.rb +++ b/test/adapter/json_api/pagination_links_test.rb @@ -43,9 +43,9 @@ def using_will_paginate def 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" } } + { 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' } } ] } end diff --git a/test/adapter/json_api/resource_type_config_test.rb b/test/adapter/json_api/resource_type_config_test.rb index e389b9b4f..3483915c2 100644 --- a/test/adapter/json_api/resource_type_config_test.rb +++ b/test/adapter/json_api/resource_type_config_test.rb @@ -21,7 +21,7 @@ def setup @comment.author = nil @post.author = @author @anonymous_post.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @blog.writer = @author @blog.articles = [@post, @anonymous_post] @author.posts = [] diff --git a/test/adapter/json_test.rb b/test/adapter/json_test.rb index 4acf0dbbb..e7beffd78 100644 --- a/test/adapter/json_test.rb +++ b/test/adapter/json_test.rb @@ -14,7 +14,7 @@ def setup @first_comment.post = @post @second_comment.post = @post @post.author = @author - @blog = Blog.new(id: 1, name: "My Blog!!") + @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog @serializer = PostSerializer.new(@post) @@ -34,11 +34,11 @@ def test_custom_keys 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!!"} + writer: {id: 1, name: 'Steve K.'}, + site: {id: 1, name: 'My Blog!!'} }, adapter.serializable_hash[:post]) end end diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb index 4600fd76a..834dea54d 100644 --- a/test/array_serializer_test.rb +++ b/test/array_serializer_test.rb @@ -42,11 +42,11 @@ def test_serializer_option_not_passed_to_each_serializer end def test_meta_and_meta_key_attr_readers - meta_content = {meta: "the meta", meta_key: "the meta key"} + meta_content = {meta: 'the meta', meta_key: 'the meta key'} @serializer = ArraySerializer.new([@comment, @post], meta_content) - assert_equal @serializer.meta, "the meta" - assert_equal @serializer.meta_key, "the meta key" + assert_equal @serializer.meta, 'the meta' + assert_equal @serializer.meta_key, 'the meta key' end def test_root_default diff --git a/test/capture_warnings.rb b/test/capture_warnings.rb index 76e77b7bb..9eb8bf77d 100644 --- a/test/capture_warnings.rb +++ b/test/capture_warnings.rb @@ -1,15 +1,15 @@ # https://raw.githubusercontent.com/metric_fu/metric_fu/master/spec/capture_warnings.rb -require "tempfile" -require "fileutils" +require 'tempfile' +require 'fileutils' class CaptureWarnings def initialize(fail_on_warnings = true) @fail_on_warnings = fail_on_warnings - @stderr_file = Tempfile.new("app.stderr") + @stderr_file = Tempfile.new('app.stderr') @app_root ||= Dir.pwd - @output_dir = File.join(app_root, "tmp") + @output_dir = File.join(app_root, 'tmp') FileUtils.mkdir_p(output_dir) - @bundle_dir = File.join(app_root, "bundle") + @bundle_dir = File.join(app_root, 'bundle') end def before_tests @@ -40,9 +40,9 @@ def after_tests end if other_warnings.any? - File.write(File.join(output_dir, "warnings.txt"), other_warnings.join("\n") << "\n") + File.write(File.join(output_dir, 'warnings.txt'), other_warnings.join("\n") << "\n") puts - puts "Non-app warnings written to tmp/warnings.txt" + puts 'Non-app warnings written to tmp/warnings.txt' puts end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index fa871d9ca..b25ea7114 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -103,7 +103,7 @@ module Spam; end url :comments def blog - Blog.new(id: 999, name: "Custom blog") + Blog.new(id: 999, name: 'Custom blog') end def custom_options diff --git a/test/generators/serializer_generator_test.rb b/test/generators/serializer_generator_test.rb index 7af39ed12..5395dff66 100644 --- a/test/generators/serializer_generator_test.rb +++ b/test/generators/serializer_generator_test.rb @@ -2,7 +2,7 @@ require 'generators/serializer/serializer_generator' class SerializerGeneratorTest < Rails::Generators::TestCase - destination File.expand_path("../../../tmp/generators", __FILE__) + destination File.expand_path('../../../tmp/generators', __FILE__) setup :prepare_destination tests Rails::Generators::SerializerGenerator @@ -10,33 +10,33 @@ class SerializerGeneratorTest < Rails::Generators::TestCase def test_generates_a_serializer run_generator - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/ + assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ end def test_generates_a_namespaced_serializer - run_generator ["admin/account"] - assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/ + run_generator ['admin/account'] + assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/ end def test_uses_application_serializer_if_one_exists Object.const_set(:ApplicationSerializer, Class.new) run_generator - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/ + assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/ ensure Object.send :remove_const, :ApplicationSerializer end def test_uses_given_parent Object.const_set(:ApplicationSerializer, Class.new) - run_generator ["Account", "--parent=MySerializer"] - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/ + run_generator ['Account', '--parent=MySerializer'] + assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/ ensure Object.send :remove_const, :ApplicationSerializer end def test_generates_attributes_and_associations run_generator - assert_file "app/serializers/account_serializer.rb" do |serializer| + assert_file 'app/serializers/account_serializer.rb' do |serializer| assert_match(/^ attributes :id, :name, :description$/, serializer) assert_match(/^ has_one :business$/, serializer) assert_match(/^end\n*\z/, serializer) @@ -44,8 +44,8 @@ def test_generates_attributes_and_associations end def test_with_no_attributes_does_not_add_extra_space - run_generator ["account"] - assert_file "app/serializers/account_serializer.rb" do |content| + run_generator ['account'] + assert_file 'app/serializers/account_serializer.rb' do |content| if RUBY_PLATFORM =~ /mingw/ assert_no_match(/\r\n\r\nend/, content) else diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index bfc1b40cd..570f92054 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -74,7 +74,7 @@ def test_has_many_with_no_serializer assert_equal key, :tags assert_equal serializer, nil - assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json + assert_equal [{ attributes: { name: '#hashtagged' }}].to_json, options[:virtual_value].to_json end end diff --git a/test/serializers/attribute_test.rb b/test/serializers/attribute_test.rb index 9399b935b..ab19a11fd 100644 --- a/test/serializers/attribute_test.rb +++ b/test/serializers/attribute_test.rb @@ -4,7 +4,7 @@ module ActiveModel class Serializer class AttributeTest < Minitest::Test def setup - @blog = Blog.new({ id: 1, name: 'AMS Hints', type: "stuff" }) + @blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' }) @blog_serializer = AlternateBlogSerializer.new(@blog) end @@ -15,14 +15,14 @@ def test_attributes_definition def test_json_serializable_hash adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer) - assert_equal({blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash) + assert_equal({blog: { id:1, title:'AMS Hints'}}, adapter.serializable_hash) end def test_attribute_inheritance_with_key inherited_klass = Class.new(AlternateBlogSerializer) blog_serializer = inherited_klass.new(@blog) adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer) - assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash) + assert_equal({:id=>1, :title=>'AMS Hints'}, adapter.serializable_hash) end def test_multiple_calls_with_the_same_attribute @@ -40,7 +40,7 @@ def test_id_attribute_override end adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog)) - assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash) + assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash) end def test_type_attribute @@ -55,7 +55,7 @@ def test_type_attribute assert_equal({ blog: { type: 1} }, adapter.serializable_hash) adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog)) - assert_equal({ blog: { type: "stuff" } }, adapter.serializable_hash) + assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash) end end end diff --git a/test/serializers/attributes_test.rb b/test/serializers/attributes_test.rb index 8b039df91..4ce85f120 100644 --- a/test/serializers/attributes_test.rb +++ b/test/serializers/attributes_test.rb @@ -6,7 +6,7 @@ class AttributesTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile_serializer = ProfileSerializer.new(@profile) - @comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015") + @comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015') @serializer_klass = Class.new(CommentSerializer) @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do attributes :date, :likes @@ -35,7 +35,7 @@ def test_attributes_inheritance_definition def test_attributes_inheritance serializer = @serializer_klass.new(@comment) - assert_equal({id: 1, body: "ZOMG!!"}, + assert_equal({id: 1, body: 'ZOMG!!'}, serializer.attributes) end @@ -46,7 +46,7 @@ def test_attribute_inheritance_with_new_attribute_definition def test_attribute_inheritance_with_new_attribute serializer = @serializer_klass_with_new_attributes.new(@comment) - assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil}, + assert_equal({id: 1, body: 'ZOMG!!', date: '2015', likes: nil}, serializer.attributes) end diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index afa583270..703316fbf 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -9,7 +9,7 @@ def setup @post = Post.new(title: 'New Post', body: 'Body') @bio = Bio.new(id: 1, content: 'AMS Contributor') @author = Author.new(name: 'Joao M. D. Moura') - @blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: []) + @blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: []) @role = Role.new(name: 'Great Author') @location = Location.new(lat: '-23.550520', lng: '-46.633309') @place = Place.new(name: 'Amazing Place') @@ -131,20 +131,20 @@ def test_cache_digest_definition end def test_serializer_file_path_on_nix - path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb" + path = '/Users/git/emberjs/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_on_windows - path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb" + path = 'c:/git/emberjs/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_digest_caller_file contents = "puts 'AMS rocks'!" - file = Tempfile.new("some_ruby.rb") + file = Tempfile.new('some_ruby.rb') file.write(contents) path = file.path caller_line = "#{path}:1:in `'" diff --git a/test/serializers/meta_test.rb b/test/serializers/meta_test.rb index 60e173848..833e891dd 100644 --- a/test/serializers/meta_test.rb +++ b/test/serializers/meta_test.rb @@ -7,8 +7,8 @@ def setup ActionController::Base.cache_store.clear @blog = Blog.new(id: 1, name: 'AMS Hints', - writer: Author.new(id: 2, name: "Steve"), - articles: [Post.new(id: 3, title: "AMS")]) + writer: Author.new(id: 2, name: 'Steve'), + articles: [Post.new(id: 3, title: 'AMS')]) end def test_meta_is_present_with_root @@ -17,9 +17,9 @@ def test_meta_is_present_with_root expected = { blog: { id: 1, - title: "AMS Hints" + title: 'AMS Hints' }, - "meta" => { + 'meta' => { total: 10 } } @@ -31,20 +31,20 @@ def test_meta_is_not_included_when_root_is_missing adapter = load_adapter(meta: {total: 10}) expected = { id: 1, - title: "AMS Hints" + title: 'AMS Hints' } assert_equal expected, adapter.as_json end def test_meta_key_is_used - serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta") + serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: 'haha_meta') adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { blog: { id: 1, - title: "AMS Hints" + title: 'AMS Hints' }, - "haha_meta" => { + 'haha_meta' => { total: 10 } } @@ -52,15 +52,15 @@ def test_meta_key_is_used end def test_meta_key_is_used_with_json_api - serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta") + serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: 'haha_meta') adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) expected = { data: { - id: "1", - type: "blogs", - attributes: { title: "AMS Hints" } + id: '1', + type: 'blogs', + attributes: { title: 'AMS Hints' } }, - "haha_meta" => { total: 10 } + 'haha_meta' => { total: 10 } } assert_equal expected, adapter.as_json end @@ -71,14 +71,14 @@ def test_meta_is_not_present_on_arrays_without_root adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer) expected = [{ id: 1, - name: "AMS Hints", + name: 'AMS Hints', writer: { id: 2, - name: "Steve" + name: 'Steve' }, articles: [{ id: 3, - title: "AMS", + title: 'AMS', body: nil }] }] @@ -86,20 +86,20 @@ def test_meta_is_not_present_on_arrays_without_root end def test_meta_is_present_on_arrays_with_root - serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta") + serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: 'haha_meta') # JSON adapter adds root by default adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { blogs: [{ id: 1, - name: "AMS Hints", + name: 'AMS Hints', writer: { id: 2, - name: "Steve" + name: 'Steve' }, articles: [{ id: 3, - title: "AMS", + title: 'AMS', body: nil }] }], diff --git a/test/support/stream_capture.rb b/test/support/stream_capture.rb index 20affb713..2d7dfea9e 100644 --- a/test/support/stream_capture.rb +++ b/test/support/stream_capture.rb @@ -1,7 +1,7 @@ # Use cleaner stream testing interface from Rails 5 if available # see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb begin - require "active_support/testing/stream" + require 'active_support/testing/stream' rescue LoadError module ActiveSupport module Testing diff --git a/test/test_helper.rb b/test/test_helper.rb index ce5164c32..87ae02866 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,7 +23,7 @@ end else at_exit do - STDOUT.puts "Minitest.after_run not available." + STDOUT.puts 'Minitest.after_run not available.' @capture_warnings.after_tests end end From 228cc1c92a90d369e654477e00242a416ee8c78d Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Mon, 31 Aug 2015 04:22:59 -0500 Subject: [PATCH 3/6] Rubocop: Consistent spacing --- .rubocop_todo.yml | 278 ++++++++---------- active_model_serializers.gemspec | 4 +- lib/action_controller/serialization.rb | 2 +- lib/active_model/serializable_resource.rb | 2 - lib/active_model/serializer.rb | 1 - .../serializer/adapter/fragment_cache.rb | 7 +- lib/active_model/serializer/adapter/json.rb | 1 - .../serializer/adapter/json/fragment_cache.rb | 2 - .../serializer/adapter/json_api.rb | 2 +- .../adapter/json_api/fragment_cache.rb | 6 +- lib/active_model/serializer/association.rb | 1 - lib/active_model/serializer/fieldset.rb | 6 +- lib/active_model/serializer/lint.rb | 2 - lib/active_model_serializers.rb | 1 - .../explicit_serializer_test.rb | 2 +- .../action_controller/json_api/linked_test.rb | 10 +- .../json_api/pagination_test.rb | 44 +-- test/action_controller/serialization_test.rb | 26 +- test/adapter/fragment_cache_test.rb | 2 +- test/adapter/json/belongs_to_test.rb | 6 +- test/adapter/json/collection_test.rb | 8 +- test/adapter/json/has_many_test.rb | 6 +- test/adapter/json_api/belongs_to_test.rb | 14 +- test/adapter/json_api/collection_test.rb | 1 - .../json_api/has_many_embed_ids_test.rb | 4 +- .../has_many_explicit_serializer_test.rb | 4 +- test/adapter/json_api/has_many_test.rb | 10 +- test/adapter/json_api/has_one_test.rb | 6 +- test/adapter/json_api/json_api_test.rb | 11 +- test/adapter/json_api/linked_test.rb | 20 +- .../adapter/json_api/pagination_links_test.rb | 16 +- test/adapter/json_test.rb | 12 +- test/adapter_test.rb | 4 +- test/array_serializer_test.rb | 10 +- test/capture_warnings.rb | 1 + test/fixtures/poro.rb | 10 +- test/lint_test.rb | 7 - test/serializers/adapter_for_test.rb | 1 - test/serializers/associations_test.rb | 8 +- test/serializers/attribute_test.rb | 8 +- test/serializers/attributes_test.rb | 19 +- test/serializers/cache_test.rb | 7 +- test/serializers/fieldset_test.rb | 7 +- test/serializers/meta_test.rb | 12 +- test/serializers/root_test.rb | 4 +- test/serializers/urls_test.rb | 1 - test/test_helper.rb | 1 - 47 files changed, 278 insertions(+), 339 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4efa90623..36ce4dfce 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,5 +1,6 @@ -# This configuration was generated by `rubocop --auto-gen-config` -# on 2015-08-30 23:03:50 -0500 using RuboCop version 0.31.0. +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2015-08-31 04:23:33 -0500 using RuboCop version 0.33.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -8,67 +9,74 @@ # Offense count: 1 # Configuration parameters: AllowSafeAssignment. Lint/AssignmentInCondition: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter/json_api.rb' # Offense count: 1 Lint/EmptyEnsure: - Enabled: false + Exclude: + - 'test/serializers/adapter_for_test.rb' # Offense count: 1 Lint/HandleExceptions: - Enabled: false + Exclude: + - 'Rakefile' # Offense count: 2 # Cop supports --auto-correct. Lint/UnusedBlockArgument: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter/json_api/fragment_cache.rb' # Offense count: 9 # Cop supports --auto-correct. Lint/UnusedMethodArgument: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter.rb' + - 'lib/active_model/serializer/adapter/null.rb' + - 'lib/active_model/serializer/pass_through_serializer.rb' + - 'test/fixtures/poro.rb' + - 'test/lint_test.rb' # Offense count: 1 Lint/UselessAccessModifier: - Enabled: false + Exclude: + - 'lib/active_model/serializable_resource.rb' # Offense count: 3 Lint/UselessAssignment: - Enabled: false + Exclude: + - 'bench/perf.rb' + - 'lib/active_model/serializer/adapter/json_api/fragment_cache.rb' + - 'test/test_helper.rb' # Offense count: 1 # Configuration parameters: EnforcedStyle, SupportedStyles. Rails/Date: - Enabled: false + Exclude: + - 'test/fixtures/poro.rb' # Offense count: 8 # Configuration parameters: EnforcedStyle, SupportedStyles. Rails/TimeZone: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/AccessModifierIndentation: - Enabled: false + Exclude: + - 'test/action_controller/serialization_test.rb' + - 'test/fixtures/poro.rb' + - 'test/serializers/cache_test.rb' # Offense count: 16 # Cop supports --auto-correct. # Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle, SupportedLastArgumentHashStyles. Style/AlignHash: - Enabled: false - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/AlignParameters: - Enabled: false + Exclude: + - 'test/action_controller/json_api/pagination_test.rb' # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. Style/AndOr: - Enabled: false + Exclude: + - 'lib/active_model/serializer/lint.rb' # Offense count: 6 # Cop supports --auto-correct. @@ -80,63 +88,51 @@ Style/BlockDelimiters: # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. Style/BracesAroundHashParameters: - Enabled: false + Exclude: + - 'test/action_controller/adapter_selector_test.rb' + - 'test/action_controller/json_api/pagination_test.rb' + - 'test/action_controller/serialization_test.rb' + - 'test/adapter/json_api/linked_test.rb' + - 'test/adapter/json_api/pagination_links_test.rb' + - 'test/adapter/null_test.rb' + - 'test/adapter_test.rb' + - 'test/array_serializer_test.rb' + - 'test/serializable_resource_test.rb' + - 'test/serializers/associations_test.rb' + - 'test/serializers/attribute_test.rb' + - 'test/serializers/attributes_test.rb' + - 'test/serializers/fieldset_test.rb' + - 'test/serializers/root_test.rb' + - 'test/serializers/urls_test.rb' # Offense count: 167 # Configuration parameters: EnforcedStyle, SupportedStyles. Style/ClassAndModuleChildren: Enabled: false -# Offense count: 1 +# Offense count: 5 # Cop supports --auto-correct. Style/CommentIndentation: - Enabled: false + Exclude: + - 'active_model_serializers.gemspec' # Offense count: 1 Style/DoubleNegation: - Enabled: false + Exclude: + - 'lib/active_model/serializable_resource.rb' # Offense count: 1 Style/EachWithObject: - Enabled: false - -# Offense count: 4 -# Cop supports --auto-correct. -Style/EmptyLines: - Enabled: false - -# Offense count: 3 -# Cop supports --auto-correct. -Style/EmptyLinesAroundAccessModifier: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundBlockBody: - Enabled: false - -# Offense count: 16 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundClassBody: - Enabled: false - -# Offense count: 9 -# Cop supports --auto-correct. -Style/EmptyLinesAroundMethodBody: - Enabled: false - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundModuleBody: - Enabled: false + Exclude: + - 'lib/active_model/serializer/fieldset.rb' # Offense count: 3 # Configuration parameters: MinBodyLength. Style/GuardClause: - Enabled: false + Exclude: + - 'lib/active_model/serializer.rb' + - 'lib/active_model/serializer/adapter/json_api.rb' + - 'test/capture_warnings.rb' # Offense count: 12 # Cop supports --auto-correct. @@ -147,7 +143,11 @@ Style/HashSyntax: # Offense count: 9 # Cop supports --auto-correct. Style/IndentArray: - Enabled: false + Exclude: + - 'test/adapter/json/has_many_test.rb' + - 'test/adapter/json_api/json_api_test.rb' + - 'test/adapter/json_api/pagination_links_test.rb' + - 'test/adapter/json_test.rb' # Offense count: 8 # Cop supports --auto-correct. @@ -159,23 +159,29 @@ Style/IndentHash: # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. Style/IndentationConsistency: - Enabled: false + Exclude: + - 'test/action_controller/serialization_scope_name_test.rb' # Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: Width. Style/IndentationWidth: - Enabled: false + Exclude: + - 'lib/active_model/serializable_resource.rb' + - 'lib/active_model/serializer/fieldset.rb' # Offense count: 1 # Cop supports --auto-correct. Style/Lambda: - Enabled: false + Exclude: + - 'lib/active_model/serializer.rb' # Offense count: 2 # Cop supports --auto-correct. Style/MethodCallParentheses: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter/json.rb' + - 'lib/active_model/serializer/adapter/json_api.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -192,12 +198,14 @@ Style/MultilineOperationIndentation: # Offense count: 1 # Cop supports --auto-correct. Style/NegatedIf: - Enabled: false + Exclude: + - 'lib/action_controller/serialization.rb' # Offense count: 1 # Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles. Style/Next: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter/json_api.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -208,95 +216,56 @@ Style/NumericLiterals: # Cop supports --auto-correct. # Configuration parameters: PreferredDelimiters. Style/PercentLiteralDelimiters: - Enabled: false + Exclude: + - 'active_model_serializers.gemspec' # Offense count: 2 # Cop supports --auto-correct. Style/PerlBackrefs: - Enabled: false + Exclude: + - 'test/fixtures/poro.rb' + - 'test/serializers/associations_test.rb' # Offense count: 6 # Configuration parameters: NamePrefix, NamePrefixBlacklist. Style/PredicateName: - Enabled: false + Exclude: + - 'lib/active_model/serializer/adapter.rb' + - 'lib/active_model/serializer/adapter/json_api.rb' + - 'lib/active_model/serializer/associations.rb' + - 'test/action_controller/json_api/linked_test.rb' # Offense count: 7 # Cop supports --auto-correct. Style/RedundantSelf: - Enabled: false + Exclude: + - 'lib/active_model/serializer.rb' + - 'lib/active_model/serializer/associations.rb' + - 'test/fixtures/poro.rb' # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AllowAsExpressionSeparator. Style/Semicolon: - Enabled: false + Exclude: + - 'lib/active_model/serializer/fieldset.rb' # Offense count: 6 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. Style/SignalException: - Enabled: false + Exclude: + - 'lib/active_model/serializer.rb' + - 'lib/active_model/serializer/adapter.rb' + - 'lib/active_model/serializer/fieldset.rb' + - 'lib/active_model/serializer/pass_through_serializer.rb' # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AllowIfMethodIsEmpty. Style/SingleLineMethods: - Enabled: false - -# Offense count: 26 -# Cop supports --auto-correct. -Style/SpaceAfterColon: - Enabled: false - -# Offense count: 7 -# Cop supports --auto-correct. -Style/SpaceAfterComma: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -Style/SpaceAfterNot: - Enabled: false - -# Offense count: 5 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/SpaceAroundEqualsInParameterDefault: - Enabled: false - -# Offense count: 39 -# Cop supports --auto-correct. -# Configuration parameters: MultiSpaceAllowedForOperators. -Style/SpaceAroundOperators: - Enabled: false - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/SpaceBeforeBlockBraces: - Enabled: false - -# Offense count: 8 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -Style/SpaceInsideBlockBraces: - Enabled: false - -# Offense count: 20 -# Cop supports --auto-correct. -Style/SpaceInsideBrackets: - Enabled: false - -# Offense count: 179 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. -Style/SpaceInsideHashLiteralBraces: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -Style/SpaceInsideParens: - Enabled: false + Exclude: + - 'test/serializers/serializer_for_test.rb' # Offense count: 2 # Cop supports --auto-correct. @@ -306,42 +275,41 @@ Style/StringLiteralsInInterpolation: # Offense count: 1 Style/StructInheritance: - Enabled: false + Exclude: + - 'bench/perf.rb' # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: IgnoredMethods. Style/SymbolProc: - Enabled: false + Exclude: + - 'lib/generators/serializer/serializer_generator.rb' -# Offense count: 9 +# Offense count: 8 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. Style/TrailingBlankLines: - Enabled: false + Exclude: + - 'lib/active_model/serializer/pass_through_serializer.rb' + - 'lib/generators/serializer/serializer_generator.rb' + - 'test/adapter/fragment_cache_test.rb' + - 'test/adapter/json_api/json_api_test.rb' + - 'test/adapter/null_test.rb' + - 'test/serializers/cache_test.rb' + - 'test/serializers/fieldset_test.rb' + - 'test/support/stream_capture.rb' # Offense count: 6 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyleForMultiline, SupportedStyles. Style/TrailingComma: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -Style/TrailingWhitespace: - Enabled: false + Exclude: + - 'test/action_controller/adapter_selector_test.rb' + - 'test/action_controller/serialization_test.rb' + - 'test/adapter/json_api/belongs_to_test.rb' + - 'test/adapter/json_api/linked_test.rb' # Offense count: 1 Style/UnlessElse: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -Style/UnneededPercentQ: - Enabled: false - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: WordRegex. -Style/WordArray: - MinSize: 2 + Exclude: + - 'lib/active_model/serializer.rb' diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index de385a97b..43b1c7ac8 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -9,8 +9,8 @@ Gem::Specification.new do |spec| spec.platform = Gem::Platform::RUBY spec.authors = ['Steve Klabnik'] spec.email = ['steve@steveklabnik.com'] - spec.summary = %q{Conventions-based JSON generation for Rails.} - spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.} + spec.summary = 'Conventions-based JSON generation for Rails.' + spec.description = 'ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.' spec.homepage = 'https://github.com/rails-api/active_model_serializers' spec.license = 'MIT' diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index 020261314..e05c340b2 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -20,7 +20,7 @@ def serialization_scope end def get_serializer(resource, options = {}) - if ! use_adapter? + if !use_adapter? warn 'ActionController::Serialization#use_adapter? has been removed. '\ "Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize" options[:adapter] = false diff --git a/lib/active_model/serializable_resource.rb b/lib/active_model/serializable_resource.rb index aa8f139a2..feae1e8e7 100644 --- a/lib/active_model/serializable_resource.rb +++ b/lib/active_model/serializable_resource.rb @@ -1,7 +1,6 @@ require 'set' module ActiveModel class SerializableResource - ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter]) def initialize(resource, options = {}) @@ -79,6 +78,5 @@ def serializer? ActiveModelSerializers.silence_warnings do attr_reader :resource, :adapter_opts, :serializer_opts end - end end diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 6e6833bb5..3727816c4 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -13,7 +13,6 @@ class Serializer include Configuration include Associations - # Matches # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `'" # AND diff --git a/lib/active_model/serializer/adapter/fragment_cache.rb b/lib/active_model/serializer/adapter/fragment_cache.rb index ae15995fe..8463b5a23 100644 --- a/lib/active_model/serializer/adapter/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/fragment_cache.rb @@ -2,7 +2,6 @@ module ActiveModel class Serializer class Adapter class FragmentCache - attr_reader :serializer def initialize(adapter, serializer, options) @@ -35,7 +34,7 @@ def fetch def cached_attributes(klass, serializers) attributes = serializer.class._attributes - cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject {|attr| klass._cache_except.include?(attr) } + cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject { |attr| klass._cache_except.include?(attr) } non_cached_attributes = attributes - cached_attributes cached_attributes.each do |attribute| @@ -60,7 +59,7 @@ def fragment_serializer(name, klass) Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached) Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached) - klass._cache_options ||= {} + klass._cache_options ||= {} klass._cache_options[:key] = klass._cache_key if klass._cache_key cached.constantize.cache(klass._cache_options) @@ -68,7 +67,7 @@ def fragment_serializer(name, klass) cached.constantize.fragmented(serializer) non_cached.constantize.fragmented(serializer) - serializers = {cached: cached, non_cached: non_cached} + serializers = { cached: cached, non_cached: non_cached } cached_attributes(klass, serializers) serializers end diff --git a/lib/active_model/serializer/adapter/json.rb b/lib/active_model/serializer/adapter/json.rb index 58704b56e..b3fa6e9c6 100644 --- a/lib/active_model/serializer/adapter/json.rb +++ b/lib/active_model/serializer/adapter/json.rb @@ -46,7 +46,6 @@ def serializable_hash(options = nil) def fragment_cache(cached_hash, non_cached_hash) Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash) end - end end end diff --git a/lib/active_model/serializer/adapter/json/fragment_cache.rb b/lib/active_model/serializer/adapter/json/fragment_cache.rb index 0d01b87a2..846a216ff 100644 --- a/lib/active_model/serializer/adapter/json/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/json/fragment_cache.rb @@ -4,11 +4,9 @@ class Serializer class Adapter class Json < Adapter class FragmentCache - def fragment_cache(cached_hash, non_cached_hash) non_cached_hash.merge cached_hash end - end end end diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 1841f9ff3..48f8173c8 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -50,7 +50,7 @@ def add_relationships(resource, name, serializers) resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } } end - def add_relationship(resource, name, serializer, val=nil) + def add_relationship(resource, name, serializer, val = nil) resource[:relationships] ||= {} resource[:relationships][name] = { data: val } diff --git a/lib/active_model/serializer/adapter/json_api/fragment_cache.rb b/lib/active_model/serializer/adapter/json_api/fragment_cache.rb index d266801fe..070371ac7 100644 --- a/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/json_api/fragment_cache.rb @@ -4,19 +4,17 @@ class Serializer class Adapter class JsonApi < Adapter class FragmentCache - def fragment_cache(root, cached_hash, non_cached_hash) hash = {} core_cached = cached_hash.first core_non_cached = non_cached_hash.first - no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] } - no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] } + no_root_cache = cached_hash.delete_if { |key, value| key == core_cached[0] } + no_root_non_cache = non_cached_hash.delete_if { |key, value| key == core_non_cached[0] } cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1] hash = (root) ? { root => cached_resource } : cached_resource hash.deep_merge no_root_non_cache.deep_merge no_root_cache end - end end end diff --git a/lib/active_model/serializer/association.rb b/lib/active_model/serializer/association.rb index bca036658..1003f0a6f 100644 --- a/lib/active_model/serializer/association.rb +++ b/lib/active_model/serializer/association.rb @@ -10,7 +10,6 @@ class Serializer # Association.new(:comments, CommentSummarySerializer) # Association = Struct.new(:name, :serializer, :options) do - # @return [Symbol] # def key diff --git a/lib/active_model/serializer/fieldset.rb b/lib/active_model/serializer/fieldset.rb index 63333cf27..30e683344 100644 --- a/lib/active_model/serializer/fieldset.rb +++ b/lib/active_model/serializer/fieldset.rb @@ -1,7 +1,6 @@ module ActiveModel class Serializer class Fieldset - def initialize(fields, root = nil) @root = root @raw_fields = fields @@ -16,7 +15,7 @@ def fields_for(serializer) fields[key.to_sym] || fields[key.pluralize.to_sym] end - private + private ActiveModelSerializers.silence_warnings do attr_reader :raw_fields, :root @@ -24,7 +23,7 @@ def fields_for(serializer) def parsed_fields if raw_fields.is_a?(Hash) - raw_fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h} + raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h } elsif raw_fields.is_a?(Array) if root.nil? raise ArgumentError, 'The root argument must be specified if the fileds argument is an array.' @@ -36,7 +35,6 @@ def parsed_fields {} end end - end end end diff --git a/lib/active_model/serializer/lint.rb b/lib/active_model/serializer/lint.rb index 811085f7e..29d564ed6 100644 --- a/lib/active_model/serializer/lint.rb +++ b/lib/active_model/serializer/lint.rb @@ -15,7 +15,6 @@ module ActiveModel::Serializer::Lint # always return +{}+, and the tests would pass. It is up to you to ensure # that the values are semantically meaningful. module Tests - # Passes if the object responds to serializable_hash and if it takes # zero or one arguments. # Fails otherwise. @@ -126,6 +125,5 @@ def resource def assert_instance_of(result, name) assert result.instance_of?(name), "#{result} should be an instance of #{name}" end - end end diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 8b90ba7a9..d847bea98 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -8,7 +8,6 @@ def silence_warnings ensure $VERBOSE = verbose end - end require 'active_model' diff --git a/test/action_controller/explicit_serializer_test.rb b/test/action_controller/explicit_serializer_test.rb index 1eafca168..17c70350a 100644 --- a/test/action_controller/explicit_serializer_test.rb +++ b/test/action_controller/explicit_serializer_test.rb @@ -103,7 +103,7 @@ def test_render_array_using_explicit_serializer_and_custom_serializers { 'title' => 'New Post', 'body' => 'Body', 'id' => assigns(:post).id, - 'comments' => [{'id' => 1}, {'id' => 2}], + 'comments' => [{ 'id' => 1 }, { 'id' => 2 }], 'author' => { 'id' => assigns(:author).id } } ] diff --git a/test/action_controller/json_api/linked_test.rb b/test/action_controller/json_api/linked_test.rb index ce299a6de..fc0c87939 100644 --- a/test/action_controller/json_api/linked_test.rb +++ b/test/action_controller/json_api/linked_test.rb @@ -75,7 +75,7 @@ def render_collection_without_include def render_collection_with_include setup_post - render json: [@post], include: ['author', 'comments'], adapter: :json_api + render json: [@post], include: %w(author comments), adapter: :json_api end end @@ -107,7 +107,7 @@ def test_render_resource_with_nested_has_many_include }, 'relationships' => { 'posts' => { 'data' => [] }, - 'roles' => { 'data' => [{ 'type' =>'roles', 'id' => '1' }, { 'type' =>'roles', 'id' => '2' }] }, + 'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] }, 'bio' => { 'data' => nil } } }, { @@ -119,7 +119,7 @@ def test_render_resource_with_nested_has_many_include 'slug' => 'admin-1' }, 'relationships' => { - 'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } } + 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } } } }, { 'id' => '2', @@ -130,7 +130,7 @@ def test_render_resource_with_nested_has_many_include 'slug' => 'colab-2' }, 'relationships' => { - 'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } } + 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } } } } ] @@ -172,7 +172,7 @@ def test_render_collection_with_missing_nested_has_many_include end def has_type?(collection, value) - collection.detect { |i| i['type'] == value} + collection.detect { |i| i['type'] == value } end end end diff --git a/test/action_controller/json_api/pagination_test.rb b/test/action_controller/json_api/pagination_test.rb index d78c04451..4286ed886 100644 --- a/test/action_controller/json_api/pagination_test.rb +++ b/test/action_controller/json_api/pagination_test.rb @@ -47,11 +47,11 @@ def render_array_without_pagination_links tests PaginationTestController def test_render_pagination_links_with_will_paginate - expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", - 'first'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - 'prev'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} + expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", + 'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" } get :render_pagination_using_will_paginate, page: { number: 2, size: 1 } response = JSON.parse(@response.body) @@ -59,47 +59,47 @@ def test_render_pagination_links_with_will_paginate end def test_render_only_last_and_next_pagination_links - expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", - 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", - 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"} + expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", + 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", + 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" } get :render_pagination_using_will_paginate, page: { number: 1, size: 2 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_pagination_links_with_kaminari - expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", - 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - 'next'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - 'last'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"} + expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", + 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" } get :render_pagination_using_kaminari, page: { number: 2, size: 1 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_prev_and_first_pagination_links - expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", - 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", - 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"} + expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", + 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" } get :render_pagination_using_kaminari, page: { number: 3, size: 1 } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_last_and_next_pagination_links_with_additional_params - expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", - 'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", - 'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"} + expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", + 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", + 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" } get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional' response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_render_only_prev_and_first_pagination_links_with_additional_params - expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", - 'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", - 'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"} + expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", + 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", + 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" } get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional' response = JSON.parse(@response.body) assert_equal expected_links, response['links'] diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index e8bd77df2..a5535fa6e 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -48,7 +48,6 @@ def render_array_using_implicit_serializer def render_array_using_implicit_serializer_and_meta with_adapter ActiveModel::Serializer::Adapter::JsonApi do - @profiles = [ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) ] @@ -69,11 +68,11 @@ def render_object_with_cache_enabled end def render_json_object_without_serializer - render json: {error: 'Result is Invalid'} + render json: { error: 'Result is Invalid' } end def render_json_array_object_without_serializer - render json: [{error: 'Result is Invalid'}] + render json: [{ error: 'Result is Invalid' }] end def update_and_render_object_with_cache_enabled @@ -138,6 +137,7 @@ def render_fragment_changed_object_with_relationship end private + def generate_cached_serializer(obj) ActiveModel::SerializableResource.new(obj).to_json end @@ -188,7 +188,7 @@ def test_render_using_default_root def test_render_array_using_custom_root get :render_array_using_custom_root - expected = {custom_roots: [{name: 'Name 1', description: 'Description 1'}]} + expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] } assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -196,7 +196,7 @@ def test_render_array_using_custom_root def test_render_array_that_is_empty_using_custom_root get :render_array_that_is_empty_using_custom_root - expected = {custom_roots: []} + expected = { custom_roots: [] } assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -204,7 +204,7 @@ def test_render_array_that_is_empty_using_custom_root def test_render_object_using_custom_root get :render_object_using_custom_root - expected = {custom_root: {name: 'Name 1', description: 'Description 1'}} + expected = { custom_root: { name: 'Name 1', description: 'Description 1' } } assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -213,7 +213,7 @@ def test_render_json_object_without_serializer get :render_json_object_without_serializer assert_equal 'application/json', @response.content_type - expected_body = {error: 'Result is Invalid'} + expected_body = { error: 'Result is Invalid' } assert_equal expected_body.to_json, @response.body end @@ -221,7 +221,7 @@ def test_render_json_array_object_without_serializer get :render_json_array_object_without_serializer assert_equal 'application/json', @response.content_type - expected_body = [{error: 'Result is Invalid'}] + expected_body = [{ error: 'Result is Invalid' }] assert_equal expected_body.to_json, @response.body end @@ -357,11 +357,11 @@ def test_render_fragment_changed_object_with_relationship response = JSON.parse(@response.body) expected_return = { - 'id'=>1, - 'time'=>Time.now.to_s, + 'id' => 1, + 'time' => Time.now.to_s, 'likeable' => { - 'id'=>1, - 'body'=>'ZOMG A COMMENT' + 'id' => 1, + 'body' => 'ZOMG A COMMENT' } } @@ -384,7 +384,7 @@ def test_cache_expiration_on_update body: 'ZOMG A COMMENT' } ], blog: { - id:999, + id: 999, name: 'Custom blog' }, author: { diff --git a/test/adapter/fragment_cache_test.rb b/test/adapter/fragment_cache_test.rb index aa39cd523..6442156d5 100644 --- a/test/adapter/fragment_cache_test.rb +++ b/test/adapter/fragment_cache_test.rb @@ -6,7 +6,7 @@ class FragmentCacheTest < Minitest::Test def setup @spam = Spam::UnrelatedLink.new(id: 'spam-id-1') @author = Author.new(name: 'Joao M. D. Moura') - @role = Role.new(name: 'Great Author', description:nil) + @role = Role.new(name: 'Great Author', description: nil) @role.author = [@author] @role_serializer = RoleSerializer.new(@role) @spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam) diff --git a/test/adapter/json/belongs_to_test.rb b/test/adapter/json/belongs_to_test.rb index 31b663bcb..217f920c9 100644 --- a/test/adapter/json/belongs_to_test.rb +++ b/test/adapter/json/belongs_to_test.rb @@ -24,21 +24,21 @@ def setup end def test_includes_post - assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post]) + assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post]) end def test_include_nil_author serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - assert_equal({post: {title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: {id: 999, name: 'Custom blog'}, author: nil}}, adapter.serializable_hash) + assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash) end def test_include_nil_author_with_specified_serializer serializer = PostPreviewSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - assert_equal({post: {title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil}}, adapter.serializable_hash) + assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash) end end end diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb index 900256dda..8ebc0aa8d 100644 --- a/test/adapter/json/collection_test.rb +++ b/test/adapter/json/collection_test.rb @@ -26,11 +26,11 @@ def test_with_serializer_option serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - expected = {blogs:[{ + expected = { blogs: [{ id: 1, special_attribute: 'Special', - articles: [{id: 1,title: 'Hello!!', body: 'Hello, world!!'}, {id: 2, title: 'New Post', body: 'Body'}] - }]} + articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }] + }] } assert_equal expected, adapter.serializable_hash end @@ -64,7 +64,7 @@ def test_include_multiple_posts id: 999, name: 'Custom blog' } - }]} + }] } assert_equal expected, adapter.serializable_hash end diff --git a/test/adapter/json/has_many_test.rb b/test/adapter/json/has_many_test.rb index 00df72324..2a0a96c61 100644 --- a/test/adapter/json/has_many_test.rb +++ b/test/adapter/json/has_many_test.rb @@ -25,8 +25,8 @@ def test_has_many serializer = PostSerializer.new(@post) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) assert_equal([ - {id: 1, body: 'ZOMG A COMMENT'}, - {id: 2, body: 'ZOMG ANOTHER COMMENT'} + { id: 1, body: 'ZOMG A COMMENT' }, + { id: 2, body: 'ZOMG ANOTHER COMMENT' } ], adapter.serializable_hash[:post][:comments]) end @@ -36,7 +36,7 @@ def test_has_many_with_no_serializer assert_equal({ id: 42, tags: [ - {'attributes'=>{'id'=>1, 'name'=>'#hash_tag'}} + { 'attributes' => { 'id' => 1, 'name' => '#hash_tag' } } ] }.to_json, adapter.serializable_hash[:post].to_json) end diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 382c92901..5cb6cdeb0 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -47,7 +47,7 @@ def test_includes_linked_post body: 'Body', }, relationships: { - comments: { data: [ { type: 'comments', id: '1' } ] }, + comments: { data: [{ type: 'comments', id: '1' }] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { type: 'authors', id: '1' } } } @@ -56,7 +56,7 @@ def test_includes_linked_post end def test_limiting_linked_post_fields - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]}) + @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: { post: [:title] }) expected = [{ id: '42', type: 'posts', @@ -64,7 +64,7 @@ def test_limiting_linked_post_fields title: 'New Post' }, relationships: { - comments: { data: [ { type: 'comments', id: '1' } ] }, + comments: { data: [{ type: 'comments', id: '1' }] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { type: 'authors', id: '1' } } } @@ -76,7 +76,7 @@ def test_include_nil_author serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships]) + assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships]) end def test_include_type_for_association_when_different_than_name @@ -108,7 +108,7 @@ def test_include_type_for_association_when_different_than_name def test_include_linked_resources_with_type_name serializer = BlogSerializer.new(@blog) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles']) + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: %w(writer articles)) linked = adapter.serializable_hash[:included] expected = [ { @@ -122,7 +122,7 @@ def test_include_linked_resources_with_type_name roles: { data: [] }, bio: { data: nil } } - },{ + }, { id: '42', type: 'posts', attributes: { @@ -130,7 +130,7 @@ def test_include_linked_resources_with_type_name body: 'Body' }, relationships: { - comments: { data: [ { type: 'comments', id: '1' } ] }, + comments: { data: [{ type: 'comments', id: '1' }] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { type: 'authors', id: '1' } } } diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index db72edb67..c17c8aadb 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -88,7 +88,6 @@ def test_limiting_fields ] assert_equal(expected, @adapter.serializable_hash[:data]) end - end end end diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 2adaa88e3..15243c7ad 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -27,8 +27,8 @@ def setup def test_includes_comment_ids expected = { data: [ - { type: 'posts', id: '1'}, - { type: 'posts', id: '2'} + { type: 'posts', id: '1' }, + { type: 'posts', id: '2' } ] } diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index aa8a926ed..aedde98c1 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -24,7 +24,7 @@ def setup @serializer = PostPreviewSerializer.new(@post) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new( @serializer, - include: ['comments', 'author'] + include: %w(comments author) ) end @@ -60,7 +60,7 @@ def test_includes_linked_data id: @author.id.to_s, type: 'authors', relationships: { - posts: { data: [ {type: 'posts', id: @post.id.to_s } ] } + posts: { data: [{ type: 'posts', id: @post.id.to_s }] } } } ] diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 5c2824434..277380a01 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -36,7 +36,7 @@ def setup end def test_includes_comment_ids - expected = { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] } + expected = { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] } assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments]) end @@ -68,7 +68,7 @@ def test_includes_linked_comments end def test_limit_fields_of_linked_comments - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]}) + @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: { comment: [:id] }) expected = [{ id: '1', type: 'comments', @@ -117,7 +117,7 @@ def test_has_many_with_no_serializer id: '1', type: 'posts', relationships: { - tags: { data: [@tag.as_json]} + tags: { data: [@tag.as_json] } } } }, adapter.serializable_hash) @@ -132,8 +132,8 @@ def test_has_many_with_virtual_value id: '1', type: 'virtual_values', relationships: { - maker: {data: {id: 1}}, - reviews: {data: [{id: 1}, {id: 2}]} + maker: { data: { id: 1 } }, + reviews: { data: [{ id: 1 }, { id: 2 }] } } } }, adapter.serializable_hash) diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index baf0c4e63..3fb2bf5fa 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -45,7 +45,7 @@ def test_includes_linked_bio id: '43', type: 'bios', attributes: { - content:'AMS Contributor', + content: 'AMS Contributor', rating: nil }, relationships: { @@ -66,8 +66,8 @@ def test_has_one_with_virtual_value id: '1', type: 'virtual_values', relationships: { - maker: {data: {id: 1}}, - reviews: {data: [{id: 1}, {id: 2}]} + maker: { data: { id: 1 } }, + reviews: { data: [{ id: 1 }, { id: 2 }] } } } } diff --git a/test/adapter/json_api/json_api_test.rb b/test/adapter/json_api/json_api_test.rb index 6867aab0a..16d1f93df 100644 --- a/test/adapter/json_api/json_api_test.rb +++ b/test/adapter/json_api/json_api_test.rb @@ -16,7 +16,6 @@ def setup @post.author = @author @blog = Blog.new(id: 1, name: 'My Blog!!') @post.blog = @blog - end def test_custom_keys @@ -25,11 +24,11 @@ def test_custom_keys assert_equal({ reviews: { data: [ - {type: 'comments', id: '1'}, - {type: 'comments', id: '2'} - ]}, - writer: { data: {type: 'authors', id: '1'} }, - site: { data: {type: 'blogs', id: '1' } } + { type: 'comments', id: '1' }, + { type: 'comments', id: '2' } + ] }, + writer: { data: { type: 'authors', id: '1' } }, + site: { data: { type: 'blogs', id: '1' } } }, adapter.serializable_hash[:data][:relationships]) end end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index 6d49cc8f7..261c001a8 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -60,7 +60,7 @@ def test_include_multiple_posts_and_linked_array body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] }, + comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { type: 'authors', id: '1' } } } @@ -107,7 +107,7 @@ def test_include_multiple_posts_and_linked_array name: 'Steve K.' }, relationships: { - posts: { data: [ { type: 'posts', id: '10' }, { type: 'posts', id: '30' } ] }, + posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] }, roles: { data: [] }, bio: { data: { type: 'bios', id: '1' } } } @@ -128,7 +128,7 @@ def test_include_multiple_posts_and_linked_array name: 'Tenderlove' }, relationships: { - posts: { data: [ { type: 'posts', id:'20' } ] }, + posts: { data: [{ type: 'posts', id: '20' }] }, roles: { data: [] }, bio: { data: { type: 'bios', id: '2' } } } @@ -168,9 +168,9 @@ def test_include_multiple_posts_and_linked name: 'Steve K.' }, relationships: { - posts: { data: [ { type: 'posts', id: '10'}, { type: 'posts', id: '30' }] }, + posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] }, roles: { data: [] }, - bio: { data: { type: 'bios', id: '1' }} + bio: { data: { type: 'bios', id: '1' } } } }, { id: '10', @@ -180,7 +180,7 @@ def test_include_multiple_posts_and_linked body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: 'comments', id: '1'}, { type: 'comments', id: '2' }] }, + comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { type: 'authors', id: '1' } } } @@ -237,13 +237,13 @@ def test_multiple_references_to_same_resource }, relationships: { comments: { - data: [{type: 'comments', id: '1'}, {type: 'comments', id: '2'}] + data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }, blog: { - data: {type: 'blogs', id: '999'} + data: { type: 'blogs', id: '999' } }, author: { - data: {type: 'authors', id: '1'} + data: { type: 'authors', id: '1' } } } } @@ -269,7 +269,7 @@ def test_nil_link_with_specified_serializer body: 'Hello, world!!' }, relationships: { - comments: { data: [ { type: 'comments', id: '1' }, { type: 'comments', id: '2' } ] }, + comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }, author: { data: nil } } } diff --git a/test/adapter/json_api/pagination_links_test.rb b/test/adapter/json_api/pagination_links_test.rb index 1a7410451..52fdfd7b2 100644 --- a/test/adapter/json_api/pagination_links_test.rb +++ b/test/adapter/json_api/pagination_links_test.rb @@ -20,9 +20,9 @@ def setup ] end - def mock_request(query_parameters={}, original_url=URI) + def mock_request(query_parameters = {}, original_url = URI) context = Minitest::Mock.new - context.expect(:original_url, original_url ) + context.expect(:original_url, original_url) context.expect(:query_parameters, query_parameters) @options = {} @options[:context] = context @@ -42,17 +42,17 @@ def using_will_paginate end def 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' } } + { 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' } } ] } end def links { - links:{ + links: { self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1", first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", @@ -74,7 +74,7 @@ def expected_response_with_pagination_links end def expected_response_with_pagination_links_and_additional_params - new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" } + new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" } {}.tap do |hash| hash[:data] = [data.values.flatten.second] hash.merge! links: new_links diff --git a/test/adapter/json_test.rb b/test/adapter/json_test.rb index e7beffd78..dac6b1408 100644 --- a/test/adapter/json_test.rb +++ b/test/adapter/json_test.rb @@ -23,8 +23,8 @@ def setup def test_has_many assert_equal([ - {id: 1, body: 'ZOMG A COMMENT'}, - {id: 2, body: 'ZOMG ANOTHER COMMENT'} + { id: 1, body: 'ZOMG A COMMENT' }, + { id: 2, body: 'ZOMG ANOTHER COMMENT' } ], @adapter.serializable_hash[:post][:comments]) end @@ -34,11 +34,11 @@ def test_custom_keys 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!!'} + writer: { id: 1, name: 'Steve K.' }, + site: { id: 1, name: 'My Blog!!' } }, adapter.serializable_hash[:post]) end end diff --git a/test/adapter_test.rb b/test/adapter_test.rb index 3349d8a76..73bacdd5e 100644 --- a/test/adapter_test.rb +++ b/test/adapter_test.rb @@ -35,12 +35,12 @@ def test_create_adapter end def test_create_adapter_with_override - adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api}) + adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api }) assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class end def test_inflected_adapter_class_for_known_adapter - ActiveSupport::Inflector.inflections(:en){|inflect| inflect.acronym 'API' } + ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' } klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api) ActiveSupport::Inflector.inflections.acronyms.clear diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb index 834dea54d..003f7d41f 100644 --- a/test/array_serializer_test.rb +++ b/test/array_serializer_test.rb @@ -7,11 +7,11 @@ def setup @comment = Comment.new @post = Post.new @resource = build_named_collection @comment, @post - @serializer = ArraySerializer.new(@resource, {some: :options}) + @serializer = ArraySerializer.new(@resource, { some: :options }) end def build_named_collection(*resource) - resource.define_singleton_method(:name){ 'MeResource' } + resource.define_singleton_method(:name) { 'MeResource' } resource end @@ -36,13 +36,13 @@ def test_each_object_should_be_serialized_with_appropriate_serializer end def test_serializer_option_not_passed_to_each_serializer - serializers = ArraySerializer.new([@post], {serializer: PostSerializer}).to_a + serializers = ArraySerializer.new([@post], { serializer: PostSerializer }).to_a refute serializers.first.custom_options.key?(:serializer) end def test_meta_and_meta_key_attr_readers - meta_content = {meta: 'the meta', meta_key: 'the meta key'} + meta_content = { meta: 'the meta', meta_key: 'the meta key' } @serializer = ArraySerializer.new([@comment, @post], meta_content) assert_equal @serializer.meta, 'the meta' @@ -77,7 +77,7 @@ def test_json_key_with_resource_with_name_and_no_serializers def test_json_key_with_resource_with_nil_name_and_no_serializers resource = [] - resource.define_singleton_method(:name){ nil } + resource.define_singleton_method(:name) { nil } serializer = ArraySerializer.new(resource) assert_equal serializer.json_key, nil end diff --git a/test/capture_warnings.rb b/test/capture_warnings.rb index 9eb8bf77d..410040b08 100644 --- a/test/capture_warnings.rb +++ b/test/capture_warnings.rb @@ -53,5 +53,6 @@ def after_tests end private + attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index b25ea7114..f831296f8 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -7,7 +7,7 @@ def self.model_name @_model_name ||= ActiveModel::Name.new(self) end - def initialize(hash={}) + def initialize(hash = {}) @attributes = hash end @@ -94,7 +94,7 @@ module Spam; end Spam::UnrelatedLink = Class.new(Model) PostSerializer = Class.new(ActiveModel::Serializer) do - cache key:'post', expires_in: 0.1, skip_digest: true + cache key: 'post', expires_in: 0.1, skip_digest: true attributes :id, :title, :body has_many :comments @@ -133,7 +133,7 @@ def custom_options end AuthorSerializer = Class.new(ActiveModel::Serializer) do - cache key:'writer', skip_digest: true + cache key: 'writer', skip_digest: true attribute :id attribute :name @@ -249,8 +249,8 @@ def self.root_name VirtualValueSerializer = Class.new(ActiveModel::Serializer) do attributes :id - has_many :reviews, virtual_value: [{id: 1}, {id: 2}] - has_one :maker, virtual_value: {id: 1} + has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }] + has_one :maker, virtual_value: { id: 1 } def reviews end diff --git a/test/lint_test.rb b/test/lint_test.rb index 61329b247..9257eec1e 100644 --- a/test/lint_test.rb +++ b/test/lint_test.rb @@ -7,27 +7,21 @@ class LintTest < Minitest::Test class CompliantResource def serializable_hash(options = nil) - end def read_attribute_for_serialization(name) - end def as_json(options = nil) - end def to_json(options = nil) - end def cache_key - end def id - end def self.model_name @@ -38,7 +32,6 @@ def self.model_name def setup @resource = CompliantResource.new end - end end end diff --git a/test/serializers/adapter_for_test.rb b/test/serializers/adapter_for_test.rb index 507b6bf1c..3a17cd243 100644 --- a/test/serializers/adapter_for_test.rb +++ b/test/serializers/adapter_for_test.rb @@ -20,7 +20,6 @@ def test_overwrite_adapter_with_symbol adapter = ActiveModel::Serializer.adapter assert_equal ActiveModel::Serializer::Adapter::Null, adapter ensure - end def test_overwrite_adapter_with_class diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index 570f92054..75e2db208 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -4,7 +4,7 @@ module ActiveModel class Serializer class AssociationsTest < Minitest::Test class Model - def initialize(hash={}) + def initialize(hash = {}) @attributes = hash end @@ -29,7 +29,7 @@ def setup @author.roles = [] @blog = Blog.new({ name: 'AMS Blog' }) @post = Post.new({ title: 'New Post', body: 'Body' }) - @tag = Tag.new({name: '#hashtagged'}) + @tag = Tag.new({ name: '#hashtagged' }) @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) @post.comments = [@comment] @post.tags = [@tag] @@ -39,7 +39,7 @@ def setup @post.author = @author @author.posts = [@post] - @post_serializer = PostSerializer.new(@post, {custom_options: true}) + @post_serializer = PostSerializer.new(@post, { custom_options: true }) @author_serializer = AuthorSerializer.new(@author) @comment_serializer = CommentSerializer.new(@comment) end @@ -74,7 +74,7 @@ def test_has_many_with_no_serializer assert_equal key, :tags assert_equal serializer, nil - assert_equal [{ attributes: { name: '#hashtagged' }}].to_json, options[:virtual_value].to_json + assert_equal [{ attributes: { name: '#hashtagged' } }].to_json, options[:virtual_value].to_json end end diff --git a/test/serializers/attribute_test.rb b/test/serializers/attribute_test.rb index ab19a11fd..c0b2e30bb 100644 --- a/test/serializers/attribute_test.rb +++ b/test/serializers/attribute_test.rb @@ -10,19 +10,19 @@ def setup def test_attributes_definition assert_equal([:id, :title], - @blog_serializer.class._attributes) + @blog_serializer.class._attributes) end def test_json_serializable_hash adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer) - assert_equal({blog: { id:1, title:'AMS Hints'}}, adapter.serializable_hash) + assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash) end def test_attribute_inheritance_with_key inherited_klass = Class.new(AlternateBlogSerializer) blog_serializer = inherited_klass.new(@blog) adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer) - assert_equal({:id=>1, :title=>'AMS Hints'}, adapter.serializable_hash) + assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash) end def test_multiple_calls_with_the_same_attribute @@ -52,7 +52,7 @@ def test_type_attribute end adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog)) - assert_equal({ blog: { type: 1} }, adapter.serializable_hash) + assert_equal({ blog: { type: 1 } }, adapter.serializable_hash) adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog)) assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash) diff --git a/test/serializers/attributes_test.rb b/test/serializers/attributes_test.rb index 4ce85f120..50a42c2d0 100644 --- a/test/serializers/attributes_test.rb +++ b/test/serializers/attributes_test.rb @@ -15,18 +15,17 @@ def setup def test_attributes_definition assert_equal([:name, :description], - @profile_serializer.class._attributes) + @profile_serializer.class._attributes) end def test_attributes_with_fields_option - assert_equal({name: 'Name 1'}, - @profile_serializer.attributes(fields: [:name])) + assert_equal({ name: 'Name 1' }, + @profile_serializer.attributes(fields: [:name])) end def test_required_fields - assert_equal({name: 'Name 1', description: 'Description 1'}, - @profile_serializer.attributes(fields: [:name, :description], required_fields: [:name])) - + assert_equal({ name: 'Name 1', description: 'Description 1' }, + @profile_serializer.attributes(fields: [:name, :description], required_fields: [:name])) end def test_attributes_inheritance_definition @@ -35,8 +34,8 @@ def test_attributes_inheritance_definition def test_attributes_inheritance serializer = @serializer_klass.new(@comment) - assert_equal({id: 1, body: 'ZOMG!!'}, - serializer.attributes) + assert_equal({ id: 1, body: 'ZOMG!!' }, + serializer.attributes) end def test_attribute_inheritance_with_new_attribute_definition @@ -46,8 +45,8 @@ def test_attribute_inheritance_with_new_attribute_definition def test_attribute_inheritance_with_new_attribute serializer = @serializer_klass_with_new_attributes.new(@comment) - assert_equal({id: 1, body: 'ZOMG!!', date: '2015', likes: nil}, - serializer.attributes) + assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil }, + serializer.attributes) end def test_multiple_calls_with_the_same_attribute diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index 703316fbf..5b7a665de 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -58,9 +58,9 @@ def test_default_cache_key_fallback end def test_cache_options_definition - assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options) + assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options) assert_equal(nil, @blog_serializer.class._cache_options) - assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options) + assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options) end def test_fragment_cache_definition @@ -118,7 +118,7 @@ def test_fragment_fetch_with_virtual_associations hash = render_object_with_cache(@location) assert_equal(hash, expected_result) - assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key)) + assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key)) end def test_uses_file_digest_in_cache_key @@ -155,6 +155,7 @@ def test_digest_caller_file end private + def render_object_with_cache(obj) ActiveModel::SerializableResource.new(obj).serializable_hash end diff --git a/test/serializers/fieldset_test.rb b/test/serializers/fieldset_test.rb index 054391779..ca613045d 100644 --- a/test/serializers/fieldset_test.rb +++ b/test/serializers/fieldset_test.rb @@ -3,12 +3,11 @@ module ActiveModel class Serializer class FieldsetTest < Minitest::Test - def test_fieldset_with_hash - fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']}) + fieldset = ActiveModel::Serializer::Fieldset.new({ 'post' => %w(id title), 'coment' => ['body'] }) assert_equal( - {:post=>[:id, :title], :coment=>[:body]}, + { :post => [:id, :title], :coment => [:body] }, fieldset.fields ) end @@ -17,7 +16,7 @@ def test_fieldset_with_array_of_fields_and_root_name fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post') assert_equal( - {:post => [:title]}, + { :post => [:title] }, fieldset.fields ) end diff --git a/test/serializers/meta_test.rb b/test/serializers/meta_test.rb index 833e891dd..976dc52f0 100644 --- a/test/serializers/meta_test.rb +++ b/test/serializers/meta_test.rb @@ -12,7 +12,7 @@ def setup end def test_meta_is_present_with_root - serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}) + serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { blog: { @@ -28,7 +28,7 @@ def test_meta_is_present_with_root def test_meta_is_not_included_when_root_is_missing # load_adapter uses FlattenJson Adapter - adapter = load_adapter(meta: {total: 10}) + adapter = load_adapter(meta: { total: 10 }) expected = { id: 1, title: 'AMS Hints' @@ -37,7 +37,7 @@ def test_meta_is_not_included_when_root_is_missing end def test_meta_key_is_used - serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: 'haha_meta') + serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta') adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { blog: { @@ -52,7 +52,7 @@ def test_meta_key_is_used end def test_meta_key_is_used_with_json_api - serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: 'haha_meta') + serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta') adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) expected = { data: { @@ -66,7 +66,7 @@ def test_meta_key_is_used_with_json_api end def test_meta_is_not_present_on_arrays_without_root - serializer = ArraySerializer.new([@blog], meta: {total: 10}) + serializer = ArraySerializer.new([@blog], meta: { total: 10 }) # FlattenJSON doesn't have support to root adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer) expected = [{ @@ -86,7 +86,7 @@ def test_meta_is_not_present_on_arrays_without_root end def test_meta_is_present_on_arrays_with_root - serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: 'haha_meta') + serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta') # JSON adapter adds root by default adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = { diff --git a/test/serializers/root_test.rb b/test/serializers/root_test.rb index 749ff0a7f..03e6ca20f 100644 --- a/test/serializers/root_test.rb +++ b/test/serializers/root_test.rb @@ -3,13 +3,12 @@ module ActiveModel class Serializer class RootTest < Minitest::Test - def setup @virtual_value = VirtualValue.new(id: 1) end def test_overwrite_root - serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'}) + serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' }) assert_equal('smth', serializer.json_key) end @@ -17,7 +16,6 @@ def test_underscore_in_root serializer = VirtualValueSerializer.new(@virtual_value) assert_equal('virtual_value', serializer.json_key) end - end end end diff --git a/test/serializers/urls_test.rb b/test/serializers/urls_test.rb index 05b33fa5a..d0dc26f16 100644 --- a/test/serializers/urls_test.rb +++ b/test/serializers/urls_test.rb @@ -3,7 +3,6 @@ module ActiveModel class Serializer class UrlsTest < Minitest::Test - def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @post = Post.new({ title: 'New Post', body: 'Body' }) diff --git a/test/test_helper.rb b/test/test_helper.rb index 87ae02866..980b04f8c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,7 +13,6 @@ # Ensure backward compatibility with Minitest 4 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) - require 'capture_warnings' @capture_warnings = CaptureWarnings.new(fail_build = true) @capture_warnings.before_tests From 8e8f6aba7e650aadecfacca79e54221c53080e43 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 3 Sep 2015 20:55:40 -0500 Subject: [PATCH 4/6] Remove space in {} --- test/generators/scaffold_controller_generator_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/generators/scaffold_controller_generator_test.rb b/test/generators/scaffold_controller_generator_test.rb index f3029e218..aabe6122f 100644 --- a/test/generators/scaffold_controller_generator_test.rb +++ b/test/generators/scaffold_controller_generator_test.rb @@ -1,5 +1,4 @@ require 'test_helper' -# require 'active_model/serializer/railtie' class ResourceGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../../../tmp/generators', __FILE__) @@ -19,6 +18,6 @@ def test_serializer_file_is_generated def copy_routes config_dir = File.join(destination_root, 'config') FileUtils.mkdir_p(config_dir) - File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }') + File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw {}') end end From 1d15de4f8f02e6dda304ed3da710566401308b44 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 3 Sep 2015 22:56:04 -0500 Subject: [PATCH 5/6] Skip checking style in tmp --- .rubocop.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index d2d21b50f..e7d729cc0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,7 +3,7 @@ inherit_from: .rubocop_todo.yml AllCops: Exclude: - config/initializers/forbidden_yaml.rb - - !ruby/regexp /(vendor|bundle|bin|db)\/.*/ + - !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/ RunRailsCops: true DisplayCopNames: true DisplayStyleGuide: true From c39c20d4a4e20a6b4e75fffdfb3499b082ec0c45 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Fri, 4 Sep 2015 02:32:16 -0500 Subject: [PATCH 6/6] Add no-op rubocop for rbx --- Rakefile | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Rakefile b/Rakefile index f7e165185..cc454b668 100644 --- a/Rakefile +++ b/Rakefile @@ -6,10 +6,19 @@ begin rescue LoadError else Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop) - desc 'Execute rubocop' - RuboCop::RakeTask.new(:rubocop) do |task| - task.options = ['--rails', '--display-cop-names', '--display-style-guide'] - task.fail_on_error = true + if !defined?(::Rubinius) + Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop) + desc 'Execute rubocop' + RuboCop::RakeTask.new(:rubocop) do |task| + task.options = ['--rails', '--display-cop-names', '--display-style-guide'] + task.fail_on_error = true + end + else + desc 'No-op rubocop to avoid rbx segfault' + task :rubocop do + puts 'Skipping rubocop on rbx due to segfault' + puts 'https://github.com/rubinius/rubinius/issues/3499' + end end end