Skip to content

Commit

Permalink
Fix remaining performance cop (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields authored Apr 21, 2023
1 parent 724d24e commit 34151e4
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 44 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ Layout/EmptyLinesAroundModuleBody:
Style/IfUnlessModifier:
Enabled: false

Performance/CollectionLiteralInLoop:
Exclude:
- 'spec/mongoid/association/embedded/embeds_many/proxy_spec.rb'
- 'spec/mongoid/association/referenced/has_many/proxy_spec.rb'
- 'spec/mongoid/attributes_spec.rb'
- 'spec/mongoid/criteria/queryable/selectable_spec.rb'
- 'spec/mongoid/criteria/queryable/selector_spec.rb'

# --------------------------------------------------
# These cops are intentionally disabled due to incompatibility.

Expand Down
27 changes: 1 addition & 26 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 1000`
# on 2023-04-20 17:16:28 UTC using RuboCop version 1.50.0.
# on 2023-04-20 23:45:43 UTC using RuboCop version 1.50.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
Expand Down Expand Up @@ -62,31 +62,6 @@ Naming/MemoizedInstanceVariableName:
- 'lib/mongoid/document.rb'
- 'lib/mongoid/traversable.rb'

# Offense count: 1
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Exclude:
- 'lib/mongoid/traversable.rb'

# Offense count: 16
# Configuration parameters: MinSize.
Performance/CollectionLiteralInLoop:
Exclude:
- 'lib/mongoid/config/encryption.rb'
- 'lib/mongoid/interceptable.rb'
- 'lib/mongoid/matcher/field_expression.rb'
- 'spec/integration/app_spec.rb'
- 'spec/mongoid/association/embedded/embeds_many/proxy_spec.rb'
- 'spec/mongoid/association/referenced/has_many/proxy_spec.rb'
- 'spec/mongoid/attributes_spec.rb'
- 'spec/mongoid/changeable_spec.rb'
- 'spec/mongoid/clients/transactions_spec.rb'
- 'spec/mongoid/contextual/aggregable/memory_table_spec.rb'
- 'spec/mongoid/criteria/queryable/selectable_spec.rb'
- 'spec/mongoid/criteria/queryable/selector_spec.rb'
- 'spec/mongoid/interceptable_spec_models.rb'

# Offense count: 25
RSpec/AnyInstance:
Exclude:
Expand Down
7 changes: 6 additions & 1 deletion lib/mongoid/config/encryption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def encryption_schema_map(database, models = ::Mongoid.models)
ActiveSupport::TimeWithZone => 'date'
}.freeze

# The base encryption properties for embedded relations.
#
# @api private
RELATION_PROPERTIES_BASE = { 'bsonType' => 'object' }.freeze

# Generate the encryptMetadata object for the provided model.
#
# @param [ Mongoid::Document ] model The model to generate the metadata for.
Expand Down Expand Up @@ -157,7 +162,7 @@ def properties_for_relations(model, visited)
).merge(
properties_for(relation.relation_class, visited)
).tap do |properties|
props[name] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
props[name] = RELATION_PROPERTIES_BASE.merge(properties) unless properties.empty?
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module Interceptable
before_validation
].freeze

# Callback methods which apply defaults to models.
#
# @api private
APPLY_DEFAULTS = %i[apply_defaults apply_post_processed_defaults].freeze

included do
extend ActiveModel::Callbacks
include ActiveModel::Validations::Callbacks
Expand Down Expand Up @@ -189,7 +194,7 @@ def pending_callbacks=(value)
# @api private
def run_pending_callbacks
pending_callbacks.each do |cb|
if %i[apply_defaults apply_post_processed_defaults].include?(cb)
if APPLY_DEFAULTS.include?(cb)
send(cb)
else
run_callbacks(cb, with_children: false)
Expand Down
5 changes: 4 additions & 1 deletion lib/mongoid/matcher/field_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module FieldExpression

extend self

# @api private
DOLLAR_REGEX_OPTIONS = %w[$regex $options].freeze

# Returns whether a value satisfies a condition.
#
# @param [ true | false ] exists Whether the value exists.
Expand All @@ -26,7 +29,7 @@ def matches?(exists, value, condition)
condition.all? do |k, cond_v|
k = k.to_s
if k.start_with?('$')
if %w[$regex $options].include?(k)
if DOLLAR_REGEX_OPTIONS.include?(k)
unless condition.key?('$regex')
raise Errors::InvalidQuery.new("$regex is required if $options is given: #{Errors::InvalidQuery.truncate_expr(condition)}")
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/traversable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def _parent

# Sets the parent document.
#
# @param [ Mongoid::Document ] p The parent document to set.
# @param [ Mongoid::Document ] value The parent document to set.
#
# @returns [ Mongoid::Document ] The parent document.
#
# @api private
def _parent=(p)
@__parent = p
def _parent=(value)
@__parent = value
end

# Module used for prepending to the various discriminator_*= methods
Expand Down
4 changes: 3 additions & 1 deletion spec/integration/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ def install_rails

APP_PATH = File.join(File.dirname(__FILE__), '../../test-apps/rails-api')

autoloaders = %w[classic zeitwerk]

%w[development production].each do |rails_env|
context "in #{rails_env}" do

%w[classic zeitwerk].each do |autoloader|
autoloaders.each do |autoloader|
context "with #{autoloader} autoloader" do

let(:env) do
Expand Down
6 changes: 4 additions & 2 deletions spec/mongoid/changeable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2041,8 +2041,9 @@
end

after do
callback_kinds = %i[before after].freeze
Acolyte._save_callbacks.select do |callback|
%i[before after].include?(callback.kind)
callback_kinds.include?(callback.kind)
end.each do |callback|
Acolyte._save_callbacks.delete(callback)
end
Expand Down Expand Up @@ -2072,8 +2073,9 @@
end

after do
callback_kinds = %i[before after].freeze
Acolyte._save_callbacks.select do |callback|
%i[before after].include?(callback.kind)
callback_kinds.include?(callback.kind)
end.each do |callback|
Acolyte._save_callbacks.delete(callback)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/mongoid/clients/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def capture_exception
end

let(:other_events) do
subscriber.started_events.reject { |event| %w[insert update].include?(event.command_name) }
skip_commands = %w[insert update]
subscriber.started_events.reject { |event| skip_commands.include?(event.command_name) }
end

context 'when a transaction is used on a model class' do
Expand Down
10 changes: 6 additions & 4 deletions spec/mongoid/contextual/aggregable/memory_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
else
YAML.safe_load(file, permitted_classes: [BigDecimal])
end.deep_symbolize_keys.fetch(:sets)
field_map = { integer: :views,
float: :rating,
big_decimal: :sales }
methods = %i[sum avg min max]

table.each do |name, spec|
context "DB values are #{name}" do
Expand All @@ -29,11 +33,9 @@
end
end

{ integer: :views,
float: :rating,
big_decimal: :sales }.each do |type, field|
field_map.each do |type, field|

%i[sum avg min max].each do |method|
methods.each do |method|
context "#{type.to_s.camelize} field :#{method}" do
let(:expected) do
spec.dig(:expected, type, method)
Expand Down
7 changes: 3 additions & 4 deletions spec/mongoid/interceptable_spec_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ module CallbackTracking
extend ActiveSupport::Concern

included do
%i[
validation save create update
].each do |what|
%i[before after].each do |whn|
whens = %i[before after]
%i[validation save create update].each do |what|
whens.each do |whn|
send("#{whn}_#{what}", "#{whn}_#{what}_stub".to_sym)
define_method("#{whn}_#{what}_stub") do
callback_registry&.record_call(self.class, "#{whn}_#{what}".to_sym)
Expand Down

0 comments on commit 34151e4

Please sign in to comment.