Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request joelmoss#5 from pathable/defaults
Browse files Browse the repository at this point in the history
Allow empty arrays and nils.
  • Loading branch information
flippyhead committed Apr 9, 2013
2 parents 1203186 + 1dab9cd commit 3a35174
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/bitmask_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def bitmask(attribute, options={}, &extension)

if default = options[:default]
after_initialize do
send("#{attribute}=", default) unless send("#{attribute}?")
send("#{attribute}=", default) unless read_attribute(attribute)
end
end

Expand Down Expand Up @@ -75,6 +75,7 @@ def bitmask(attribute, options={}, &extension)
end

define_method "#{attribute}=" do |value|
value ||= default if default
if value.is_a?(Fixnum)
value = self.class.send("#{attribute}_for_bitmask", value)
end
Expand Down
6 changes: 6 additions & 0 deletions test/bitmask_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ def assert_stored(record, *values)
assert_equal DefaultValue.new.default_array, [:y, :z]
assert_equal DefaultValue.new(:default_sym => :x).default_sym, [:x]
assert_equal DefaultValue.new(:default_array => [:x]).default_array, [:x]
assert_equal DefaultValue.new(:default_sym => []).default_sym, []

default_value = DefaultValue.new(:default_sym => :x)

default_value.default_sym = nil
assert_equal default_value.default_sym, [:y]
end

context_with_classes 'Campaign with null attributes',CampaignWithNull,CompanyWithNull
Expand Down

0 comments on commit 3a35174

Please sign in to comment.