Skip to content

Commit

Permalink
Add option to remove trailing slashes
Browse files Browse the repository at this point in the history
This adds a new option to the adapter config and supporting tests and
documentation.  Documentation adds a new "configuring" section with some
back-filling for other options that already exist.
  • Loading branch information
Dustin Farris committed Jan 10, 2015
1 parent ca9ad7d commit 135634b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 3 deletions.
10 changes: 8 additions & 2 deletions addon/adapters/drf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Ember from 'ember';
*/
export default DS.RESTAdapter.extend({
defaultSerializer: "DS/djangoREST",
add_trailing_slashes: true,

init: function() {
this._super();
Expand Down Expand Up @@ -53,6 +54,9 @@ export default DS.RESTAdapter.extend({
* If an ID is specified, it adds the ID to the path generated for
* the type, separated by a `/`.
*
* If the adapter has the property `add_trailing_slashes` set to
* true, a trailing slash will be appended to the result.
*
* @method buildURL
* @param {String} type
* @param {String} id
Expand All @@ -61,8 +65,10 @@ export default DS.RESTAdapter.extend({
*/
buildURL: function(type, id, record) {
var url = this._super(type, id, record);
if (url.charAt(url.length - 1) !== '/') {
url += '/';
if (this.get('add_trailing_slashes')) {
if (url.charAt(url.length - 1) !== '/') {
url += '/';
}
}
return url;
},
Expand Down
4 changes: 4 additions & 0 deletions app/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ export default DRFAdapter.extend({

namespace: function() {
return ENV.APP.API_NAMESPACE;
}.property(),

add_trailing_slashes: function() {
return ENV.APP.API_ADD_TRAILING_SLASHES;
}.property()
});
3 changes: 2 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module.exports = function(/* environment, appConfig */) {
var ENV = {
APP: {
API_HOST: '',
API_NAMESPACE: 'api'
API_NAMESPACE: 'api',
API_ADD_TRAILING_SLASHES: true
}
};
return ENV;
Expand Down
51 changes: 51 additions & 0 deletions docs/configuring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Configuring

There are a number of configuration variables you can set in your environment.js file.


## API_HOST

**Default:** `''`

The fully-qualified host of your API server.


## API_NAMESPACE

**Default:** `'api'`

The URL prefix (namespace) for your API. In other words, if you set this to my-api/v1, then all
API requests will look like /my-api/v1/users/56/, or similar.


## API_ADD_TRAILING_SLASHES

**Default:** `true`

Whether to append a trailing slash to API URL endpoints.


## Example

```js
// my-ember-cli-project/config/environment.js

module.exports = function(environment) {
var ENV = {
APP: {
API_ADD_TRAILING_SLASHES: false
}
};

if (environment === 'development') {
ENV.APP.API_HOST = 'http://localhost:8000';
}

if (environment === 'production') {
ENV.APP.API_HOST = 'https://api.myproject.com';
ENV.APP.NAMESPACE = 'v2';
}

return ENV;
};
```
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ In your Ember CLI project, install Ember Django Adapter from the command line:
```bash
npm install --save-dev ember-django-adapter
```

See [configuring](configuring.md) for more information on customizing the adapter.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ site_name: Ember Django Adapter
repo_url: https://github.com/dustinfarris/ember-django-adapter
pages:
- ['index.md', 'Introduction']
- ['configuring.md']
- ['pagination.md']
- ['contributing.md']
- ['changelog.md']
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/adapters/drf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ test('buildURL', function() {
equal(adapter.buildURL('FurryAnimals', 5, null), 'test-host/test-api/furry-animals/5/');
});

test('buildURL - no trailing slashes', function() {
var adapter = this.subject();
adapter.set('add_trailing_slashes', false);
equal(adapter.buildURL('Animal', 5, null), 'test-host/test-api/animals/5');
equal(adapter.buildURL('FurryAnimals', 5, null), 'test-host/test-api/furry-animals/5');
});

test('ajaxError - returns invalid error if 400 response', function() {
var error = new DS.InvalidError({errors: {name: ['This field cannot be blank.']}});

Expand Down

0 comments on commit 135634b

Please sign in to comment.