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

Do not overwrite the database default value if provided by attributes #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 11 additions & 0 deletions lib/default_value_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ def set_default_values
end
next unless connection_default_value_defined || attribute_blank

is_present = -> (value) do
if [true, false].include?(value)
!value.nil?
else
value.present?
end
end

# Do not overwrite if the attribute is present in @initialization_attributes
next if @initialization_attributes.is_a?(Hash) && is_present.call(@initialization_attributes.dig(attribute))

# allow explicitly setting nil through allow nil option
next if @initialization_attributes.is_a?(Hash) &&
(
Expand Down
16 changes: 16 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ToSql < (Arel::VERSION[/\A\d+/].to_i >= 6 ? Arel::Visitors::Reduce : Arel:
t.string :type
t.integer :number
t.integer :count, :null => false, :default => 1
t.boolean :flag2, :null => false, :default => false
t.integer :user_id
t.timestamp :timestamp
t.text :stuff
Expand Down Expand Up @@ -168,6 +169,16 @@ def test_overwrites_db_default
assert_equal 1234, Book.new.count
end

def test_does_not_overwrite_db_default_if_provided_by_mass_assignment
Book.default_value_for(:count, :allows_nil => false) { 1234 }
assert_equal Book.column_defaults['count'], Book.new(count: Book.column_defaults['count']).count
end

def test_does_not_overwrite_db_default_boolean_value_if_provided_by_mass_assignment
Book.default_value_for(:flag2, :allows_nil => false) { true }
assert_equal Book.column_defaults['flag2'], Book.new(flag2: Book.column_defaults['flag2']).flag2
end

def test_doesnt_overwrite_values_provided_by_mass_assignment
Book.default_value_for :number, 1234
assert_equal 1, Book.new(:number => 1, :count => 2).number
Expand Down Expand Up @@ -199,6 +210,11 @@ def test_overwrites_explicitly_provided_nil_values_in_mass_assignment
assert_equal 1234, Book.new(:number => nil).number
end

def test_overwrites_string_blank_value_provided_by_mass_assignment
Book.default_value_for(:type, :allows_nil => false) { 'string' }
assert_equal 'string', Book.new(type: '').type
end
Copy link
Author

Choose a reason for hiding this comment

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

I noticed that this case did not have a test, so added one here.


def test_default_values_are_inherited
Book.default_value_for :number, 1234
assert_equal 1234, Novel.new.number
Expand Down