Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/serializable_attributes/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/serialized_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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