lib/bigamy/proxy.rb in bigamy-0.2.0 vs lib/bigamy/proxy.rb in bigamy-0.2.1

- old
+ new

@@ -72,23 +72,32 @@ end def add_getter me.class_eval <<-EOF def #{name} - self.id.nil? ? nil : #{target_klass}.first(:conditions => {:#{foreign_key} => export_id_val(self.id)}) + self.#{primary_key}.nil? ? nil : #{target_klass}.first(:conditions => {:#{foreign_key} => export_id_val(self.#{primary_key})}) end EOF end def add_setter me.class_eval <<-EOF def #{name}= v - raise NewRecordAssignment.new('Child must be saved') if v.new_record? - raise NewRecordAssignment.new('Parent must be saved') if self.new_record? - raise TypeError unless v.is_a? #{klass} + if v.nil? + new_id = nil + else + raise NewRecordAssignment.new('Child must be saved') if v.new_record? + raise NewRecordAssignment.new('Parent must be saved') if self.new_record? + raise TypeError unless v.is_a? #{klass} + new_id = export_id_val(self.#{primary_key}) + end - v.#{foreign_key} = export_id_val(self.id) + if #{name} + #{name}.update_attributes :#{foreign_key} => nil + end + + v.#{foreign_key} = new_id v.save! end EOF end end @@ -99,22 +108,30 @@ end def add_getter me.class_eval <<-EOF def #{name} - self.id.nil? ? nil : #{target_klass}.all(:conditions => {:#{foreign_key} => export_id_val(self.id)}) + self.#{primary_key}.nil? ? nil : #{target_klass}.all(:conditions => {:#{foreign_key} => export_id_val(self.#{primary_key})}) end EOF end def add_setter me.class_eval <<-EOF def #{name}= val - raise NewRecordAssignment.new('All children must be saved') if val.select(&:new_record?).present? - raise NewRecordAssignment.new('Parent must be saved') if self.new_record? + val ||= [] + if val == [] + new_id = nil + else + raise NewRecordAssignment.new('All children must be saved') if val.select(&:new_record?).present? + raise NewRecordAssignment.new('Parent must be saved') if self.new_record? + new_id = export_id_val(self.#{primary_key}) + end + + #{name}.each {|x| x.update_attributes :#{foreign_key} => nil } - val.each {|v| v.send "#{foreign_key}=", export_id_val(self.id); v.save! } + val.each {|v| v.send "#{foreign_key}=", new_id; v.save! } end EOF end end @@ -128,23 +145,28 @@ end def add_getter code = <<-EOF def #{name} - self.id.blank? ? nil : #{klass}.first(:conditions => {:#{primary_key} => export_id_val(self.id)}) + self.#{primary_key}.blank? ? nil : #{klass}.first(:conditions => {:#{primary_key} => export_id_val(self.#{primary_key})}) end EOF me.class_eval code, __FILE__, __LINE__ end def add_setter code = <<-EOF def #{name}= val + if val.nil? + set_value(:#{foreign_key}, nil) + return + end + raise NewRecordAssignment if val.new_record? raise TypeError.new("Should get #{klass}") unless val.is_a? #{klass} - set_value :#{foreign_key}, val.id + set_value :#{foreign_key}, val.#{primary_key} end EOF me.class_eval code, __FILE__, __LINE__ end