snusnu / ramom

relational algebra meets object mapping
MIT License
6 stars 0 forks source link

Support DM models using STI #22

Closed snusnu closed 10 years ago

snusnu commented 10 years ago

Generate

For this to work, we need a mapping of STI hierarchy models to their respective name, base relation name and discriminator column (the discriminator value is inferred from the mapping key, i.e. the model name).

Given a mapping like this:

{
  Models::Account => {
    name: :accounts ,
    children: {
      Models::Account::Employee => { name: :employees, discriminator: :type },
      Models::Account::Operator => { name: :operators, discriminator: :type }
    }
  }
}

Compiles the following relation access methods into the schema:

def accounts
  Axiom::Relation::Base.new(:accounts, [...]) # actually gets instantiated only once
end

def employees
  accounts.restrict(type: 'Models::Account::Employee')
end

def operators
  accounts.restrict(type: 'Models::Account::Operator')
end

This is also necessary, because all STI hierarchy models share the same storage_name, which means that for every STI hierarchy within DataMapper::Model.descendants, mappings need to be declared for the base model and all leaf models.

snusnu commented 10 years ago

I've decided that this is not worth the complexity needed to support this kind of thing. Instead, it should be possible to simply give the Relation::Registry a blacklist of datamapper models for which it should not register base relations.

This will in turn allow schema authors to simply add the restricted base relations for the respective STI subclasses themselves.