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..228c2b96b 100644 --- a/lib/mongoid/association/many.rb +++ b/lib/mongoid/association/many.rb @@ -173,9 +173,7 @@ def serializable_hash(options = {}) # person.addresses.unscoped # # @return [ Mongoid::Criteria ] The unscoped criteria. - def unscoped - criteria.unscoped - end + delegate :unscoped, to: :criteria private diff --git a/lib/mongoid/association/referenced/has_many/enumerable.rb b/lib/mongoid/association/referenced/has_many/enumerable.rb index 1599937df..757c38741 100644 --- a/lib/mongoid/association/referenced/has_many/enumerable.rb +++ b/lib/mongoid/association/referenced/has_many/enumerable.rb @@ -295,9 +295,7 @@ def include?(doc) # enumerable.inspect # # @return [ String ] The inspected enum. - def inspect - entries.inspect - end + delegate :inspect, to: :entries # Return all the documents in the enumerable that have been _loaded or # _added. @@ -463,9 +461,7 @@ def as_json(options = {}) # enumerable.uniq # # @return [ Array ] The unique documents. - def uniq - entries.uniq - end + delegate :uniq, to: :entries private 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..2a22f1ac3 100644 --- a/lib/mongoid/fields/foreign_key.rb +++ b/lib/mongoid/fields/foreign_key.rb @@ -117,9 +117,7 @@ def object_id_field? # field.resizable? # # @return [ true | false ] If the field is resizable. - def resizable? - type.resizable? - end + delegate :resizable?, to: :type private diff --git a/lib/mongoid/findable.rb b/lib/mongoid/findable.rb index a29a47bc9..6d8ebe7a4 100644 --- a/lib/mongoid/findable.rb +++ b/lib/mongoid/findable.rb @@ -71,9 +71,7 @@ module Findable # Person.where(title: "Sir").count # # @return [ Integer ] The number of matching documents. - def count - with_default_scope.count - end + delegate :count, to: :with_default_scope # Returns an estimated count of records in the database. # @@ -81,9 +79,7 @@ def count # Person.estimated_count # # @return [ Integer ] The number of matching documents. - def estimated_count - with_default_scope.estimated_count - end + delegate :estimated_count, to: :with_default_scope # Returns true if count is zero # 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