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#4 from pathable/default-value
Browse files Browse the repository at this point in the history
Add default option.
  • Loading branch information
flippyhead committed Oct 23, 2012
2 parents 927b051 + 4fba188 commit d63dab3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/bitmask_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def bitmask(attribute, options={}, &extension)
raise ArgumentError, "Must provide an Array :as option"
end

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

bitmask_definitions[attribute] = Definition.new(attribute, options[:as].to_a,options[:null].nil? || options[:null], options[:zero_value], &extension)
bitmask_definitions[attribute].install_on(self)
end
Expand All @@ -34,4 +40,4 @@ def base_class_bitmasks
end
end

ActiveRecord::Base.send :include, BitmaskAttributes
ActiveRecord::Base.send :include, BitmaskAttributes
7 changes: 7 additions & 0 deletions test/bitmask_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ def assert_stored(record, *values)
end
end

should "accept a default value option" do
assert_equal DefaultValue.new.default_sym, [:y]
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]
end

context_with_classes 'Campaign with null attributes',CampaignWithNull,CompanyWithNull
context_with_classes 'Campaign without null attributes',CampaignWithoutNull,CompanyWithoutNull
context_with_classes 'SubCampaign with null attributes',SubCampaignWithNull,CompanyWithNull
Expand Down
10 changes: 9 additions & 1 deletion test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
create_table :company_without_nulls do |t|
t.string :name
end
create_table :default_values do |t|
t.integer :default_sym, :default_array
end
end

# Pseudo models for testing purposes
Expand Down Expand Up @@ -55,4 +58,9 @@ def worked?
end

class SubCampaignWithoutNull < CampaignWithNull
end
end

class DefaultValue < ActiveRecord::Base
bitmask :default_sym, :as => [:x, :y, :z], :default => :y
bitmask :default_array, :as => [:x, :y, :z], :default => [:y, :z]
end

0 comments on commit d63dab3

Please sign in to comment.