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

Commit d63dab3

Browse files
committed
Merge pull request joelmoss#4 from pathable/default-value
Add default option.
2 parents 927b051 + 4fba188 commit d63dab3

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/bitmask_attributes.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def bitmask(attribute, options={}, &extension)
1010
raise ArgumentError, "Must provide an Array :as option"
1111
end
1212

13+
if default = options[:default]
14+
after_initialize do
15+
send("#{attribute}=", default) unless send("#{attribute}?")
16+
end
17+
end
18+
1319
bitmask_definitions[attribute] = Definition.new(attribute, options[:as].to_a,options[:null].nil? || options[:null], options[:zero_value], &extension)
1420
bitmask_definitions[attribute].install_on(self)
1521
end
@@ -34,4 +40,4 @@ def base_class_bitmasks
3440
end
3541
end
3642

37-
ActiveRecord::Base.send :include, BitmaskAttributes
43+
ActiveRecord::Base.send :include, BitmaskAttributes

test/bitmask_attributes_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ def assert_stored(record, *values)
312312
end
313313
end
314314

315+
should "accept a default value option" do
316+
assert_equal DefaultValue.new.default_sym, [:y]
317+
assert_equal DefaultValue.new.default_array, [:y, :z]
318+
assert_equal DefaultValue.new(:default_sym => :x).default_sym, [:x]
319+
assert_equal DefaultValue.new(:default_array => [:x]).default_array, [:x]
320+
end
321+
315322
context_with_classes 'Campaign with null attributes',CampaignWithNull,CompanyWithNull
316323
context_with_classes 'Campaign without null attributes',CampaignWithoutNull,CompanyWithoutNull
317324
context_with_classes 'SubCampaign with null attributes',SubCampaignWithNull,CompanyWithNull

test/support/models.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
create_table :company_without_nulls do |t|
1616
t.string :name
1717
end
18+
create_table :default_values do |t|
19+
t.integer :default_sym, :default_array
20+
end
1821
end
1922

2023
# Pseudo models for testing purposes
@@ -55,4 +58,9 @@ def worked?
5558
end
5659

5760
class SubCampaignWithoutNull < CampaignWithNull
58-
end
61+
end
62+
63+
class DefaultValue < ActiveRecord::Base
64+
bitmask :default_sym, :as => [:x, :y, :z], :default => :y
65+
bitmask :default_array, :as => [:x, :y, :z], :default => [:y, :z]
66+
end

0 commit comments

Comments
 (0)