Skip to content

Commit

Permalink
style(Rails/Delegate): safe autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsavitskiy committed May 4, 2023
1 parent 61ab07e commit 57b7de4
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 132 deletions.
21 changes: 0 additions & 21 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions lib/mongoid/association/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down
24 changes: 1 addition & 23 deletions lib/mongoid/association/referenced/has_many/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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.
#
Expand Down Expand Up @@ -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<Mongoid::Document> ] The unique documents.
def uniq
entries.uniq
end

private

def set_base(document)
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/association/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
20 changes: 5 additions & 15 deletions lib/mongoid/clients/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/contextual/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/contextual/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
#
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/extensions/nil_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/extensions/symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 1 addition & 9 deletions lib/mongoid/fields/foreign_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 2 additions & 22 deletions lib/mongoid/findable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down Expand Up @@ -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?
Expand Down
4 changes: 1 addition & 3 deletions lib/mongoid/scopable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57b7de4

Please sign in to comment.