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

Remove usage of OpenStruct #51510

Merged
merged 1 commit into from Apr 9, 2024
Merged

Remove usage of OpenStruct #51510

merged 1 commit into from Apr 9, 2024

Conversation

fatkodima
Copy link
Member

Follow up to #51491 (comment).

There was one more error regarding the missing require "ostruct" after that PR merge (https://buildkite.com/rails/rails/builds/106158#018eaee1-7e2b-4e8a-9876-8d3c358d5576), so maybe we can just remove its usage instead?

cc @byroot

@@ -355,9 +355,6 @@ Performance/DeletePrefix:
Performance/DeleteSuffix:
Enabled: true

Performance/OpenStruct:
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't that cop have prevented use of OpenStruct?

I also see there is a Style/OpenStructUse cop now? If we're to remove it from the test suite might as well ensure it won't be re-introduced.

Copy link
Contributor

Choose a reason for hiding this comment

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

All Performance cops are disabled for the test suite here. It would need to be explicitly enabled by path

@@ -55,7 +55,7 @@ class ActiveRecord::Encryption::ConfigurableTest < ActiveRecord::EncryptionTestC
end

test "installing autofiltered parameters will work with unnamed classes" do
application = OpenStruct.new(config: OpenStruct.new(filter_parameters: []))
application = ActiveSupport::OrderedOptions[config: ActiveSupport::OrderedOptions[filter_parameters: []]]
Copy link
Member

Choose a reason for hiding this comment

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

I'm not too sure how I feel about using OrderedOption as a stand-in for OpenStruct.

From a purely pragmatic point of view, it's definitely superior as at least it's a class we control and we know it's loaded.

But I feel in many cases here, and espeically on the code just above, it may make more sense to use a Minitest::Mock as it is closer to the intent. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Turned out, that OpenStruct is not equivalent to ActiveSupport::OrderedOptions, as failing CI shows. The problem is described in #51469. ActiveSupport::OrderedOptions#respond_to_missing? returns true for all the calls and so the line

scope = scope_obj.scope.respond_to?(:to_proc) ? scope_obj.scope : scope_obj.scope.method(:call)
is not working correctly.

Just tried to convert to use mocks, and turned out that methods like is_a? should be mocked too. Meh. So probably just require "ostruct" in all the places?

Copy link
Contributor

Choose a reason for hiding this comment

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

So probably just require "ostruct" in all the places?

Meh, I think the issue will just crop back up.

I'm tempted to suggest using Struct, it's a tiny bit more verbose but would do the job fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

@byroot Updated to use Structs. Please take a look.

eileencodes added a commit that referenced this pull request Apr 9, 2024
`ostruct` was being implictly required by the `json` gem. But once it
was upgraded, these tests failed to initialize `OpenStruct`.

While we're trying to remove `ostruct` usage in #51510, CI is currently
failing so I'm pushing these in the mean time.
@eileencodes
Copy link
Member

Hey, I saw you're trying to remove these but CI was failing due to json bump, so I added more requires you'll need to get rid of in 50515fb

carlosantoniodasilva added a commit that referenced this pull request Apr 9, 2024
Similar to 50515fb, make sure we
require `ostruct` where we use `OpenStruct`, to get the build back to
green, while we work to remove its usage on tests. (see #51510.)

Sample error:

```
Error:
ActiveRecord::Encryption::ConfigurableTest#test_installing_autofiltered_parameters_will_add_the_encrypted_attribute_as_a_filter_parameter_using_the_dot_notation:
NameError: uninitialized constant ActiveRecord::Encryption::ConfigurableTest::OpenStruct
    test/cases/encryption/configurable_test.rb:45:in `block in <class:ConfigurableTest>'
```
@byroot byroot merged commit 773d174 into rails:main Apr 9, 2024
3 of 4 checks passed
@byroot
Copy link
Member

byroot commented Apr 9, 2024

Thank you! ❤️

notapatch pushed a commit to notapatch/rails that referenced this pull request Apr 10, 2024
Similar to 50515fb, make sure we
require `ostruct` where we use `OpenStruct`, to get the build back to
green, while we work to remove its usage on tests. (see rails#51510.)

Sample error:

```
Error:
ActiveRecord::Encryption::ConfigurableTest#test_installing_autofiltered_parameters_will_add_the_encrypted_attribute_as_a_filter_parameter_using_the_dot_notation:
NameError: uninitialized constant ActiveRecord::Encryption::ConfigurableTest::OpenStruct
    test/cases/encryption/configurable_test.rb:45:in `block in <class:ConfigurableTest>'
```
rafaelfranca pushed a commit that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants