Skip to content

Instantly share code, notes, and snippets.

@ra0x3
Last active November 11, 2019 23:50
Show Gist options
  • Save ra0x3/8102043481b26beef26c88b688824f44 to your computer and use it in GitHub Desktop.
Save ra0x3/8102043481b26beef26c88b688824f44 to your computer and use it in GitHub Desktop.
model attributes rfc
// app/core/model-builder.js
class ModelBuilder {
// still using a wrapper function over `defineAttribute`, that
// creates default options via instance chanining. this will
// reduce boiler plate in the model manager files
formAttribute(attr, options) {
return this.defineAttribute(attr, options)
.certifiable()
.customizable()
}
// we are breaking each partition/category of attributes
// into their own separate wrappers, so that developer's
// recognize that there are different attribute types
workflowAttribute(attr, options) {
return this.defineAttribute(attr, options)
.customizable()
}
// on each separate attribute category wrapper, we are
// setting different default options via this instance
// chaining
computedAttribute(attr, options) {
return this.defineAttribute(attr, options)
.isComputedAttribute();
}
}
// app/models/issue.js
class IssueManager {
// we still keep only a single `defineAttributes` function because
// at the end of the day these are all still 'attributes', though
// they are partitioned into different buckets/categories
modelBuilder.defineAttributes() {
// in the simplest case, we won't need to add _anything_ because
// our wrapper function sets all our defaults properly
modelBuilder.formAttribute(‘status’);
// if necessary, we can pass options to overwrite the default
// behavior. we must use instance chaining, because if we get
// rid of chaining, and just go with passing options objects,
// we will get a _ton_ of breakage
modelBuilder.formAttribute('title')
.formLabel('Issue Title') // explicit overwrite
.certifiable(false) // explicit overwrite
.customizable(false); // explicit overwrite
// as a developer in the model manager, I can clearly see that
// I'm using a different model builder API `workflowAttribute`, so
// this must be different than `formAttribute`
modelBuilder.workflowAttribute(‘open’)
.formLabel('Open By')
.customizable(true)
.enforce('action.edit');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment