Skip to content

Commit de6283e

Browse files
authored
Merge pull request #4 from Exelord/fix-package
Fix package
2 parents fb51f4f + 699d563 commit de6283e

File tree

7 files changed

+80
-35
lines changed

7 files changed

+80
-35
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ You can define your custom options in your `config/environment.js` file
7777
``` js
7878
module.exports = function(environment) {
7979
var ENV = {
80-
'ember-api-actions': {
80+
'emberCustomActions': {
8181
type: 'PUT',
8282
ajaxOptions: {},
8383
pushToStore: false,

addon/actions/action.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import UrlBuilder from '../utils/url-builder';
33
import normalizePayload from '../utils/normalize-payload';
44
import defaultConfig from '../config';
55

6-
const { assign, getOwner, computed, isArray, ArrayProxy, Object } = Ember;
6+
const { assign, getOwner, computed, isArray, ArrayProxy, ObjectProxy, Object } = Ember;
77

88
export default Object.extend({
99
model: null,
1010
options: {},
1111
payload: {},
1212
instance: false,
1313

14-
store: computed('model', function() {
15-
return this.get('model').store;
16-
}),
14+
store: computed.reads('model.store'),
1715

1816
modelName: computed('model', function() {
1917
let { constructor } = this.get('model');
@@ -29,22 +27,25 @@ export default Object.extend({
2927
}),
3028

3129
appConfig: computed('model', function() {
32-
return getOwner(this.get('model')).resolveRegistration('config:environment').emberApiActions || {};
30+
let config = getOwner(this.get('model')).resolveRegistration('config:environment').emberApiActions || {};
31+
return Object.create(config);
3332
}),
3433

35-
config: computed('model', 'options', function() {
36-
return assign(defaultConfig, this.get('appConfig'), this.get('options'));
34+
defaultConfig: computed(function() {
35+
return Object.create(defaultConfig);
3736
}),
3837

39-
requestType: computed('config', function() {
40-
return this.get('config').type.toUpperCase();
38+
config: computed('defaultConfig', 'options', 'appConfig', function() {
39+
return Object.create(assign(this.get('defaultConfig'), this.get('appConfig'), this.get('options')));
4140
}),
4241

43-
urlType: computed('config', 'requestType', function() {
44-
return this.get('config').urlType || this.get('requestType');
42+
requestType: computed('config.type', function() {
43+
return this.get('config.type').toUpperCase();
4544
}),
4645

47-
url: computed('model', 'path', 'urlType', 'instance', function() {
46+
urlType: computed.or('config.urlType', 'requestType'),
47+
48+
url: computed('model', 'path', 'urlType', 'instance', 'adapter', function() {
4849
return UrlBuilder.create({
4950
path: this.get('path'),
5051
adapter: this.get('adapter'),
@@ -54,15 +55,15 @@ export default Object.extend({
5455
}).build();
5556
}),
5657

57-
data: computed('config', 'payload', function() {
58-
let payload = (this.get('payload') instanceof Object) ? this.get('payload') : {};
59-
let data = normalizePayload(payload, this.get('config').normalizeOperation);
60-
return assign(this.get('config').ajaxOptions, { data });
58+
data: computed('config.{normalizeOperation,ajaxOptions}', 'payload', function() {
59+
let payload = (this.get('payload') instanceof window.Object) ? this.get('payload') : {};
60+
let data = normalizePayload(payload, this.get('config.normalizeOperation'));
61+
return assign(this.get('config.ajaxOptions'), { data });
6162
}),
6263

6364
callAction() {
6465
return this.get('adapter').ajax(this.get('url'), this.get('requestType'), this.get('data')).then((response) => {
65-
if (this.get('config').pushToStore && response.data) {
66+
if (this.get('config.pushToStore') && response.data) {
6667
return this._pushToStore(this.get('serializer').pushPayload(this.get('store'), response));
6768
} else {
6869
return response;
@@ -71,10 +72,7 @@ export default Object.extend({
7172
},
7273

7374
_pushToStore(content) {
74-
if (isArray(content)) {
75-
return ArrayProxy.create({ content });
76-
} else {
77-
return content;
78-
}
75+
let proxy = isArray(content) ? ArrayProxy : ObjectProxy;
76+
return proxy.create({ content });
7977
}
8078
});

addon/actions/model.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import Ember from 'ember';
12
import Action from './action';
23

4+
const { assign } = Ember;
5+
36
export default function(path, options = {}) {
4-
return function(payload = {}) {
7+
return function(payload = {}, customOptions = {}) {
58
return Action.create({
69
model: this,
710
instance: true,
8-
options,
11+
options: assign(options, customOptions),
912
payload,
1013
path
1114
}).callAction();

addon/actions/resource.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import Ember from 'ember';
12
import Action from './action';
23

4+
const { assign } = Ember;
5+
36
export default function(path, options = {}) {
4-
return function(payload = {}) {
7+
return function(payload = {}, customOptions = {}) {
58
return Action.create({
69
model: this,
10+
options: assign(options, customOptions),
711
path,
8-
options,
912
payload
1013
}).callAction();
1114
};

addon/utils/normalize-payload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function transformObject(object, operation) {
77
let data = {};
88

99
Object.keys(object).forEach((key) => {
10-
data[key[operation]()] = transformObject(object[key], operation);
10+
data[String[operation](key)] = transformObject(object[key], operation);
1111
});
1212

1313
return data;

tests/dummy/app/models/post.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { modelAction, resourceAction } from 'ember-custom-actions';
33

44
export default Model.extend({
55
publish: modelAction('publish'),
6-
list: resourceAction('list')
6+
list: resourceAction('list'),
7+
search: resourceAction('search', { type: 'GET', normalizeOperation: 'dasherize' })
78
});

tests/unit/models/post-test.js

+46-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ moduleForModel('post', 'Unit | Model | post', {
1414
});
1515

1616
test('model action', function(assert) {
17-
this.server.put('/posts/:id/publish', () => {
17+
assert.expect(3);
18+
19+
this.server.put('/posts/:id/publish', (request) => {
20+
let data = JSON.parse(request.requestBody);
21+
assert.deepEqual(data, { myParam: 'My first param' });
22+
assert.equal(request.url, '/posts/1/publish');
23+
1824
return [200, { }, 'true'];
1925
});
2026

@@ -31,9 +37,13 @@ test('model action', function(assert) {
3137
});
3238

3339
test('model action pushes to store', function(assert) {
34-
assert.expect(3);
40+
assert.expect(5);
41+
42+
this.server.put('/posts/:id/publish', (request) => {
43+
let data = JSON.parse(request.requestBody);
44+
assert.deepEqual(data, { myParam: 'My first param' });
45+
assert.equal(request.url, '/posts/1/publish');
3546

36-
this.server.put('/posts/:id/publish', () => {
3747
return [200, {}, '{"data": {"id": 2, "type": "Post"}}'];
3848
});
3949

@@ -53,7 +63,13 @@ test('model action pushes to store', function(assert) {
5363
});
5464

5565
test('resource action', function(assert) {
56-
this.server.put('/posts/list', () => {
66+
assert.expect(3);
67+
68+
this.server.put('/posts/list', (request) => {
69+
let data = JSON.parse(request.requestBody);
70+
assert.deepEqual(data, { myParam: 'My first param' });
71+
assert.equal(request.url, '/posts/list');
72+
5773
return [200, { }, 'true'];
5874
});
5975

@@ -69,10 +85,34 @@ test('resource action', function(assert) {
6985
});
7086
});
7187

72-
test('resource action pushes to store', function(assert) {
88+
test('resource action with params in GET', function(assert) {
7389
assert.expect(3);
7490

75-
this.server.put('/posts/list', () => {
91+
this.server.get('/posts/search', (request) => {
92+
assert.equal(request.url, '/posts/search?my-param=My%20first%20param');
93+
assert.equal(request.requestHeaders.test, 'Custom header');
94+
return [200, { }, 'true'];
95+
});
96+
97+
let done = assert.async();
98+
let payload = { myParam: 'My first param' };
99+
100+
let model = this.subject();
101+
model.set('id', 1);
102+
model.search(payload, { ajaxOptions: { headers: { test: 'Custom header' } } }).then((response) => {
103+
assert.ok(response, true);
104+
done();
105+
});
106+
});
107+
108+
test('resource action pushes to store', function(assert) {
109+
assert.expect(5);
110+
111+
this.server.put('/posts/list', (request) => {
112+
let data = JSON.parse(request.requestBody);
113+
assert.deepEqual(data, { myParam: 'My first param' });
114+
assert.equal(request.url, '/posts/list');
115+
76116
return [200, {}, '{"data": [{"id": "2", "type": "post"},{"id": "3", "type": "post"}]}'];
77117
});
78118

0 commit comments

Comments
 (0)