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 973de14
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 84 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
4 changes: 1 addition & 3 deletions lib/mongoid/association/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions lib/mongoid/association/referenced/has_many/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -463,9 +461,7 @@ def as_json(options = {})
# enumerable.uniq
#
# @return [ Array<Mongoid::Document> ] The unique documents.
def uniq
entries.uniq
end
delegate :uniq, to: :entries

private

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

Expand Down
8 changes: 2 additions & 6 deletions lib/mongoid/findable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,15 @@ 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.
#
# @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
delegate :estimated_count, to: :with_default_scope

# Returns true if count is zero
#
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 973de14

Please sign in to comment.