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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/default_value_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,18 @@ def default_value_for(attribute, options = {}, &block)
allows_nil = opts.fetch('allows_nil', true)
end

if !method_defined?(:set_default_values)
if !singleton_methods(false).include?(:_default_attribute_values)
include(InstanceMethods)

after_initialize :set_default_values

class_attribute :_default_attribute_values
class_attribute :_default_attribute_values_not_allowing_nil

extend(DelayedClassMethods)
init_hash = true
else
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?


extend(DelayedClassMethods)
end

if block_given?
Expand Down
Loading