hmarr/mongoengine — Issue #327: Promote and demote inherited classes.
Author: amcgregor
Created: 2011-10-20 07:57:53+00:00
State: open

A feature I could really, really use is class promotion and demotion when using inheritance. As an example in [BDD](http://en.wikipedia.org/wiki/Behavior_Driven_Development) style:

* **Given** three *classes*: `Asset(Document)`, `Folder(Asset)`, `Gallery(Folder)`.
* **And given** an *instance* of `Folder`.
* **When** the system *promotes* the `Folder` instance to `Gallery`. [This is specific; not specifying a parent class defaults to a single step.]
* **Then** a new *instance* of type `Gallery` should be returned.
* **And then** old *instances* (from caches, say) should be invalidated. [Attempts to use these should result in exceptions.]
* **And then** data values common to both classes should be transferred. [E.g. `name` is common to both.]
* **When** the system *demotes* the `Folder` instance to `Asset`.
* **Then** a new *instance* of type `Asset` should be returned.
* **And then** old *instances* (from caches, say) should be invalidated.
* **And then** data values common to both classes should be transferred.
* **And then** data values not common should optionally be removed from the underlying record. [This is not possible when promoting as you can't "delete" existing definitions, only override them. E.g. the `view` (list, icon, etc.) would be deleted.]

The first is a real use-case. ;) The second is needed for the use case of demoting a `User` subclass instance back to `User`, then promoting it back up to a different subclass in order to change the capabilities and data-based "role" of the given user.