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

Add without_scopes to enum matcher. #1453

Merged
merged 1 commit into from
Jan 31, 2022

Conversation

dewyze
Copy link
Contributor

@dewyze dewyze commented Jul 9, 2021

Rails 6 added the _scopes: false option when defining enums. This will
prevent rails from creating class scopes for each of the values.

Previously, shoulda-matchers checked for the presence of singleton
methods as a way to check that the corresponding enum methods have been
defined. In rails 6, if someone were using _scopes: false the matcher
will fail.

Instead, we should check on the presence of the instance methods that
are generated: #{value}! and #{value}? as the default checker for
the enum values generating methods.

We can then check on the scopes/singleton methods generated when using
the new without_scopes matcher.

@dewyze
Copy link
Contributor Author

dewyze commented Jul 9, 2021

I am sure this could use some work, particularly around the message wording. But if you are open to this let me know and I can clean it up.

@dewyze
Copy link
Contributor Author

dewyze commented Jul 9, 2021

I am also unsure how to structure this so it only tests for rails 6 on this test. I couldn't find examples of that (based on grepping the words rails or 6 in the unit tests.

@vsppedro
Copy link
Collaborator

vsppedro commented Jul 9, 2021

Hi, @dewyze, I hope you are doing fine.

I think this PR can help you: https://github.com/thoughtbot/shoulda-matchers/pull/1429/files

As you can see there was a method to check if the rails version supported active storage before running the tests. You can do something similar.

Or maybe this PR can help you: https://github.com/thoughtbot/shoulda-matchers/pull/1428/files.

Take a closer look on the spec/support/unit/helpers/rails_versions.rb. I think you will need a similar method to check if Rails is greater than 5.2. As you can see the tests are failing when running with this version.

@dewyze
Copy link
Contributor Author

dewyze commented Jul 14, 2021

@vsppedro tests are passing, and I added a test that was missing. I had a hard time figuring out the cleanest way to handle the messages. I am also not sure if there needs to be updates with the messages. Both the instance methods and class scopes can have prefixes or suffixes, so it did seem to make sense to include that messaging in both.

Additionally, I felt torn about the exclude_scopes? method. It felt weird to define a method in the negative. But in most cases I am checking for a negative, and it felt equally weird to have a include_scopes method but to have all uses be !include_scopes.

Nevertheless, best I can tell this includes all use cases and does work. Let me know if there is any cleanup you'd like to see.

@vsppedro
Copy link
Collaborator

Hi, @dewyze, I hope you're doing fine.

In case you don't have the time to continue working on this PR, I can help.

What do you think?

@dewyze dewyze force-pushed the add_without_scopes_to_enum_matcher branch from 16abb59 to 60d77be Compare January 14, 2022 12:24
@dewyze dewyze changed the base branch from master to main January 14, 2022 12:25
@dewyze
Copy link
Contributor Author

dewyze commented Jan 14, 2022

@vsppedro sorry about the long silence! I fixed the rubocop errors, but the 2.6/2.7 errors were too old to see. If you can run the workflows I can take a look at the errors and get them fixed up.

@vsppedro
Copy link
Collaborator

vsppedro commented Jan 14, 2022

No Problem. 👍🏽

@dewyze
Copy link
Contributor Author

dewyze commented Jan 16, 2022

@vsppedro the issue was that I was adding the scopes parameter in the builder, but not checking the rails version, so it would fail on rails 5 versions. I updated it.

@vsppedro vsppedro self-requested a review January 18, 2022 17:24
_suffix: suffix,
}

if rails_version =~ '~> 6.0'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dewyze, I'm wondering if this should be rails_version >= '6.0' instead? I would imagine Rails 7 has the same change as Rails 6 did.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcmire annoyingly, in rails 7, they removed the leading underscore. https://api.rubyonrails.org/classes/ActiveRecord/Enum.html

So this won't work for rails 7, but there are no appraisals yet, so i wasn't going to worry about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no problem then!

@vsppedro
Copy link
Collaborator

Thank you for your contribution!

@vsppedro
Copy link
Collaborator

What do you think about squashing the commits into one?

Rails 6 added the `_scopes: false` option when defining enums. This will
prevent rails from creating class scopes for each of the values.

Previously, shoulda-matchers checked for the presence of singleton
methods as a way to check that the corresponding enum methods have been
defined. In rails 6, if someone were using `_scopes: false` the matcher
will fail.

Instead, we should check on the presence of the instance methods that
are generated: `#{value}!` and `#{value}?` as the default checker for
the enum values generating methods.

We can then check on the scopes/singleton methods generated when using
the new `without_scopes` matcher.
@dewyze dewyze force-pushed the add_without_scopes_to_enum_matcher branch from 121bcbe to 7c559d9 Compare January 31, 2022 23:14
@vsppedro
Copy link
Collaborator

Thank you! ❤️

@vsppedro vsppedro merged commit 38db235 into thoughtbot:main Jan 31, 2022
@vsppedro vsppedro mentioned this pull request Sep 15, 2022
@vsppedro vsppedro mentioned this pull request Jan 1, 2023
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