From 57b7de4994feaf832e9c8f26fe3484c5326b0f61 Mon Sep 17 00:00:00 2001 From: Anton Savitskiy Date: Thu, 4 May 2023 11:52:43 +0300 Subject: [PATCH] style(Rails/Delegate): safe autocorrect --- .rubocop_todo.yml | 21 ---------------- lib/mongoid/association/many.rb | 13 +--------- .../referenced/has_many/enumerable.rb | 24 +------------------ lib/mongoid/association/relatable.rb | 4 +--- lib/mongoid/clients.rb | 4 +--- lib/mongoid/clients/options.rb | 20 ++++------------ lib/mongoid/contextual/memory.rb | 4 +--- lib/mongoid/contextual/none.rb | 4 +--- lib/mongoid/criteria.rb | 4 +--- lib/mongoid/document.rb | 8 ++----- lib/mongoid/extensions/nil_class.rb | 4 +--- lib/mongoid/extensions/symbol.rb | 4 +--- lib/mongoid/fields/foreign_key.rb | 10 +------- lib/mongoid/findable.rb | 24 ++----------------- lib/mongoid/scopable.rb | 4 +--- 15 files changed, 20 insertions(+), 132 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8e3ab2c5a..5089499cd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -688,27 +688,6 @@ Rails/Date: - 'spec/mongoid/persistable/maxable_spec.rb' - 'spec/mongoid/persistable/minable_spec.rb' -# Offense count: 22 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforceForPrefixed. -Rails/Delegate: - Exclude: - - 'lib/mongoid/association/many.rb' - - 'lib/mongoid/association/referenced/has_many/enumerable.rb' - - 'lib/mongoid/association/relatable.rb' - - 'lib/mongoid/clients.rb' - - 'lib/mongoid/clients/options.rb' - - 'lib/mongoid/contextual/memory.rb' - - 'lib/mongoid/contextual/mongo.rb' - - 'lib/mongoid/contextual/none.rb' - - 'lib/mongoid/criteria.rb' - - 'lib/mongoid/document.rb' - - 'lib/mongoid/extensions/nil_class.rb' - - 'lib/mongoid/extensions/symbol.rb' - - 'lib/mongoid/fields/foreign_key.rb' - - 'lib/mongoid/findable.rb' - - 'lib/mongoid/scopable.rb' - # Offense count: 91 # Configuration parameters: Include. # Include: spec/**/*.rb, test/**/*.rb diff --git a/lib/mongoid/association/many.rb b/lib/mongoid/association/many.rb index 6e8f2f6dc..3cc79baf2 100644 --- a/lib/mongoid/association/many.rb +++ b/lib/mongoid/association/many.rb @@ -9,7 +9,7 @@ class Many < Association::Proxy extend Forwardable include ::Enumerable - def_delegators :criteria, :avg, :max, :min, :sum + def_delegators :criteria, :avg, :max, :min, :sum, :unscoped def_delegators :_target, :length, :size, :any? # Is the association empty? @@ -166,17 +166,6 @@ def serializable_hash(options = {}) _target.map { |document| document.serializable_hash(options) } end - # Get a criteria for the embedded documents without the default scoping - # applied. - # - # @example Get the unscoped criteria. - # person.addresses.unscoped - # - # @return [ Mongoid::Criteria ] The unscoped criteria. - def unscoped - criteria.unscoped - end - private def _session diff --git a/lib/mongoid/association/referenced/has_many/enumerable.rb b/lib/mongoid/association/referenced/has_many/enumerable.rb index 1599937df..34c8376bf 100644 --- a/lib/mongoid/association/referenced/has_many/enumerable.rb +++ b/lib/mongoid/association/referenced/has_many/enumerable.rb @@ -20,6 +20,7 @@ class Enumerable attr_accessor :_added, :_loaded, :_unloaded def_delegators [], :is_a?, :kind_of? + def_delegators :entries, :inspect, :uniq # Check if the enumerable is equal to the other object. # @@ -288,17 +289,6 @@ def include?(doc) _unloaded.exists?(_id: doc._id) || _added.key?(doc._id) end - # Inspection will just inspect the entries for nice array-style - # printing. - # - # @example Inspect the enumerable. - # enumerable.inspect - # - # @return [ String ] The inspected enum. - def inspect - entries.inspect - end - # Return all the documents in the enumerable that have been _loaded or # _added. # @@ -455,18 +445,6 @@ def as_json(options = {}) entries.as_json(options) end - # Return all the unique documents in the enumerable. - # - # @note This operation loads all documents from the database. - # - # @example Get all the unique documents. - # enumerable.uniq - # - # @return [ Array ] The unique documents. - def uniq - entries.uniq - end - private def set_base(document) diff --git a/lib/mongoid/association/relatable.rb b/lib/mongoid/association/relatable.rb index 64fadbe84..0093d7554 100644 --- a/lib/mongoid/association/relatable.rb +++ b/lib/mongoid/association/relatable.rb @@ -222,9 +222,7 @@ def foreign_key_setter # The atomic path for this association. # # @return [ Mongoid::Atomic::Paths::Root ] The atomic path object. - def path(document) - relation.path(document) - end + delegate :path, to: :relation # Gets the setter for the field that sets the type of document on a # polymorphic association. diff --git a/lib/mongoid/clients.rb b/lib/mongoid/clients.rb index 0629e7cee..c4ef6b4de 100644 --- a/lib/mongoid/clients.rb +++ b/lib/mongoid/clients.rb @@ -27,9 +27,7 @@ class << self # Mongoid::Clients.clear # # @return [ Array ] The empty clients. - def clear - clients.clear - end + delegate :clear, to: :clients # Get the default client. # diff --git a/lib/mongoid/clients/options.rb b/lib/mongoid/clients/options.rb index b8de545e4..7e2cdba63 100644 --- a/lib/mongoid/clients/options.rb +++ b/lib/mongoid/clients/options.rb @@ -54,9 +54,7 @@ def collection(parent = nil) # # @return [ String ] The collection name for the current persistence # context. - def collection_name - persistence_context.collection_name - end + delegate :collection_name, to: :persistence_context # Get the database client for the document's current persistence context. # @@ -127,9 +125,7 @@ module ClassMethods # # @return [ String ] The database client name for the current # persistence context. - def client_name - persistence_context.client_name - end + delegate :client_name, to: :persistence_context # Get the collection name for the current persistence context of the # document class. @@ -139,9 +135,7 @@ def client_name # # @return [ String ] The collection name for the current persistence # context. - def collection_name - persistence_context.collection_name - end + delegate :collection_name, to: :persistence_context # Get the database name for the current persistence context of the # document class. @@ -151,9 +145,7 @@ def collection_name # # @return [ String ] The database name for the current persistence # context. - def database_name - persistence_context.database_name - end + delegate :database_name, to: :persistence_context # Get the collection for the current persistence context of the # document class. @@ -163,9 +155,7 @@ def database_name # # @return [ Mongo::Collection ] The collection for the current # persistence context. - def collection - persistence_context.collection - end + delegate :collection, to: :persistence_context # Get the client for the current persistence context of the # document class. diff --git a/lib/mongoid/contextual/memory.rb b/lib/mongoid/contextual/memory.rb index 854098304..bfbf293d3 100644 --- a/lib/mongoid/contextual/memory.rb +++ b/lib/mongoid/contextual/memory.rb @@ -230,9 +230,7 @@ def last! # context.length # # @return [ Integer ] The matching length. - def length - documents.length - end + delegate :length, to: :documents alias_method :size, :length # Limits the number of documents that are returned. diff --git a/lib/mongoid/contextual/none.rb b/lib/mongoid/contextual/none.rb index 6660f4c4a..4521fc8c3 100644 --- a/lib/mongoid/contextual/none.rb +++ b/lib/mongoid/contextual/none.rb @@ -334,9 +334,7 @@ def third_to_last! # context.length # # @return [ Integer ] Always zero. - def length - entries.length - end + delegate :length, to: :entries alias_method :size, :length alias_method :find_first, :first alias_method :one, :first diff --git a/lib/mongoid/criteria.rb b/lib/mongoid/criteria.rb index 2ca68a411..fa2db18dc 100644 --- a/lib/mongoid/criteria.rb +++ b/lib/mongoid/criteria.rb @@ -157,9 +157,7 @@ def embedded? # criteria.extract_id # # @return [ Object ] The id. - def extract_id - selector.extract_id - end + delegate :extract_id, to: :selector # Adds a criterion to the +Criteria+ that specifies additional options # to be passed to the Ruby driver, in the exact format for the driver. diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb index 9ebec281e..24cc45fd3 100644 --- a/lib/mongoid/document.rb +++ b/lib/mongoid/document.rb @@ -57,9 +57,7 @@ def freeze # document.frozen? # # @return [ true | false ] True if frozen, else false. - def frozen? - attributes.frozen? - end + delegate :frozen?, to: :attributes # Delegates to identity in order to allow two records of the same identity # to work with something like: @@ -71,9 +69,7 @@ def frozen? # document.hash # # @return [ Integer ] The hash of the document's identity. - def hash - identity.hash - end + delegate :hash, to: :identity # A Document's is identified absolutely by its class and database id: # diff --git a/lib/mongoid/extensions/nil_class.rb b/lib/mongoid/extensions/nil_class.rb index d23623fd7..55db31615 100644 --- a/lib/mongoid/extensions/nil_class.rb +++ b/lib/mongoid/extensions/nil_class.rb @@ -22,9 +22,7 @@ def __setter__ # nil.collectionize # # @return [ String ] A blank string. - def collectionize - to_s.collectionize - end + delegate :collectionize, to: :to_s end end end diff --git a/lib/mongoid/extensions/symbol.rb b/lib/mongoid/extensions/symbol.rb index e0da88777..3e5a63fb4 100644 --- a/lib/mongoid/extensions/symbol.rb +++ b/lib/mongoid/extensions/symbol.rb @@ -12,9 +12,7 @@ module Symbol # :_id.mongoid_id? # # @return [ true | false ] If the symbol is :id or :_id. - def mongoid_id? - to_s.mongoid_id? - end + delegate :mongoid_id?, to: :to_s module ClassMethods diff --git a/lib/mongoid/fields/foreign_key.rb b/lib/mongoid/fields/foreign_key.rb index aaa27d019..bd4822e2e 100644 --- a/lib/mongoid/fields/foreign_key.rb +++ b/lib/mongoid/fields/foreign_key.rb @@ -111,15 +111,7 @@ def object_id_field? association.polymorphic? ? true : association.klass.using_object_ids? end - # Returns true if an array, false if not. - # - # @example Is the field resizable? - # field.resizable? - # - # @return [ true | false ] If the field is resizable. - def resizable? - type.resizable? - end + def_delegators :type, :resizable? private diff --git a/lib/mongoid/findable.rb b/lib/mongoid/findable.rb index a29a47bc9..7afaab23f 100644 --- a/lib/mongoid/findable.rb +++ b/lib/mongoid/findable.rb @@ -17,10 +17,12 @@ module Findable def_delegators :with_default_scope, :aggregates, :avg, + :count, :create_with, :distinct, :each, :each_with_index, + :estimated_count, :extras, :fifth, :fifth!, @@ -63,28 +65,6 @@ module Findable :update, :update_all - # Returns a count of records in the database. - # If you want to specify conditions use where. - # - # @example Get the count of matching documents. - # Person.count - # Person.where(title: "Sir").count - # - # @return [ Integer ] The number of matching documents. - def count - with_default_scope.count - end - - # Returns an estimated count of records in the database. - # - # @example Get the count of matching documents. - # Person.estimated_count - # - # @return [ Integer ] The number of matching documents. - def estimated_count - with_default_scope.estimated_count - end - # Returns true if count is zero # # @example Are there no saved documents for this model? diff --git a/lib/mongoid/scopable.rb b/lib/mongoid/scopable.rb index a6ae5b939..fff54dc92 100644 --- a/lib/mongoid/scopable.rb +++ b/lib/mongoid/scopable.rb @@ -193,9 +193,7 @@ def unscoped # Model.with_default_scope # # @return [ Mongoid::Criteria ] The criteria. - def with_default_scope - queryable.with_default_scope - end + delegate :with_default_scope, to: :queryable alias_method :criteria, :with_default_scope # Pushes the provided criteria onto the scope stack, and removes it after the