Skip to content

Commit

Permalink
changing readme
Browse files Browse the repository at this point in the history
  • Loading branch information
runemadsen committed Feb 1, 2013
1 parent 2c248ca commit 5cb6b86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
28 changes: 20 additions & 8 deletions README.md
@@ -1,11 +1,11 @@
CanCan for Backbone.js
======================

Real-world web applications often rely on a combination of server-side and client-side code. If you're building a Rails application, you're probably relying on an auth library like CanCan, and a JS framwork like Backbone.js for client side rendering.
Real-world web applications often rely on a combination of server-side and client-side code. If you're building a Rails application, you're probably relying on an access control library like CanCan, and a JS framwork like Backbone.js for client side rendering.

This library makes it possible to export your CanCan abilities from Ruby to JS, and do the same access checks in the client side. This is great for doing UI-specific functionality, but should of course be backed by an API with tight access control.
This library makes it possible to export your CanCan abilities from Ruby to JS, and do the same access checks on the client side. This is great for doing UI-specific functionality, but should of course be backed by an API with tight access control.

The JS code was adapted directly from the CanCan Ruby code, but without specific functionality like blocks, SQL, etc. See the tests for coverage.
The JS code was adapted directly from the CanCan Ruby code, but without Ruby-specific functionality like blocks, etc. See the tests for coverage.


Setup
Expand All @@ -19,9 +19,6 @@ Then in you Backbone models, implement a class_name var:
var Comment = Backbone.Model.extend({}, {class_name:"Comment"});
```

Usage
------------

In your controller/helper, implement a method that exports you abilities to JSON. This looks something like this:

```
Expand Down Expand Up @@ -49,18 +46,33 @@ In your view, you can now pass the abilities into js:
var ability = new Ability({rules : <%= @js_abilities.to_json.html_safe %>});
````

Now you're all ready to check stuff:
Usage
------------

If you already loaded your abilities into your model, you're all ready to check for access:

```
ability.can("read", Comment);
ability.can("read", "custom");
ability.can("read", new Comment());
```

The methods for setting abilities are renamed to set_:
If you want to set abilities from JS, you need to use the set_ functions:

```
ability.set_can("read", Comment, {id:1});
ability.set_can("read", "somethingelse");
```

It's also possible to pass the name of your Backbone models as strings. It will still work:

```
ability.set_can("index", "Comment");
ability.can("index", Comment) // => true
ability.can("index", "Comment") // => true
ability.set_can("index", Post);
ability.can("index", Post) // => true
ability.can("index", "Post") // => true
Obviously, you need the class_name of your backbone models to correspond to your Rails models.
3 changes: 1 addition & 2 deletions cancan-backbone.js
Expand Up @@ -25,7 +25,6 @@
this.set("rules", _.map(this.get("rules"), function(rule) {
return new Rule(rule);
}));
console.log(this.get("rules"));
}
},

Expand Down Expand Up @@ -161,7 +160,7 @@
// if both are backbone objects (either class or instance) implementing class_name
var sub_class = sub.class_name || sub.constructor.class_name;
var subject_class = subject.class_name || subject.constructor.class_name;
return sub_class && subject_class && sub_class == subject_class;
return (sub_class && subject_class && sub_class == subject_class) || sub == subject_class || sub_class == subject;
});
},

Expand Down
15 changes: 14 additions & 1 deletion test/ability.js
Expand Up @@ -16,11 +16,13 @@ var Comment = Backbone.Model.extend({
test( "should work when passing in existing object with rules and subjects", function() {
var existing = { rules : [
{
"base_behavior":true,
"subjects":["Post"],
"actions":["index","show"],
"conditions":{"id":1}
},
{
"base_behavior":true,
"subjects":["Comment"],
"actions":["index","show","new","create"],
"conditions":{"post_id":1}
Expand All @@ -38,13 +40,24 @@ test( "should work when passing in existing object with rules and subjects", fun
ok(a.cannot("new", new Comment({post_id:2})));
});


test( "should work on backbone model", function() {
var a = new Ability();
a.set_can("read", Post);
ok(a.can("read", Post));
});

test( "should work on backbone model name as string", function() {
var a = new Ability();
a.set_can("read", Post);
ok(a.can("read", "Post"));
});

test( "should work on backbone model name as string the other way around", function() {
var a = new Ability();
a.set_can("read", "Post");
ok(a.can("read", Post));
});

test( "should be able to 'read' anything", function() {
var a = new Ability();
a.set_can("read", "all");
Expand Down

0 comments on commit 5cb6b86

Please sign in to comment.