Browse files

Add working example for custom serialization

  • Loading branch information...
1 parent c73f4b9 commit 3e2a2f890cbc7cda7cc31c02fef0ff83e3914f3b Ankur Sethi committed Apr 12, 2012
Showing with 19 additions and 0 deletions.
  1. +3 −0 spec/lib/amoeba_spec.rb
  2. +3 −0 spec/support/data.rb
  3. +12 −0 spec/support/models.rb
  4. +1 −0 spec/support/schema.rb
View
3 spec/lib/amoeba_spec.rb
@@ -83,6 +83,9 @@
new_post.widgets.map(&:id).each do |id|
old_post.widgets.map(&:id).include?(id).should_not be true
end
+ new_post.custom_things.length == 2
+ new_post.custom_things.select{ |ct| ct.value == [] }.length == 1
+ new_post.custom_things.select{ |ct| ct.value == [1,2]}.length == 1
# }}}
# Author {{{
old_author = Author.find(1)
View
3 spec/support/data.rb
@@ -115,4 +115,7 @@
necklace1.sections << jewelry
necklace1.sections << accessories
necklace1.save
+
+CustomThing.create :value => [1,2]
+CustomThing.create :value => []
# }}}
View
12 spec/support/models.rb
@@ -25,6 +25,7 @@ class Post < ActiveRecord::Base
has_many :categories, :through => :supercats
has_many :post_widgets
has_many :widgets, :through => :post_widgets
+ has_many :custom_things
has_and_belongs_to_many :tags
has_and_belongs_to_many :notes
@@ -73,6 +74,17 @@ class Post < ActiveRecord::Base
class CustomThing < ActiveRecord::Base
belongs_to :post
+ class ArrayPack
+ def load(str)
+ str.split(',')
+ end
+
+ def dump(int_array)
+ int_array.join(',')
+ end
+ end
+
+ serialize :value, ArrayPack.new
end
class Account < ActiveRecord::Base
View
1 spec/support/schema.rb
@@ -82,6 +82,7 @@
end
create_table :custom_things, :force => true do |t|
+ t.integer :post_id
t.string :value
t.timestamps
end

0 comments on commit 3e2a2f8

Please sign in to comment.