Skip to content
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
12 changes: 9 additions & 3 deletions src/concerns/hides-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ const HidesAttributes = (Model) => {

makeVisible(...keys) {
const visible = flattenDeep(keys);
this.visible = [...this.visible, ...visible];

if (this.visible.length > 0) {
this.visible = [...this.visible, ...visible];
}

this.hidden = difference(this.hidden, visible);
return this;
}

makeHidden(...keys) {
const hidden = flattenDeep(keys);
this.hidden = [...this.hidden, ...hidden];

this.visible = difference(this.visible, hidden);
if (this.hidden.length > 0) {
this.hidden = [...this.hidden, ...hidden];
}
return this;
}

Expand All @@ -32,10 +36,12 @@ const HidesAttributes = (Model) => {

setHidden(hidden) {
this.hidden = hidden;
return this;
}

setVisible(visible) {
this.visible = visible;
return this;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/soft-deleting-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { tap } = require('./utils');

const hasJoins = (statements) => {
for (const statement of statements) {
if (typeof statement === 'JoinClause' || statement?.grouping === 'join') {
if (statement?.grouping === 'join') {
return true;
}
}
Expand Down
37 changes: 28 additions & 9 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('Model', () => {
});

it('serializes correctly', () => {
testModel.makeVisible('firstName');
testModel.setVisible(['firstName']);
expect(testModel.toJson()).toBe('{"firstName":"Joe"}');
expect(testModel.toString()).toBe('{"firstName":"Joe"}');
});
Expand All @@ -251,32 +251,51 @@ describe('Model', () => {
});

it('hides the fields specified in the model\'s "hidden" property', () => {
expect(testModel.makeHidden('firstName').toData()).toEqual({id: 1, lastName: 'Shmoe', address: '123 Main St.'});
expect(testModel.setHidden(['firstName']).toData()).toEqual({id: 1, lastName: 'Shmoe', address: '123 Main St.'});
testModel.setHidden(['firstName', 'lastName']).makeVisible('firstName');
expect(testModel.getHidden()).toEqual(['lastName']);
expect(testModel.toData()).toEqual({id: 1, firstName: 'Joe', address: '123 Main St.'});
});

it('hides the fields specified in the "options.hidden" property', () => {
testModel.makeHidden(['firstName', 'id']);
testModel.setHidden(['firstName', 'id']);
expect(testModel.toData()).toEqual({lastName: 'Shmoe', address: '123 Main St.'});
});

it('prioritizes "hidden" if there are conflicts when using both "hidden" and "visible"', () => {
testModel.makeVisible('firstName', 'lastName');
testModel.makeHidden('lastName');
testModel.setVisible(['firstName', 'lastName']);
testModel.setHidden(['lastName']);
expect(testModel.toData()).toEqual({ firstName: 'Joe' });
});

it('allows overriding the model\'s "hidden" property with a "setHidden" argument', () => {
testModel.hidden = ['lastName'];
testModel.setHidden(['firstName', 'id']);
const data = testModel.toData();
expect(data).toEqual({
lastName: 'Shmoe',
address: '123 Main St.'
});
});

it('allows overriding the model\'s "hidden" property with a "makeHidden" argument', () => {
testModel.hidden = ['lastName'];
const data = testModel.makeHidden('firstName', 'id').toData();
expect(data).toEqual({address: '123 Main St.'});
testModel.makeHidden(['firstName', 'id']);
const data = testModel.toData();
expect(data).toEqual({
address: '123 Main St.'
});
});

it('prioritizes "makeHidden" when overriding both the model\'s "hidden" and "visible" properties with "makeHidden" and "makeVisible" arguments', () => {
it('prioritizes "setHidden" when overriding both the model\'s "hidden" and "visible" properties with "setHidden" and "setVisible" arguments', () => {
testModel.visible = ['lastName', 'address'];
testModel.hidden = ['address'];
const data = testModel.makeVisible('firstName', 'lastName').makeHidden('lastName').toData();
const data = testModel.setVisible(['firstName', 'lastName']).setHidden(['lastName']).toData();

expect(data).toEqual({firstName: 'Joe'});

const knex = require('knex')({ client: 'mysql' });
console.log(knex.client.JoinClause);
});

it('append virtual attribute', () => {
Expand Down
37 changes: 28 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('Model', () => {
});

it('serializes correctly', () => {
testModel.makeVisible('firstName');
testModel.setVisible(['firstName']);
expect(testModel.toJson()).toBe('{"firstName":"Joe"}');
expect(testModel.toString()).toBe('{"firstName":"Joe"}');
});
Expand All @@ -202,32 +202,51 @@ describe('Model', () => {
});

it('hides the fields specified in the model\'s "hidden" property', () => {
expect(testModel.makeHidden('firstName').toData()).toEqual({id: 1, lastName: 'Shmoe', address: '123 Main St.'});
expect(testModel.setHidden(['firstName']).toData()).toEqual({id: 1, lastName: 'Shmoe', address: '123 Main St.'});
testModel.setHidden(['firstName', 'lastName']).makeVisible('firstName');
expect(testModel.getHidden()).toEqual(['lastName']);
expect(testModel.toData()).toEqual({id: 1, firstName: 'Joe', address: '123 Main St.'});
});

it('hides the fields specified in the "options.hidden" property', () => {
testModel.makeHidden(['firstName', 'id']);
testModel.setHidden(['firstName', 'id']);
expect(testModel.toData()).toEqual({lastName: 'Shmoe', address: '123 Main St.'});
});

it('prioritizes "hidden" if there are conflicts when using both "hidden" and "visible"', () => {
testModel.makeVisible('firstName', 'lastName');
testModel.makeHidden('lastName');
testModel.setVisible(['firstName', 'lastName']);
testModel.setHidden(['lastName']);
expect(testModel.toData()).toEqual({ firstName: 'Joe' });
});

it('allows overriding the model\'s "hidden" property with a "setHidden" argument', () => {
testModel.hidden = ['lastName'];
testModel.setHidden(['firstName', 'id']);
const data = testModel.toData();
expect(data).toEqual({
lastName: 'Shmoe',
address: '123 Main St.'
});
});

it('allows overriding the model\'s "hidden" property with a "makeHidden" argument', () => {
testModel.hidden = ['lastName'];
const data = testModel.makeHidden('firstName', 'id').toData();
expect(data).toEqual({address: '123 Main St.'});
testModel.makeHidden(['firstName', 'id']);
const data = testModel.toData();
expect(data).toEqual({
address: '123 Main St.'
});
});

it('prioritizes "makeHidden" when overriding both the model\'s "hidden" and "visible" properties with "makeHidden" and "makeVisible" arguments', () => {
it('prioritizes "setHidden" when overriding both the model\'s "hidden" and "visible" properties with "setHidden" and "setVisible" arguments', () => {
testModel.visible = ['lastName', 'address'];
testModel.hidden = ['address'];
const data = testModel.makeVisible('firstName', 'lastName').makeHidden('lastName').toData();
const data = testModel.setVisible(['firstName', 'lastName']).setHidden(['lastName']).toData();

expect(data).toEqual({firstName: 'Joe'});

const knex = require('knex')({ client: 'mysql' });
console.log(knex.client.JoinClause);
});

it('append virtual attribute', () => {
Expand Down