vendor/plugins/dataset/lib/dataset/record/fixture.rb in radiant-0.7.2 vs vendor/plugins/dataset/lib/dataset/record/fixture.rb in radiant-0.8.0

- old
+ new

@@ -2,16 +2,18 @@ module Dataset module Record # :nodoc: class Fixture # :nodoc: - attr_reader :meta, :symbolic_name + attr_reader :meta, :symbolic_name, :session_binding - def initialize(meta, attributes, symbolic_name = nil) - @meta = meta - @attributes = attributes.stringify_keys - @symbolic_name = symbolic_name || object_id + def initialize(meta, attributes, symbolic_name, session_binding) + @meta = meta + @attributes = attributes.stringify_keys + @symbolic_name = symbolic_name || object_id + @session_binding = session_binding + install_default_attributes! end def create record_class.connection.insert_fixture to_fixture, meta.table_name @@ -24,18 +26,24 @@ def record_class meta.record_class end - def to_hash - @attributes - end - def to_fixture ::Fixture.new(to_hash, meta.class_name) end + def to_hash + hash = @attributes.dup + hash[meta.inheritance_column] = meta.sti_name if meta.inheriting_record? + record_class.reflections.each do |name, reflection| + name = name.to_s + add_reflection_attributes(hash, name, reflection) if hash[name] + end + hash + end + def install_default_attributes! @attributes['id'] ||= symbolic_name.to_s.hash.abs install_timestamps! end @@ -46,9 +54,20 @@ end def now(column) (ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now).to_s(:db) end + + private + def add_reflection_attributes(hash, name, reflection) + value = hash.delete(name) + case value + when Symbol + hash[reflection.primary_key_name] = session_binding.find_id(reflection.klass, value) + else + hash[reflection.primary_key_name] = value + end + end end end end \ No newline at end of file