From 4ef4335b87a78deada842d81d0d222800399aa8b Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Sat, 20 Apr 2024 09:28:13 +0900 Subject: [PATCH] Fix rubocop --- lib/mongoid/clients/sessions.rb | 24 +++++------ lib/mongoid/validatable/associated.rb | 4 +- .../has_and_belongs_to_many_spec.rb | 6 +-- .../referenced/belongs_to/proxy_spec.rb | 2 +- spec/mongoid/clients/sessions_spec.rb | 40 +++++++++---------- spec/support/models/name.rb | 9 ++--- 6 files changed, 41 insertions(+), 44 deletions(-) diff --git a/lib/mongoid/clients/sessions.rb b/lib/mongoid/clients/sessions.rb index 9264b5249..89e3ef191 100644 --- a/lib/mongoid/clients/sessions.rb +++ b/lib/mongoid/clients/sessions.rb @@ -270,22 +270,22 @@ def ensure_client_compatibility! # active. let's see if one of them was started with the model's # client... session = Threaded.get_session(client: persistence_context.client) + return unless session.nil? - # if not, then we have a case of the programmer trying to use + # if the session is nil, then we have a case of the programmer trying to use # a model within a transaction, where the model is not itself # controlled by that transaction. this is potentially a bug, so # let's tell them about it. - if session.nil? - # This is hacky; we're hijacking Mongoid::Errors::MongoidError in - # order to get the spiffy error message translation. If we later - # decide to raise an error instead of just writing a message, we can - # subclass MongoidError and raise that exception here. - message = Errors::MongoidError.new.compose_message( - 'client_session_mismatch', - model: self.class.name - ) - logger.info(message) - end + # + # This is hacky; we're hijacking Mongoid::Errors::MongoidError in + # order to get the spiffy error message translation. If we later + # decide to raise an error instead of just writing a message, we can + # subclass MongoidError and raise that exception here. + message = Errors::MongoidError.new.compose_message( + 'client_session_mismatch', + model: self.class.name + ) + logger.info(message) end end end diff --git a/lib/mongoid/validatable/associated.rb b/lib/mongoid/validatable/associated.rb index c647794aa..bf89043d6 100644 --- a/lib/mongoid/validatable/associated.rb +++ b/lib/mongoid/validatable/associated.rb @@ -81,8 +81,6 @@ def validate_association(document, attribute) document.errors.add(attribute, :invalid) unless valid end - private - # Examine the given target object and return an array of # documents (possibly empty) that the target represents. # @@ -107,7 +105,7 @@ def get_target_documents(target) # @return [ Array ] the in-memory documents # held by the target. def get_target_documents_for_has_many(target) - [ *target._loaded.values, *target._added.values ] + [*target._loaded.values, *target._added.values] end # Returns the target as an array. If the target represents a single diff --git a/spec/integration/associations/has_and_belongs_to_many_spec.rb b/spec/integration/associations/has_and_belongs_to_many_spec.rb index a84e9d2c8..5d03fe1b3 100644 --- a/spec/integration/associations/has_and_belongs_to_many_spec.rb +++ b/spec/integration/associations/has_and_belongs_to_many_spec.rb @@ -47,15 +47,15 @@ class Attachment let(:page) { HabtmSpec::Page.create! } let(:image_block) do - image_block = page.blocks.build({ + page.blocks.build({ _type: 'HabtmSpec::ImageBlock', - attachment_ids: [ attachment.id.to_s ], + attachment_ids: [attachment.id.to_s], attachments_attributes: { '1234' => { file: 'bar.jpg', id: attachment.id.to_s } } }) end it 'does not raise on save' do - expect { image_block.save! }.not_to raise_error + expect { image_block.save! }.to_not raise_error end end end diff --git a/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb b/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb index 3dffb7ad2..dab566c26 100644 --- a/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +++ b/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb @@ -755,7 +755,7 @@ expect(drug).to be_destroyed end - it "doesn't deletes parent" do + it "doesn't delete parent" do expect(person).to_not be_destroyed end diff --git a/spec/mongoid/clients/sessions_spec.rb b/spec/mongoid/clients/sessions_spec.rb index 28eff1011..6619aabd8 100644 --- a/spec/mongoid/clients/sessions_spec.rb +++ b/spec/mongoid/clients/sessions_spec.rb @@ -4,7 +4,25 @@ describe Mongoid::Clients::Sessions do let(:buffer) { StringIO.new } - let(:logger) { ::Logger.new(buffer, Logger::DEBUG) } + let(:logger) { Logger.new(buffer, Logger::DEBUG) } + + let(:subscriber) do + client = Mongoid::Clients.with_name(:other) + monitoring = client.send(:monitoring) + monitoring.subscribers['Command'].find do |s| + s.is_a?(EventSubscriber) + end + end + + let(:insert_events) do + # Driver 2.5 sends command_name as a symbol + subscriber.started_events.select { |event| event.command_name.to_s == 'insert' } + end + + let(:update_events) do + # Driver 2.5 sends command_name as a symbol + subscriber.started_events.select { |event| event.command_name.to_s == 'update' } + end around do |example| old_logger = Mongoid.logger @@ -27,24 +45,6 @@ Mongoid::Clients.clients.delete(:other) end - let(:subscriber) do - client = Mongoid::Clients.with_name(:other) - monitoring = client.send(:monitoring) - monitoring.subscribers['Command'].find do |s| - s.is_a?(EventSubscriber) - end - end - - let(:insert_events) do - # Driver 2.5 sends command_name as a symbol - subscriber.started_events.select { |event| event.command_name.to_s == 'insert' } - end - - let(:update_events) do - # Driver 2.5 sends command_name as a symbol - subscriber.started_events.select { |event| event.command_name.to_s == 'update' } - end - context 'when a session is used on a model class' do context 'when sessions are supported' do @@ -241,7 +241,7 @@ end it 'does not warn about a different client' do - expect(buffer.string).not_to include("used within another client's session") + expect(buffer.string).to_not include("used within another client's session") end end diff --git a/spec/support/models/name.rb b/spec/support/models/name.rb index 9083504b7..e81c24831 100644 --- a/spec/support/models/name.rb +++ b/spec/support/models/name.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -# rubocop:todo all class Name include Mongoid::Document @@ -7,7 +6,7 @@ class Name validate :is_not_jamis - field :_id, type: String, overwrite: true, default: ->{ + field :_id, type: String, overwrite: true, default: lambda { "#{first_name}-#{last_name}" } @@ -30,8 +29,8 @@ def set_parent=(set = false) private def is_not_jamis - if first_name == 'Jamis' && last_name == 'Buck' - errors.add(:base, :invalid) - end + return unless first_name == 'Jamis' && last_name == 'Buck' + + errors.add(:base, :invalid) end end