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

default_value_for on a non-column attribute doesn't honor initialized attributes when allows_nil is false. #59

Closed
erichmenge opened this issue Feb 22, 2016 · 1 comment

Comments

@erichmenge
Copy link

Here's what's currently in default_value_for.rb

        attribute_blank = if column && column.type == :boolean
          attributes[attribute].nil?
        else
          attributes[attribute].blank?
        end

Since the attributes hash is the only thing inspected, for non-column values this will always be nil.

It seems like the fix for this should be as easy as switching from checking the attributes hash to using the getter method.

@erichmenge
Copy link
Author

The test_also_works_on_attributes_that_arent_database_columns test currently passes by accident because of:

        # allow explicitly setting nil through allow nil option
        next if @initialization_attributes.is_a?(Hash) &&
                (
                  @initialization_attributes.has_key?(attribute) ||
                  (
                    @initialization_attributes.has_key?("#{attribute}_attributes") &&
                    nested_attributes_options.stringify_keys[attribute]
                  )
                ) &&
                !self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)

Since it is nil, and allows_nil is the default, it doesn't update the value.

njakobsen added a commit to culturecode/default_value_for that referenced this issue Dec 16, 2016
Fix bug that prevented default value from working when a value was passed in at initialization, and `:allows_nil => false`. Only the attributes hash was being read, so stored attributes were not being detected.

Closes FooBarWidget#59.
WillemOpperman pushed a commit to WillemOpperman/default_value_for that referenced this issue Mar 22, 2018
Fix bug that prevented default value from working when a value was passed in at initialization, and `:allows_nil => false`. Only the attributes hash was being read, so stored attributes were not being detected.

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

No branches or pull requests

1 participant