Permalink
Browse files

Support serialized preferences

Merges #1612
  • Loading branch information...
sbounmy authored and radar committed May 29, 2012
1 parent c680c14 commit 74153232272c0dc18d1d1c160df0efefb3ba88b1
@@ -21,6 +21,8 @@ def value
self[:value].to_i
when :boolean
(self[:value].to_s =~ /^[t|1]/i) != nil
else
self[:value].is_a?(String) ? YAML.load(self[:value]) : self[:value]
end
else
self[:value]
@@ -65,7 +65,7 @@ def round_trip_preference(key, value, value_type)
pref.value.should eq value
pref.value_type.should == value_type.to_s
end
it ":text" do
value_type = :text
value = "This is a string stored as text"
@@ -84,6 +84,15 @@ def round_trip_preference(key, value, value_type)
pref.value_type.should == value_type.to_s
end
it ":any" do
value_type = :any
value = [1, 2]
key = "any_key"
pref = round_trip_preference(key, value, value_type)
pref.value.should eq value
pref.value_type.should == value_type.to_s
end
end
describe "converting old values" do
@@ -179,6 +179,25 @@ class B < A
end
end
context "converts any preferences to any values" do
before do
A.preference :product_ids, :any, :default => []
A.preference :product_attributes, :any, :default => {}
end
it "with array" do
@a.preferences[:product_ids].should == []
@a.set_preference(:product_ids, [1, 2])
@a.preferences[:product_ids].should == [1, 2]
end
it "with hash" do
@a.preferences[:product_attributes].should == {}
@a.set_preference(:product_attributes, {:id => 1, :name => 2})
@a.preferences[:product_attributes].should == {:id => 1, :name => 2}
end
end
end
describe "persisted preferables" do
@@ -201,6 +220,7 @@ def self.down
class PrefTest < ActiveRecord::Base
preference :pref_test_pref, :string, :default => 'abc'
preference :pref_test_any, :any, :default => []
end
end
@@ -221,6 +241,14 @@ class PrefTest < ActiveRecord::Base
pr.save!
pr.get_preference(:pref_test_pref).should == 'XXX'
end
it "saves preferences for serialized object" do
pr = PrefTest.new
pr[:pref_test_any] = [1, 2]
pr[:pref_test_any].should == [1, 2]
pr.save!
pr[:pref_test_any].should == [1, 2]
end
end
describe "requires a valid id" do
@@ -275,6 +303,7 @@ class PrefTest < ActiveRecord::Base
@a.has_preference?(:test_temp).should be_false
@a.respond_to?(:preferred_test_temp).should be_false
end
end

0 comments on commit 7415323

Please sign in to comment.