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

Scoping reserved names #31179

Merged
merged 1 commit into from Nov 28, 2017

Conversation

Projects
None yet
7 participants
@kinnrot
Contributor

kinnrot commented Nov 18, 2017

Summary

Suggestion for resolving #31120
Don't allow creating scopes named same as ActiveRecord::Relation instance method

@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Nov 18, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

rails-bot commented Nov 18, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Show outdated Hide outdated activerecord/lib/active_record/scoping/named.rb
Show outdated Hide outdated activerecord/test/cases/scoping/named_scoping_test.rb
Show outdated Hide outdated activerecord/test/cases/scoping/named_scoping_test.rb
@kinnrot

This comment has been minimized.

Show comment
Hide comment
@kinnrot

kinnrot Nov 19, 2017

Contributor

@eugeneius 10x, If you already looked at this code, What do you think about a more actionable message when this issue occurs? How about something that explain's how to cancel auto scope generation from enum or any other places that scopes are auto generated on rail load?

Contributor

kinnrot commented Nov 19, 2017

@eugeneius 10x, If you already looked at this code, What do you think about a more actionable message when this issue occurs? How about something that explain's how to cancel auto scope generation from enum or any other places that scopes are auto generated on rail load?

@eugeneius

This comment has been minimized.

Show comment
Hide comment
@eugeneius

eugeneius Nov 19, 2017

Member

It looks like ActiveRecord::Enum has its own logic to reject dangerous methods: https://github.com/rails/rails/blob/v5.1.4/activerecord/lib/active_record/enum.rb#L219

I think we should extend that to cover instance methods on Relation too.

I'm not aware of any other places where Active Record automatically creates scopes for you, so I think adding the enum-specific check should be enough to give users reasonably useful error messages.

Member

eugeneius commented Nov 19, 2017

It looks like ActiveRecord::Enum has its own logic to reject dangerous methods: https://github.com/rails/rails/blob/v5.1.4/activerecord/lib/active_record/enum.rb#L219

I think we should extend that to cover instance methods on Relation too.

I'm not aware of any other places where Active Record automatically creates scopes for you, so I think adding the enum-specific check should be enough to give users reasonably useful error messages.

@kinnrot

This comment has been minimized.

Show comment
Hide comment
@kinnrot

kinnrot Nov 21, 2017

Contributor

@eugeneius sounds right, I added the check, so what is the way to tell rails to not create a scope for a enum field named 'record' for example ?

Contributor

kinnrot commented Nov 21, 2017

@eugeneius sounds right, I added the check, so what is the way to tell rails to not create a scope for a enum field named 'record' for example ?

@eugeneius

This comment has been minimized.

Show comment
Hide comment
@eugeneius

eugeneius Nov 22, 2017

Member

Nice one 😊 could you add a test for the enum validation too?

It's not currently possible to suppress enum scopes; that sounds like a reasonable addition to me, but I think you should wait for a maintainer to review what you have here already and then follow up with a separate pull request afterwards if they're 👍 on the idea.

Member

eugeneius commented Nov 22, 2017

Nice one 😊 could you add a test for the enum validation too?

It's not currently possible to suppress enum scopes; that sounds like a reasonable addition to me, but I think you should wait for a maintainer to review what you have here already and then follow up with a separate pull request afterwards if they're 👍 on the idea.

@rafaelfranca

Can you add a CHANGELOG entry and squash your commits after it?

Show outdated Hide outdated activerecord/test/cases/enum_test.rb
Show outdated Hide outdated activerecord/lib/active_record/enum.rb
Show outdated Hide outdated activerecord/CHANGELOG.md
Show outdated Hide outdated activerecord/lib/active_record/enum.rb
@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth
Member

kaspth commented Nov 25, 2017

@rails-bot rails-bot assigned rafaelfranca and unassigned kaspth Nov 25, 2017

Fixes #31120.
*kinnrot*

This comment has been minimized.

@eugeneius

eugeneius Nov 26, 2017

Member

New changelog entries should be added at the top of the file.

@eugeneius

eugeneius Nov 26, 2017

Member

New changelog entries should be added at the top of the file.

Show outdated Hide outdated activerecord/CHANGELOG.md
@kinnrot

This comment has been minimized.

Show comment
Hide comment
@kinnrot

kinnrot Nov 27, 2017

Contributor
Contributor

kinnrot commented Nov 27, 2017

@kinnrot

This comment has been minimized.

Show comment
Hide comment
@kinnrot

kinnrot Nov 28, 2017

Contributor

The changelog thing is good now.

Contributor

kinnrot commented Nov 28, 2017

The changelog thing is good now.

@@ -1,5 +1,11 @@
## Rails 5.2.0.beta1 (November 27, 2017) ##

This comment has been minimized.

@eugeneius

eugeneius Nov 28, 2017

Member

So close 😄 but the new changelog entry needs to be above this line, at the very top of the file.

Rails 5.2.0.beta1 has already been released, so it doesn't make sense to include this change under that heading.

@eugeneius

eugeneius Nov 28, 2017

Member

So close 😄 but the new changelog entry needs to be above this line, at the very top of the file.

Rails 5.2.0.beta1 has already been released, so it doesn't make sense to include this change under that heading.

@kinnrot

This comment has been minimized.

Show comment
Hide comment
@kinnrot

kinnrot Nov 28, 2017

Contributor
Contributor

kinnrot commented Nov 28, 2017

Prevent scope named same as a ActiveRecord::Relation instance method.
Due to inconsistent behavior when chaining scopes and one scope named after a Relation method

Validation code added in 2 places:

- scope, to prevent problematic scope names.
- enum, cause it tries to auto define scope.

@rafaelfranca rafaelfranca merged commit 6552ce0 into rails:master Nov 28, 2017

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Nov 28, 2017

@hw676018683

This comment has been minimized.

Show comment
Hide comment
@hw676018683

hw676018683 Apr 9, 2018

when I write the following code:

  class Attachemnt < ApplicationRecord
    enum extension: {
      unknown: 0,
      image: 1,
      video: 2,
      audio: 3
    }
  end

Get the following error:

You tried to define an enum named "extension" on the model "Attachment", but this will generate a class method "extensions", which is already defined by ActiveRecord::Relation. (ArgumentError)

But I never use something like Attachment.all.extensions. So I think it may be more appropriate to only limit scope.
detect_enum_conflict!(name, name.pluralize, true), the class method name.pluralize should not be limited.

hw676018683 commented Apr 9, 2018

when I write the following code:

  class Attachemnt < ApplicationRecord
    enum extension: {
      unknown: 0,
      image: 1,
      video: 2,
      audio: 3
    }
  end

Get the following error:

You tried to define an enum named "extension" on the model "Attachment", but this will generate a class method "extensions", which is already defined by ActiveRecord::Relation. (ArgumentError)

But I never use something like Attachment.all.extensions. So I think it may be more appropriate to only limit scope.
detect_enum_conflict!(name, name.pluralize, true), the class method name.pluralize should not be limited.

@murb murb referenced this pull request Aug 15, 2018

Open

Make scope creation optional #6

kamipo added a commit to kamipo/rails that referenced this pull request Oct 8, 2018

Generate delegation methods to named scope in the definition time
The delegation methods to named scope are defined when `method_missing`
is invoked on the relation.

Since rails#29301, the receiver in the named scope is changed to the relation
like others (e.g. `default_scope`, etc) for consistency.

Most named scopes would be delegated from relation by `method_missing`,
since we don't allow scopes to be defined which conflict with instance
methods on `Relation` (rails#31179). But if a named scope is defined with the
same name as any method on the `superclass` (e.g. `Kernel.open`), the
`method_missing` on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes rails#34098.

kamipo added a commit to kamipo/rails that referenced this pull request Oct 8, 2018

Generate delegation methods to named scope in the definition time
The delegation methods to named scope are defined when `method_missing`
is invoked on the relation.

Since rails#29301, the receiver in the named scope is changed to the relation
like others (e.g. `default_scope`, etc) for consistency.

Most named scopes would be delegated from relation by `method_missing`,
since we don't allow scopes to be defined which conflict with instance
methods on `Relation` (rails#31179). But if a named scope is defined with the
same name as any method on the `superclass` (e.g. `Kernel.open`), the
`method_missing` on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes rails#34098.

kamipo added a commit to kamipo/rails that referenced this pull request Oct 9, 2018

Generate delegation methods to named scope in the definition time
The delegation methods to named scope are defined when `method_missing`
is invoked on the relation.

Since rails#29301, the receiver in the named scope is changed to the relation
like others (e.g. `default_scope`, etc) for consistency.

Most named scopes would be delegated from relation by `method_missing`,
since we don't allow scopes to be defined which conflict with instance
methods on `Relation` (rails#31179). But if a named scope is defined with the
same name as any method on the `superclass` (e.g. `Kernel.open`), the
`method_missing` on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes rails#34098.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment