-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix call context when a preference default is a proc #4721
Fix call context when a preference default is a proc #4721
Conversation
867b550
to
fc02e88
Compare
Codecov Report
@@ Coverage Diff @@
## master #4721 +/- ##
=======================================
Coverage 86.13% 86.13%
=======================================
Files 578 578
Lines 14651 14651
=======================================
+ Hits 12619 12620 +1
+ Misses 2032 2031 -1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
Thanks!! ❤️
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.
Thanks, this is great! I left a question about the testing approach, let me know! ❤️
fc02e88
to
ea53bc9
Compare
I changed the test def preferences
@preferences ||= default_preferences
end to def preferences
@preferences ||= {}
end
def initialize_preference_defaults
@preferences = default_preferences.merge(preferences)
end Because Any thoughts about it ? |
ea53bc9
to
6c17a48
Compare
Thanks, @Roddoric. We definitely need to understand better what's going on here 🤔 |
I think I understood what's going on |
Hello @waiting-for-dev @kennyadsl |
Thanks, @Roddoric. I answered in the comment. |
71132e5
to
686049f
Compare
Sorry for the delay... @waiting-for-dev it should be fine now, all tests pass in local but I don't see the CI triggering |
Thanks, @Roddoric. Last ask, could you squash all the commits? 🙏 Yeah, we have some issues with CI. Once it's fixed we can go ahead. |
686049f
to
f402c08
Compare
@Roddoric, I know I said last ask, but as any good movie there's still a VERY last scene 😅 Could you please rebase from master to trigger CI? |
Before v3.1, the proc given as the default value in a preference had its context bound to the initialized instance. That allowed, for example, referencing other preferences. From v3.1., the context is bound to the class. We are coming back to the behavior by calling the proc with `instance_exec` The test changed because initializing `preferences` with `default_preferences` cause an infinite loop. This is not a regression as it's the expected behavior on anterior versions but added some information on the comments. Change Preference Context: - `class A` is declared once in a `before(:all)` so the class WILL NOT be destroyed/recreate for each test - `@a = A.new` is in a `before(:each)` therefore the variable is initialized before the test start - The 4 examples from l.285 define the same preference `:secret` but the last one is the only using a default If the test run first: - `@a` is instantiated without `preference :secret` - `@a.preferences` looks like `{:color=>"green"}` - We define the `preference :secret` with a default - Calling `@a.get_preference(:secret)` will work because it will look for the default If the test doesn't run first: - `@a` is instantiated with a `preference :secret` without `:default` - `@a.preferences` looks like `{:color=>"green", :secret=>"rJ0s--jPH2d2iyyB9KseLU--HbYfXFtRB36+yj9L3fSDFA=="} ` (`:secret` value is `nil` but encrypted) - We override `preference :secret` with a default - `@a.get_preference(:secret)` returns `nil` as it's initialized and does not look for the default Setting `@a = A.new` after the override works because it'll be initialized with the new default To fix it we declared the class as anonymous and create them only when needed.
f402c08
to
67bd03d
Compare
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.
Thanks! ❤️
💚 All backports created successfully
Questions ?Please refer to the Backport tool documentation and see the Github Action logs for details |
Summary
Before v3.1, the proc given as the default value in a preference had its context bound to the initialized instance. That allowed, for example, referencing other preferences.
From v3.1., the context is bound to the class.
We are coming back to the behavior by calling the proc with
instance_exec
More context in the linked Issue
Checklist
Check out our PR guidelines for more details.
The following are mandatory for all PRs:
The following are not always needed (
cross them outif they are not):