Skip to content
Browse files

fixed Schema read_attribute when using custom data_field

  • Loading branch information...
1 parent c33b1d8 commit 115b97c84f9aa9bbb609b68ea938d0be10109820 @jsmilani committed Jan 10, 2013
Showing with 37 additions and 1 deletion.
  1. +1 −1 lib/serializable_attributes/schema.rb
  2. +21 −0 test/serialized_attributes_test.rb
  3. +15 −0 test/test_helper.rb
View
2 lib/serializable_attributes/schema.rb
@@ -96,7 +96,7 @@ def reload(options = nil)
@model.send(:define_method, :read_attribute) do |attribute_name|
schema = self.class.send("#{data_field}_schema")
if schema.include?(attribute_name)
- data[attribute_name.to_s]
+ send(data_field)[attribute_name.to_s]
else
super(attribute_name)
end
View
21 test/serialized_attributes_test.rb
@@ -291,6 +291,27 @@ def @record.title=(v)
end
end
+ Object.const_set("SerializedAttributeWithSerializedPrefsDataTestWith#{fmt.name.demodulize}", Class.new(ActiveSupport::TestCase)).class_eval do
+ class << self
+ attr_accessor :format, :current_time, :raw_hash, :raw_prefs
+ end
+ self.format = fmt
+ self.current_time = Time.now.utc.midnight
+ self.raw_hash = {:title => 'abc'}
+ self.raw_prefs = format.encode(raw_hash)
+
+ def setup
+ SerializedPrefsRecord.prefs_schema.formatter = self.class.format
+ @record = SerializedPrefsRecord.new
+ @record.raw_prefs = self.class.raw_prefs
+ end
+
+ test "#read_attribute reads serialized fields" do
+ @record.body = 'a'
+ assert_equal 'a', @record.read_attribute(:body)
+ end
+ end
+
Object.const_set("SerializedAttributeTest#{fmt.name.demodulize}", Class.new(ActiveSupport::TestCase)).class_eval do
class << self
attr_accessor :format
View
15 test/test_helper.rb
@@ -105,3 +105,18 @@ class SerializedRecordWithDefaults < ActiveRecord::Base
def add_to_transaction
end
end
+
+class SerializedPrefsRecord < ActiveRecord::Base
+ extend SerializableMethods
+
+ attr_accessor :raw_prefs
+
+ serialize_attributes :prefs do
+ string :title, :body
+ end
+
+ before_save { |r| false } # cancel the save
+
+ def add_to_transaction
+ end
+end

0 comments on commit 115b97c

Please sign in to comment.
Something went wrong with that request. Please try again.