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)