diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8b95d8ee4..5ce3fddb7 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -699,25 +699,6 @@ Rails/Date: - 'spec/mongoid/persistable/maxable_spec.rb' - 'spec/mongoid/persistable/minable_spec.rb' -# Offense count: 20 -# 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/none.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 9ccb3d2e0..2eaf747e9 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 c408c2770..f6c2c7dea 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 ca6866ab3..d6fcfb971 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 1f8489916..0201d8eb9 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. # @@ -129,9 +127,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. @@ -141,9 +137,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. @@ -153,9 +147,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. @@ -165,9 +157,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 18593f540..f0a784ac5 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 ef842875f..40e810c3c 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/document.rb b/lib/mongoid/document.rb index 31a879448..96d094690 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 da4eff4db..7e938dde9 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 667c730c8..e36312809 100644 --- a/lib/mongoid/extensions/symbol.rb +++ b/lib/mongoid/extensions/symbol.rb @@ -14,7 +14,7 @@ module Symbol # @return [ true | false ] If the symbol is :id or :_id. # @deprecated def mongoid_id? - to_s.mongoid_id? + to_s.mongoid_id? # rubocop:disable Rails/Delegate end Mongoid.deprecate(self, :mongoid_id?) diff --git a/lib/mongoid/fields/foreign_key.rb b/lib/mongoid/fields/foreign_key.rb index 9b6f145fc..1a786267d 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