From b27d0a8e9229054edd3af9def8d10b0807a8cb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 14 Oct 2020 12:03:26 +0200 Subject: [PATCH] Add and apply ConstantDefinitionInBlock rule This rule was added in Rubocop 0.91.0. A similar rule named LeakyConstantDeclaration was added in rubocop-rspec 1.34.0. Note using the FILENAMES constant did not result in an offense using the ConstantDefinitionInBlock rule but did result in an offense using the LeakyConstantDeclaration rule. I've simplified the code to get rid of the constant; not sure why we were adding a constant with `||=` in the middle of a spec. --- .rubocop.yml | 3 +++ spec/lib/consul_form_builder_spec.rb | 12 ++++++++---- spec/models/statisticable_spec.rb | 12 ++++++++---- spec/shared/system/nested_documentable.rb | 5 ++--- spec/views/shared/errors_spec.rb | 16 ++++++++++------ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index ac924d8bf..b74689518 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -166,6 +166,9 @@ Layout/TrailingWhitespace: Lint/AmbiguousRegexpLiteral: Enabled: true +Lint/ConstantDefinitionInBlock: + Enabled: true + Lint/DuplicateMethods: Enabled: true diff --git a/spec/lib/consul_form_builder_spec.rb b/spec/lib/consul_form_builder_spec.rb index 91d22ab89..ed73b6b62 100644 --- a/spec/lib/consul_form_builder_spec.rb +++ b/spec/lib/consul_form_builder_spec.rb @@ -1,10 +1,14 @@ require "rails_helper" describe ConsulFormBuilder do - class DummyModel - include ActiveModel::Model - OPTIONS = %w[Good Bad Ugly].freeze - attr_accessor :title, :quality + before do + dummy_model = Class.new do + include ActiveModel::Model + attr_accessor :title, :quality + end + + stub_const("DummyModel", dummy_model) + stub_const("DummyModel::OPTIONS", %w[Good Bad Ugly].freeze) end let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) } diff --git a/spec/models/statisticable_spec.rb b/spec/models/statisticable_spec.rb index 3757d3db8..1cc57ca2a 100644 --- a/spec/models/statisticable_spec.rb +++ b/spec/models/statisticable_spec.rb @@ -1,12 +1,16 @@ require "rails_helper" describe Statisticable do - class DummyStats - include Statisticable + before do + dummy_stats = Class.new do + include Statisticable - def participants - User.all + def participants + User.all + end end + + stub_const("DummyStats", dummy_stats) end let(:stats) { DummyStats.new(nil) } diff --git a/spec/shared/system/nested_documentable.rb b/spec/shared/system/nested_documentable.rb index 9c6482d84..a44096f06 100644 --- a/spec/shared/system/nested_documentable.rb +++ b/spec/shared/system/nested_documentable.rb @@ -235,12 +235,11 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na end login_as user_to_login visit send(path, arguments) - FILENAMES ||= %w[clippy empty logo].freeze send(fill_resource_method_name) if fill_resource_method_name - documentable.class.max_documents_allowed.times.zip(FILENAMES).each do |_n, fn| - documentable_attach_new_file(Rails.root.join("spec/fixtures/files/#{fn}.pdf")) + %w[clippy empty logo].take(documentable.class.max_documents_allowed).each do |filename| + documentable_attach_new_file(Rails.root.join("spec/fixtures/files/#{filename}.pdf")) end click_on submit_button diff --git a/spec/views/shared/errors_spec.rb b/spec/views/shared/errors_spec.rb index 6e3c4ec49..1159f58c7 100644 --- a/spec/views/shared/errors_spec.rb +++ b/spec/views/shared/errors_spec.rb @@ -1,13 +1,17 @@ require "rails_helper" describe "shared errors" do - class DummyModel - include ActiveModel::Model - attr_accessor :title, :description, :days + before do + dummy_model = Class.new do + include ActiveModel::Model + attr_accessor :title, :description, :days - validates :title, presence: true - validates :description, presence: true, length: { in: 10..100 } - validates :days, numericality: { greater_than: 10 } + validates :title, presence: true + validates :description, presence: true, length: { in: 10..100 } + validates :days, numericality: { greater_than: 10 } + end + + stub_const("DummyModel", dummy_model) end it "counts the number of fields with errors" do