[Nitro] Og Editing Context

Michael Neumann mneumann at ntecs.de
Thu Feb 24 09:33:41 EST 2005


Would it be possible to introduce an editing context, where you can 
register those objects that should be saved (with possible dependency 
resolving). Example:

class Book; end # forward

class Author
   property :name, String
   has_many :books, Book

class Book
   belongs_to :author, Author


a = Author.new
a.name = "Michael"
b = Book.new

The problem here is, that the author must exist, before we can add a 
book to it (add_book will store the author to the database if it's not 
yet saved). Problem: You can't undo this. And it's not very clear, that 
add_book will save the author to database.

ed = EditingContext.new
a = Author.new
a.name = "Michael"

b = Book.new

# everything is correct linked in memory, but not yet saved to database
b.author #=> a

# save all registered objects. this is done in correct order,
# so first a then b (the order can be infered automatically)

Alternatively, could we modify add_book so that it only assign the 
book.author_oid to the author object (not it's oid, as we don't have one 
yet), and not saves the author object to database. book.save would then 
query author.oid (if the author is already stored) or else raise an 
error. Even better, if author.save would trigger books.each {|b| b.save} 
in it's save method.

This is *very* useful for advanced scaffolding, where you want to add 
e.g. a new phone-number to a person, but not save it to database, yet, 
as we can cancel the whole "create Person" process.

Hope you get the point I'm trying to explain.



More information about the Nitro-general mailing list