From 39a92109b5c7e09f3ed66e31fe5d3a271c6c1164 Mon Sep 17 00:00:00 2001 From: Bryan Larsen Date: Sun, 16 Jan 2011 16:22:27 -0500 Subject: [PATCH] [#147] bugfix and test/documentation for HoboFields::SerializedObject --- hobofields/lib/hobo_fields/serialized_object.rb | 2 +- hobofields/test/rich_types.rdoctest | 46 ++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/hobofields/lib/hobo_fields/serialized_object.rb b/hobofields/lib/hobo_fields/serialized_object.rb index 67ae68d..2b905f1 100644 --- a/hobofields/lib/hobo_fields/serialized_object.rb +++ b/hobofields/lib/hobo_fields/serialized_object.rb @@ -5,7 +5,7 @@ module HoboFields COLUMN_TYPE = :text def self.declared(model, name, options) - model.serialize name, options.delete(:class) + model.serialize name, options.delete(:class) || Object end HoboFields.register_type(:serialized, self) diff --git a/hobofields/test/rich_types.rdoctest b/hobofields/test/rich_types.rdoctest index fa567de..4a147f7 100644 --- a/hobofields/test/rich_types.rdoctest +++ b/hobofields/test/rich_types.rdoctest @@ -165,8 +165,52 @@ Provides validation of correct email address format. ### `HoboFields::PasswordString` -`HoboFields::PasswordString` provides a simple `to_html` to prevent accidental display of a password. It simply returns "`[password hidden]`". The type is also used to indicate the need for an `` +`HoboFields::PasswordString` provides a simple `to_html` to prevent +accidental display of a password. It simply returns "`[password +hidden]`". The type is also used to indicate the need for an `` + +### `HoboFields::Serialized` + +This type lets you store an arbitrary object as serialized text into +the database column. You can optionally pass the :class option to +restrict the column type. + + >> + def migrate(renames={}) + up, down = HoboFields::MigrationGenerator.run(renames) + ActiveRecord::Migration.class_eval(up) + ActiveRecord::Base.send(:subclasses).each { |model| model.reset_column_information } + [up, down] + end +{.hidden} + >> + class Vault1 < ActiveRecord::Base + fields do + content :serialized + end + end + + >> migrate + >> Vault1.create!(:content => {:key => "in Vault"}) + >> Vault1.first.content + => {:key => "in Vault"} + + >> + class Vault2 < ActiveRecord::Base + fields do + content :serialized, :class => Hash + end + end + + >> migrate + >> Vault2.create!(:content => {:key => "in Vault"}) + >> Vault2.first.content + => {:key => "in Vault"} + >> Vault2.create!(:content => 17) rescue ActiveRecord::SerializationTypeMismatch + >> Vault2.all.size + => 1 # second record not created because of type mismatch ## Enum Strings -- 1.7.1