-
Notifications
You must be signed in to change notification settings - Fork 0
~inheritance
Tower supports inheritance in both root and embedded documents. In scenarios where documents are inherited from their fields, relations, validations and scopes get copied down into their child documents, but not vise-versa.
class Canvas extends Tower.Model
@field "name", type: "String"
@hasMany "shapes", embedded: true
class Browser extends Canvas
@field "version", type: "Integer"
@scope "recent", @where(version: ">": 3)
class Firefox extends Browser
class Shape extends Tower.Model
@field "x", type: "Integer"
@field "y", type: "Integer"
@belongsTo "canvas", embedded: true
class Circle extends Shape
@field "radius", type: "Float"
class Rectangle extends Shape
@field "width", type: "Float"
@field "height", type: "Float"
In the above example, Canvas, Browser and Firefox will all save in the canvases collection. An additional attribute _type is stored in order to make sure when loaded from the database the correct document is returned. This also holds true for the embedded documents Circle, Rectangle, and Shape.
Querying for subclasses is handled in the normal manner, and although the documents are all in the same collection, queries will only return documents of the correct type, similar to Single Table Inheritance in ActiveRecord.
# Returns Canvas documents and subclasses
Canvas.where(name: "Paper")
# Returns only Firefox documents
Firefox.where(name: "Window 1")
You can add any type of subclass to a has one or has many association, through either normal setting or through the build and create methods on the association: firefox = Firefox.new
firefox.shapes.build({ x: 0, y: 0 })
firefox.shapes.build({ x: 0, y: 0 }, Circle)
firefox.shapes.create({ x: 0, y: 0 }, Rectangle)
rect = Rectangle.new(width: 100, height: 200) firefox.shapes