Skip to content

Commit

Permalink
feat(lucid): allow to unfreeze model instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Radoslaw Mejer committed Dec 21, 2017
1 parent 874268a commit 906c3c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Lucid/Model/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ class BaseModel {
this.$frozen = true
}

/**
* Unfreezes the model allowing further modifications
*
* @method unfreeze
*
* @return {void}
*/
unfreeze () {
this.$frozen = false
}

/**
* Converts model instance toJSON using the serailizer
* toJSON method
Expand Down
2 changes: 1 addition & 1 deletion src/Lucid/Model/proxyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const proxyHandler = exports = module.exports = {}
* @param {Mixed} value
*/
proxyHandler.set = function (target, name, value) {
if (target.isDeleted && name !== 'frozen') {
if (target.isDeleted && name !== '$frozen') {
throw CE.ModelException.deletedInstance(target.constructor.name)
}

Expand Down
12 changes: 12 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,18 @@ test.group('Model', (group) => {
assert.equal(userQuery.sql, helpers.formatQuery('delete from "users" where "id" = ?'))
})

test('allow to unfreeze model instance', async (assert) => {
assert.plan(1)
class User extends Model {
}

const user = new User()
user.freeze()
user.unfreeze()

assert.isFalse(user.$frozen)
})

test('dates should be an empty array when createdAtColumn and updatedAtColumn is not defined', async (assert) => {
class User extends Model {
static get createdAtColumn () {
Expand Down

0 comments on commit 906c3c0

Please sign in to comment.