-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Blacklist ruby keywords for scopes #14582
Conversation
@@ -29,6 +29,8 @@ def self.set_name_cache(name, value) | |||
end | |||
} | |||
|
|||
BLACKLISTED_CLASS_METHODS = %w(private public protected alias and BEGIN begin break case class def defined? do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield).freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not just if I should break this in multiple lines or not. Let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to break the lines. Also we don't need to freeze
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
cc @tenderlove |
Do we have to hardcode that list? Can't we get that dynamically from somewhere? Like |
@fw42 this list is not only about method on Class, it is also about some ruby keywords. I think it is fine to use a hardcoded list. |
public/protected/private are not keywords in Ruby as far as I know |
Can we add the test cases to the enum tests as well? 😄 |
@fw42 this is why I said "not only about" |
Seems good, some comments:
>> class A
>> def rescue
>> puts 'lol'
>> end
>>
?> begin
?> raise
>> rescue
>> puts 'omg'
>> end
>> end
omg
=> nil
>> A.rescue
lol
=> nil When you call it on the class as an explicit receiver it resolves correctly (the common usage for a scope), and when you use it without the receiver the keyword takes precedence (the common usage for the rescue keyword). Not that I would recommend defining a scope with those names, but since this is supposed to be a "helpful" feature to help you discover problems early on, perhaps we can start with the most dangerous scenario so we don't stop people from upgrading over the unlikely problems. Not a huge deal though as I don't think anyone will actually define a scope called |
@chancancode Enum tests added . About what it should be on the list, I am on the fence about it. Let me know, I can change the list content. |
@rafaelfranca thoughts? If you don't feel strongly about this either we can probably just keep it as is and merge On Thu, Apr 3, 2014 at 12:13 PM, Arthur Nogueira Neves
|
@arthurnn I think is better to trim down this list a little. You can check which method would be problematic using the same code @chancancode used in the above comment. WDYT? |
I'm more concerned to release a patch release only to remove methods from this list. |
@rafaelfranca make sense. TBH, keywords doesnt need to be in that list, as @chancancode showed in his code... I will have only |
Seems good. |
Add tests to make sure scopes cannot be create with names such as: private, protected, public. Make sure enum values don't collide with those methods too.
Done. |
Blacklist ruby keywords for scopes
Blacklist ruby keywords for scopes
Blacklist ruby keywords for scopes Conflicts: activerecord/CHANGELOG.md
This broke my app, which has a couple of I've fixed it, but I was disappointed to see a non-essential change like this slip in after RC and days before final release. (And more so because there is no rationale expressed here or in the commit message for why this change is a good idea; |
Thanks for the link! On Thursday, April 10, 2014, Arthur Nogueira Neves notifications@github.com
|
Good call @wincent, for the future, it would be nice to include the rationale in the commit message for these kind of changes, so that when someone git blame and found the commit they'll know what's going on, I'll keep that in mind when writing commit messages (and reviewing commits) in the future! In this case, this is dangerous because things would appear to work on the surface but could be strangely broken inside, and it could be very non-obvious and tricky to debug. For example: class Posts < AR::Base
enum visibility: [:public, :private]
private
def dont_call_me
...
end
end
Post.new.dont_call_me # <- it works, huh. |
Avoid collision with `public` class method. As of Rails 4.1, `public` is explicitly black-listed as a scope name: rails/rails#14582
Avoid collision with `public` class method. As of Rails 4.1, `public` is explicitly black-listed as a scope name: rails/rails#14582
Avoid collision with `public` class method. As of Rails 4.1, `public` is explicitly black-listed as a scope name: rails/rails#14582
review @rafaelfranca @chancancode