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

Ember can v2 #94

Merged
merged 25 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions addon/helpers/can.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ export default Helper.extend({

_removeAbilityObserver() {
this.removeObserver(`ability.${this.get('propertyName')}`, this, 'recompute');

let ability = this.get('ability')

if (ability) {
ability.destroy();
}
}
});
15 changes: 8 additions & 7 deletions addon/services/can.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Service from '@ember/service';
import { assert } from '@ember/debug';
import { getOwner } from '@ember/application';
import { assign } from '@ember/polyfills';
import { run } from '@ember/runloop';

import normalizeAbilityString from 'ember-can/utils/normalize';

Expand All @@ -10,16 +11,16 @@ export default Service.extend({
return normalizeAbilityString(abilityString);
},

abilityFor(abilityName, model, properties = {}) {
let Ability = getOwner(this).factoryFor(`ability:${abilityName}`);

return this.abilityFor(...arguments);
},
assert(`No ability type found for '${abilityName}'`, Ability);

abilityFor(abilityName, model, properties = {}) {
let ability = getOwner(this).lookup(`ability:${abilityName}`);
assert(`No ability type found for ${abilityName}`, ability);
if (typeof model != 'undefined') {
properties = assign({}, { model }, properties);
}

ability.setProperties(assign({}, { model }, properties));
return ability;
return Ability.create(properties);
},

can(abilityString, model, properties) {
Expand Down
35 changes: 33 additions & 2 deletions tests/integration/helpers/can-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,51 @@ module('Integration | Helper | can', function(hooks) {
});

test('it can receives model', async function(assert) {
assert.expect(2);
assert.expect(4);

this.owner.register('ability:post', Ability.extend({
canWrite: computed('model.write', function() {
return this.get('model.write');
}),
}));

this.set('model', { write: false });
await render(hbs`{{if (can "write post" model) "true" "false"}}`);
assert.equal(this.element.textContent.trim(), 'false');

this.set('model', { write: false });
assert.equal(this.element.textContent.trim(), 'false');

this.set('model', { write: true });
assert.equal(this.element.textContent.trim(), 'true');

this.set('model', null);
assert.equal(this.element.textContent.trim(), 'false');
});

test('it works with default model', async function(assert) {
assert.expect(4);

this.owner.register('ability:post', Ability.extend({
model: computed(function() {
return { write: true }
}),

canWrite: computed('model.write', function() {
return this.get('model.write');
}),
}));

await render(hbs`{{if (can "write post" model) "true" "false"}}`);
assert.equal(this.element.textContent.trim(), 'true');

this.set('model', undefined);
assert.equal(this.element.textContent.trim(), 'true');

this.set('model', null);
assert.equal(this.element.textContent.trim(), 'false');

this.set('model', { write: false });
assert.equal(this.element.textContent.trim(), 'false');
});

test('it can receives properties', async function(assert) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/services/can-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module('Unit | Service | can', function(hooks) {
}),
}));

assert.ok(service.can('touchThis in superModel', { yeah: true }));
assert.equal(service.can('touchThis in superModel', { yeah: true }), true);
});

test('cannot', function(assert) {
Expand All @@ -63,6 +63,6 @@ module('Unit | Service | can', function(hooks) {
}),
}));

assert.notOk(service.can('touchThis in superModel', { yeah: false }));
assert.equal(service.can('touchThis in superModel', { yeah: false }), false);
});
});