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

Explicitly define :_default_attribute_values using class_attribute in child class #101

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

k-tsuchiya-jp
Copy link

Detail

Fix #100

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@k-tsuchiya-jp
Copy link
Author

before

$ BUNDLE_GEMFILE=/home/zenet/default_value_for/gemfiles/rails_80.gemfile bundle exec rake
/home/k-tsuchiya-jp/.rbenv/versions/3.3.6/bin/ruby test.rb

Testing with ruby version 3.3.6

Testing with Active Record version 8.0.1

Testing with Action Pack version 8.0.1

Run options: --seed 21384

# Running:

...................................F..

Finished in 0.207456s, 183.1710 runs/s, 265.1159 assertions/s.

  1) Failure:
DefaultValuePluginTest#test_multiple_default_values_in_subclass_with_default_values_in_parent_class [test.rb:236]:
Expected: 5678
  Actual: nil

38 runs, 55 assertions, 1 failures, 0 errors, 0 skips
rake aborted!

after

$ BUNDLE_GEMFILE=/home/zenet/default_value_for/gemfiles/rails_80.gemfile bundle exec rake
/home/k-tsuchiya-jp/.rbenv/versions/3.3.6/bin/ruby test.rb

Testing with ruby version 3.3.6

Testing with Active Record version 8.0.1

Testing with Action Pack version 8.0.1

Run options: --seed 45765

# Running:

......................................

Finished in 0.200715s, 189.3235 runs/s, 279.0031 assertions/s.

38 runs, 56 assertions, 0 failures, 0 errors, 0 skips

init_hash = !singleton_methods(false).include?(:_default_attribute_values)
end

if init_hash
self._default_attribute_values = {}
self._default_attribute_values_not_allowing_nil = []
Copy link
Collaborator

@jrafanie jrafanie Dec 31, 2024

Choose a reason for hiding this comment

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

I'll need to dig into the history of the original code to better understand why we avoided initializing/resetting these class attributes previously and why it's ok now. I see the tests are green on older rails versions but I'm not sure the reason why this code was written like this.

@k-tsuchiya-jp did you figure out why your change is safe to do for rails 6 through 8?

@jrafanie
Copy link
Collaborator

Thanks for the possible fix for #100. See comment above. If you can explain it to me, I'd be more confident this change wouldn't break older versions of rails. Thank you!

@jrafanie jrafanie self-assigned this Jan 3, 2025
@jrafanie jrafanie added the bug label Jan 3, 2025
@jrafanie
Copy link
Collaborator

jrafanie commented Jan 3, 2025

@k-tsuchiya-jp thanks, I missed this comment (#100 (comment)) before... after reviewing it and testing it locally with ruby 3.2 and 3.3 on the supported rails, your change is great. The original code dates back to 2012.

Thanks for your contribution.

@jrafanie jrafanie merged commit 1486999 into FooBarWidget:master Jan 3, 2025
17 checks passed
jrafanie added a commit that referenced this pull request Jan 3, 2025
@jrafanie
Copy link
Collaborator

jrafanie commented Jan 3, 2025

@k-tsuchiya-jp released 4.1.1... thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

There are cases where default value is not set in STI since Rails 8.0.1
2 participants