Skip to content

Commit

Permalink
Add documentation for subproperty feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ratierd committed Aug 3, 2020
1 parent 3b49ff3 commit 28d8373
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,53 @@ Cannot helper is a negation of `can` helper with the same API.
{{cannot "doSth in myModel" model extraProperties}}
```

### `ability`

Ability helper will return the ability property as is instead of as a boolean like with can/cannot.
It allows you to return objects in your abilitie's property as long as you include a 'can' property that represents the usual boolean ability you would return in a more classic scenario.

```js
// app/abilities/post.js

import { computed } from '@ember/object';
import { Ability } from 'ember-can';

export default Ability.extend({
// only an admin can edit a post, if and only the post is editable
canEdit: computed('user.isAdmin', 'model.isNotEditable', function() {
if (!this.get('model.isNotEditable')) {
return {
can: false,
reason: 'This post cannot be edited'
}
}

if (!this.get('user.isAdmin')) {
return {
can: false,
reason: 'You need to be an admin to edit a post'
}
}

return true;
})
});
```

```hbs
{{ability "write post" post}}
{{!-- returns { can: ..., reason: ... } or true --}}
{{ability "write post:reason" post}}
{{!-- returns 'This post cannot be edited', 'You need to be an admin to edit a post' or undefined --}}
{{#if (can "write post" post)}}
<p>A post</p>
{{else}}
{{#with (ability "write post:reason" post) as |cannotEditPostReason|}}
{{cannotEditPostReason}}
{{/with}}
{{/if}}
```

## Abilities

Expand Down

0 comments on commit 28d8373

Please sign in to comment.