Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lucid): allow to unfreeze model instance #266

Merged
merged 1 commit into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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