Skip to content

Consider disabling the `Rails/Delegate` cop

The Rails/Delegate cop suggests using delegate instead of defining a method that only delegates.

We should consider disabling this for a few reasons:

  • def name is more greppable than delegate :foo, :bar, :name, prefix: true, allow_nil: true
  • If the method is overriden in a subclass, it's better to have it implemented explicitly in the parent class to avoid any confusion
  • When prefix: true is used, the generated method is prefixed with the name of the object the method is delegated to. For instance delegate :name, to: :project, prefix: true is equivalent to def project_name, which is confusing, and anti-greppable
  • Even if delegate provides the allow_nil: true option, you can do the same with object&.method
  • When using delegate, the public interface of the class is expanded in a subtle manner. Having the delegation method defined explicitly would make the interface size more obvious and could lead to realize that the class may be doing too much.

/cc @jneen @nick.thomas @rspeicher @brodock @DouweM