Skip to content

Commit

Permalink
fix(model): handle visible and hidden properties
Browse files Browse the repository at this point in the history
visible and hidden properties should be handled in the same way for every model instance obtained in any way.
  • Loading branch information
thetutlage committed Nov 8, 2017
2 parents be987ca + 8757f4a commit cbe2cce
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
6 changes: 5 additions & 1 deletion src/Lucid/Model/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ class BaseModel {
'$relations',
'$sideLoaded',
'$parent',
'$frozen'
'$frozen',
'$visible',
'$hidden'
]

this.$attributes = {}
Expand All @@ -197,6 +199,8 @@ class BaseModel {
this.$sideLoaded = {}
this.$parent = null
this.$frozen = false
this.$visible = this.constructor.visible
this.$hidden = this.constructor.hidden
}

/**
Expand Down
43 changes: 3 additions & 40 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,43 +470,6 @@ class Model extends BaseModel {
return !!this.$parent
}

/**
* Instantiate the model by defining constructor properties
* and also setting `__setters__` to tell the proxy that
* these values should be set directly on the constructor
* and not on the `attributes` object.
*
* @method instantiate
*
* @return {void}
*
* @private
*/
_instantiate () {
this.__setters__ = [
'$attributes',
'$persisted',
'primaryKeyValue',
'$originalAttributes',
'$relations',
'$sideLoaded',
'$parent',
'$frozen',
'$visible',
'$hidden'
]

this.$attributes = {}
this.$persisted = false
this.$originalAttributes = {}
this.$relations = {}
this.$sideLoaded = {}
this.$parent = null
this.$frozen = false
this.$visible = null
this.$hidden = null
}

/**
* Formats the date fields from the payload, only
* when they are marked as dates and there are
Expand Down Expand Up @@ -1326,13 +1289,13 @@ class Model extends BaseModel {
}

if (!this.isNew) {
const attributes = await this.constructor.find(this.primaryKeyValue)
if (!attributes) {
const newInstance = await this.constructor.find(this.primaryKeyValue)
if (!newInstance) {
throw GE
.RuntimeException
.invoke(`Cannot reload model since row with ${this.constructor.primaryKey} ${this.primaryKeyValue} has been removed`)
}
this.newUp(attributes)
this.newUp(newInstance.$attributes)
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/unit/lucid-serializer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,43 @@ test.group('Relations | Serializer', (group) => {
assert.equal(json.perPage, 20)
assert.equal(json.lastPage, 1)
})

test('test toJSON with default visible() and hidden()', async (assert) => {
class User extends Model {
}
User._bootIfNotBooted()
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])

// Test 1 - find in DB
const a = await User.find(1)
assert.include(a.toJSON(), {id: 1, username: 'virk'})
})

test('test toJSON with visible()', async (assert) => {
class User extends Model {
static get visible () {
return ['id', 'username']
}
}
User._bootIfNotBooted()
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])

// Test 1 - find in DB
const a = await User.find(1)
assert.deepEqual(a.toJSON(), {id: 1, username: 'virk'}, 'Test 1 failed')

// // test 2 - find in db then reload
const b = await User.find(1)
await b.reload()
assert.deepEqual(b.toJSON(), {id: 1, username: 'virk'}, 'Test 2 failed')

// // test 3 - create
const c = await User.create({username: 'ben'})
assert.deepEqual(c.toJSON(), {id: 3, username: 'ben'}, 'Test 3 failed')

// // test 4 - create then reload from db
const d = await User.create({username: 'simon'})
await d.reload()
assert.deepEqual(d.toJSON(), {id: 4, username: 'simon'}, 'Test 4 failed')
})
})

0 comments on commit cbe2cce

Please sign in to comment.