Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ReadWriteAttribute cop aware of shadowing methods #585

Merged

Conversation

Drenmi
Copy link
Contributor

@Drenmi Drenmi commented Nov 16, 2021

Background

Sometimes you might want to put a method "in front of" an attribute. In this case, that method will shadow the attribute name, and using self[:foo] within the method will be an infinite loop.

Example:

class Foo < ApplicationModel
  attribute :foo

  def foo
    bar || self[:foo]
  end

  def bar
    nil
  end
end

foo = Foo.new(foo: 1)
#=> (irb):11:in `foo': stack level too deep (SystemStackError)

What is this change?

This PR makes it so that no offense is registered if #read_attribute is used in a method named after that attribute. Same for #write_attribute if called inside a writer method (suffix =) of the same name.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

CHANGELOG.md Outdated Show resolved Hide resolved
@koic
Copy link
Member

koic commented Nov 16, 2021

Can you add this acceptance case to the cop's doc?

@koic
Copy link
Member

koic commented Nov 29, 2021

@Drenmi Ping :-)

@pirj
Copy link
Member

pirj commented Dec 25, 2021

@Drenmi Ping

@Drenmi Drenmi force-pushed the feature/read-write-attribute-shadowing-methods branch 2 times, most recently from 7039a76 to 1d4a2e9 Compare January 4, 2022 07:53
@Drenmi
Copy link
Contributor Author

Drenmi commented Jan 4, 2022

@koic @pirj Thanks for the wait. Rebased and incorporated suggestions. 🙂

@Drenmi Drenmi force-pushed the feature/read-write-attribute-shadowing-methods branch from 1d4a2e9 to 8e0d453 Compare January 5, 2022 03:48
@koic koic merged commit d0e6bcf into rubocop:master Jan 5, 2022
@koic
Copy link
Member

koic commented Jan 5, 2022

Thanks!

@Drenmi Drenmi deleted the feature/read-write-attribute-shadowing-methods branch January 5, 2022 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants