Skip to content

Commit

Permalink
Fix rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Apr 21, 2024
1 parent 5102e35 commit 4ef4335
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
24 changes: 12 additions & 12 deletions lib/mongoid/clients/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/validatable/associated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -107,7 +105,7 @@ def get_target_documents(target)
# @return [ Array<Mongoid::Document> ] 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
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/associations/has_and_belongs_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 20 additions & 20 deletions spec/mongoid/clients/sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions spec/support/models/name.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true
# rubocop:todo all

class Name
include Mongoid::Document
include Mongoid::Attributes::Dynamic

validate :is_not_jamis

field :_id, type: String, overwrite: true, default: ->{
field :_id, type: String, overwrite: true, default: lambda {
"#{first_name}-#{last_name}"
}

Expand All @@ -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

0 comments on commit 4ef4335

Please sign in to comment.