diff --git a/lib/bitmask_attributes.rb b/lib/bitmask_attributes.rb index 9d80f79..01dd34d 100644 --- a/lib/bitmask_attributes.rb +++ b/lib/bitmask_attributes.rb @@ -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 @@ -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 diff --git a/test/bitmask_attributes_test.rb b/test/bitmask_attributes_test.rb index dca0564..08232a5 100644 --- a/test/bitmask_attributes_test.rb +++ b/test/bitmask_attributes_test.rb @@ -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