angularjs-rails-resource 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  module Angularjs
2
2
  module Rails
3
3
  module Resource
4
- VERSION = "0.1.7"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,4 +1,4 @@
1
- describe("http setting", function () {
1
+ describe('http setting', function () {
2
2
  'use strict';
3
3
 
4
4
  beforeEach(module('rails'));
@@ -0,0 +1,105 @@
1
+ describe('transformers', function () {
2
+ 'use strict';
3
+ var $httpBackend, $rootScope, factory, Test, testInterceptor,
4
+ config = {
5
+ url: '/test',
6
+ name: 'test'
7
+ };
8
+
9
+ beforeEach(function() {
10
+ module('rails');
11
+
12
+ angular.module('rails').factory('railsTestInterceptor', function () {
13
+ return function (promise) {
14
+ return promise.then(function (response) {
15
+ response.data.interceptorAdded = 'x';
16
+ return response;
17
+ });
18
+ }
19
+ });
20
+ });
21
+
22
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor) {
23
+ $httpBackend = _$httpBackend_;
24
+ $rootScope = _$rootScope_;
25
+ factory = railsResourceFactory;
26
+ Test = railsResourceFactory(config);
27
+ testInterceptor = railsTestInterceptor;
28
+ }));
29
+
30
+ afterEach(function() {
31
+ $httpBackend.verifyNoOutstandingExpectation();
32
+ $httpBackend.verifyNoOutstandingRequest();
33
+ });
34
+
35
+
36
+ it('should be able to reference interceptor using name', function() {
37
+ var promise, result, Resource, testConfig = {};
38
+
39
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
40
+
41
+ angular.copy(config, testConfig);
42
+ testConfig.responseInterceptors = ['railsTestInterceptor'];
43
+ Resource = factory(testConfig);
44
+
45
+ expect(promise = Resource.get(123)).toBeDefined();
46
+
47
+ promise.then(function (response) {
48
+ result = response;
49
+ });
50
+
51
+ $httpBackend.flush();
52
+
53
+ expect(result).toBeInstanceOf(Resource);
54
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
55
+ });
56
+
57
+ it('should be able to add interceptor using reference', function() {
58
+ var promise, result, Resource, testConfig = {};
59
+
60
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
61
+
62
+
63
+ angular.copy(config, testConfig);
64
+ testConfig.responseInterceptors = [testInterceptor];
65
+ Resource = factory(testConfig);
66
+
67
+ expect(promise = Resource.get(123)).toBeDefined();
68
+
69
+ promise.then(function (response) {
70
+ result = response;
71
+ });
72
+
73
+ $httpBackend.flush();
74
+
75
+ expect(result).toBeInstanceOf(Resource);
76
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
77
+ });
78
+
79
+ it('should be able to add interceptor using beforeResponse', function() {
80
+ var promise, result, Resource, interceptorCalled = false;
81
+
82
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
83
+
84
+ Resource = factory(config);
85
+
86
+ Resource.beforeResponse(function (resource, constructor) {
87
+ expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
88
+ expect(constructor).toEqual(Resource);
89
+ interceptorCalled = true;
90
+ });
91
+
92
+ expect(promise = Resource.get(123)).toBeDefined();
93
+
94
+ promise.then(function (response) {
95
+ result = response;
96
+ });
97
+
98
+ $httpBackend.flush();
99
+
100
+ expect(result).toBeInstanceOf(Resource);
101
+ expect(result).toEqualData({id: 123, abcDef: 'xyz'});
102
+ expect(interceptorCalled).toBeTruthy();
103
+ });
104
+
105
+ });
@@ -1,4 +1,4 @@
1
- describe("nested urls", function () {
1
+ describe('nested urls', function () {
2
2
  'use strict';
3
3
 
4
4
  beforeEach(module('rails'));
@@ -135,48 +135,6 @@ describe("nested urls", function () {
135
135
  $httpBackend.flush();
136
136
  }));
137
137
 
138
- it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) {
139
- var promise, result, resource;
140
-
141
- $httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
142
-
143
- resource = factory(nestedConfig);
144
- resource.responseInterceptors = [];
145
- resource.requestTransformers = [];
146
- expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
147
-
148
- promise.then(function (response) {
149
- result = response;
150
- });
151
-
152
- $httpBackend.flush();
153
-
154
- expect(result).toBeInstanceOf(resource);
155
- expect(result).toEqualData({id: 123, nested_id: 1234, abc_def: 'xyz'});
156
- }));
157
-
158
- it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) {
159
- var promise, result, resource, testConfig = {};
160
-
161
- $httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
162
-
163
- angular.copy(nestedConfig, testConfig);
164
- testConfig.requestTransformers = [];
165
- testConfig.responseInterceptors = ['railsFieldRenamingInterceptor'];
166
- resource = factory(testConfig);
167
-
168
- expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
169
-
170
- promise.then(function (response) {
171
- result = response;
172
- });
173
-
174
- $httpBackend.flush();
175
-
176
- expect(result).toBeInstanceOf(resource);
177
- expect(result).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
178
- }));
179
-
180
138
  it('should be able to create new instance and save it', inject(function($httpBackend) {
181
139
  var data = new NestedTest({nestedId: 1234, abcDef: 'xyz'});
182
140
 
@@ -198,7 +156,7 @@ describe("nested urls", function () {
198
156
 
199
157
  expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
200
158
 
201
- $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, xyz: 'abc', abc_def: 'xyz', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
159
+ $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {abc_def: 'xyz', nested_id: 1234, id: 123, xyz: 'abc'}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
202
160
  data.xyz = 'abc';
203
161
  data.update();
204
162
  $httpBackend.flush();
@@ -238,7 +196,7 @@ describe("nested urls", function () {
238
196
  expect(result).toBeInstanceOf(NestedTest);
239
197
  expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
240
198
 
241
- $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, abc: 'xyz', xyz: 'abc', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
199
+ $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, nested_id: 1234, abc: 'xyz', xyz: 'abc'}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
242
200
  result.xyz = 'abc';
243
201
  result.update();
244
202
  $httpBackend.flush();
@@ -263,7 +221,7 @@ describe("nested urls", function () {
263
221
  expect(result).toBeInstanceOf(NestedTest);
264
222
  expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
265
223
 
266
- $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, abc: 'xyz', xyz: 'abc', nested_id: 1234}}).respond(204);
224
+ $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, nested_id: 1234, abc: 'xyz', xyz: 'abc'}}).respond(204);
267
225
  result.xyz = 'abc';
268
226
  result.update();
269
227
  $httpBackend.flush();
@@ -292,7 +250,7 @@ describe("nested urls", function () {
292
250
  $httpBackend.flush();
293
251
  }));
294
252
 
295
- it('should be able to create new instance and update it', inject(function($httpBackend) {
253
+ it('should be able to create new instance and delete it', inject(function($httpBackend) {
296
254
  var data = new NestedTest({abcDef: 'xyz'});
297
255
 
298
256
  $httpBackend.expectPOST('/nested/1234/test', {nested_test: {abc_def: 'xyz', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz'}});
@@ -0,0 +1,128 @@
1
+ describe('railsResourceFactory', function () {
2
+ 'use strict';
3
+
4
+
5
+ beforeEach(function () {
6
+ module('rails');
7
+
8
+ angular.module('rails').factory('BookSerializer', function(railsSerializer) {
9
+ return railsSerializer(function () {
10
+ this.exclude('publicationDate');
11
+ });
12
+ });
13
+ });
14
+
15
+ describe('serialization', function() {
16
+ var $httpBackend, $rootScope, factory, Author, Book,
17
+ config = {
18
+ url: '/test',
19
+ name: 'test'
20
+ };
21
+
22
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsSerializer) {
23
+ $httpBackend = _$httpBackend_;
24
+ $rootScope = _$rootScope_;
25
+ factory = railsResourceFactory;
26
+
27
+ Author = railsResourceFactory({
28
+ url: '/authors',
29
+ name: 'author',
30
+ serializer: railsSerializer(function () {
31
+ this.exclude('birthDate', 'books');
32
+ })
33
+ });
34
+
35
+ Book = railsResourceFactory({
36
+ url: '/books',
37
+ name: 'book',
38
+ serializer: 'BookSerializer'
39
+ });
40
+ }));
41
+
42
+ afterEach(function() {
43
+ $httpBackend.verifyNoOutstandingExpectation();
44
+ $httpBackend.verifyNoOutstandingRequest();
45
+ });
46
+
47
+ it('create should exclude serializer exclusions', inject(function($httpBackend) {
48
+ var promise, result;
49
+ var data = new Author({firstName: 'George', lastName: 'Martin', middleName: 'R. R.', birthDate: '1948-09-20', books: [{id: 1, title: 'A Game of Thrones'}]});
50
+
51
+ $httpBackend.expectPOST('/authors', {author: {first_name: 'George', last_name: 'Martin', middle_name: 'R. R.'}}).respond(200, {author: {id: 123, first_name: 'George', last_name: 'Martin', middle_name: 'R. R.'}});
52
+ data.create();
53
+ $httpBackend.flush();
54
+ }));
55
+
56
+ it('create should use factory serializer', inject(function($httpBackend) {
57
+ var promise, result;
58
+ var data = new Book({title: 'A Game of Thrones', publicationDate: '1996-08-06', pages: 694});
59
+
60
+ $httpBackend.expectPOST('/books', {book: {title: 'A Game of Thrones', pages: 694}}).respond(200, {book: {id: 123, title: 'A Game of Thrones', pages: 694}});
61
+ data.create();
62
+ $httpBackend.flush();
63
+ }));
64
+ });
65
+
66
+ describe('nested resource serialization', function() {
67
+ var $httpBackend, $rootScope, factory, Author, Book,
68
+ config = {
69
+ url: '/test',
70
+ name: 'test'
71
+ };
72
+
73
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsSerializer) {
74
+ $httpBackend = _$httpBackend_;
75
+ $rootScope = _$rootScope_;
76
+ factory = railsResourceFactory;
77
+
78
+ Book = railsResourceFactory({
79
+ url: '/books',
80
+ name: 'book',
81
+ serializer: 'BookSerializer'
82
+ });
83
+
84
+ Author = railsResourceFactory({
85
+ url: '/authors',
86
+ name: 'author',
87
+ serializer: railsSerializer(function () {
88
+ this.resource('books', Book)
89
+ this.exclude('birthDate');
90
+ })
91
+ });
92
+
93
+ }));
94
+
95
+ afterEach(function() {
96
+ $httpBackend.verifyNoOutstandingExpectation();
97
+ $httpBackend.verifyNoOutstandingRequest();
98
+ });
99
+
100
+ it('create should use nested resource serializer', inject(function($httpBackend) {
101
+ var promise, result;
102
+ var data = new Author({firstName: 'George', lastName: 'Martin', middleName: 'R. R.', birthDate: '1948-09-20', books: [{id: 1, title: 'A Game of Thrones', publicationDate: '1996-08-06', pages: 694}]});
103
+
104
+ $httpBackend.expectPOST('/authors', {author: {first_name: 'George', last_name: 'Martin', middle_name: 'R. R.', books: [{id: 1, title: 'A Game of Thrones', pages: 694}]}})
105
+ .respond(200, {author: {id: 123, first_name: 'George', last_name: 'Martin', middle_name: 'R. R.'}});
106
+ data.create();
107
+ $httpBackend.flush();
108
+ }));
109
+
110
+ it('create should construct nested resource objects', inject(function($httpBackend) {
111
+ var promise, result;
112
+
113
+ $httpBackend.expectGET('/authors/1').respond(200, {author: {id: 123, first_name: 'George', last_name: 'Martin', middle_name: 'R. R.', books: [{id: 1, title: 'A Game of Thrones', publication_date: '1996-08-06', pages: 694}]}});
114
+ expect(promise = Author.get(1)).toBeDefined();
115
+
116
+ promise.then(function (response) {
117
+ result = response;
118
+ });
119
+
120
+ $httpBackend.flush();
121
+
122
+ expect(result).toBeInstanceOf(Author);
123
+ expect(result['books']).toBeDefined();
124
+ expect(result['books'].length).toBe(1);
125
+ expect(result['books'][0]).toBeInstanceOf(Book);
126
+ }));
127
+ });
128
+ });
@@ -1,20 +1,32 @@
1
- describe("railsResourceFactory", function () {
1
+ describe('railsResourceFactory', function () {
2
2
  'use strict';
3
3
 
4
- beforeEach(module('rails'));
4
+ beforeEach(function() {
5
+ module('rails');
6
+
7
+ angular.module('rails').factory('railsTestInterceptor', function () {
8
+ return function (promise) {
9
+ return promise.then(function (response) {
10
+ response.data.interceptorAdded = 'x';
11
+ return response;
12
+ });
13
+ }
14
+ });
15
+ });
5
16
 
6
17
  describe('singular', function() {
7
- var $httpBackend, $rootScope, factory, Test,
18
+ var $httpBackend, $rootScope, factory, Test, testInterceptor,
8
19
  config = {
9
20
  url: '/test',
10
21
  name: 'test'
11
22
  };
12
23
 
13
- beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
24
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor) {
14
25
  $httpBackend = _$httpBackend_;
15
26
  $rootScope = _$rootScope_;
16
27
  factory = railsResourceFactory;
17
28
  Test = railsResourceFactory(config);
29
+ testInterceptor = railsTestInterceptor;
18
30
  }));
19
31
 
20
32
  afterEach(function() {
@@ -153,48 +165,6 @@ describe("railsResourceFactory", function () {
153
165
  $httpBackend.flush();
154
166
  }));
155
167
 
156
- it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) {
157
- var promise, result, resource;
158
-
159
- $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
160
-
161
- resource = factory(config);
162
- resource.responseInterceptors = [];
163
- resource.requestTransformers = [];
164
- expect(promise = resource.get(123)).toBeDefined();
165
-
166
- promise.then(function (response) {
167
- result = response;
168
- });
169
-
170
- $httpBackend.flush();
171
-
172
- expect(result).toBeInstanceOf(resource);
173
- expect(result).toEqualData({id: 123, abc_def: 'xyz'});
174
- }));
175
-
176
- it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) {
177
- var promise, result, resource, testConfig = {};
178
-
179
- $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
180
-
181
- angular.copy(config, testConfig);
182
- testConfig.requestTransformers = [];
183
- testConfig.responseInterceptors = ['railsFieldRenamingInterceptor'];
184
- resource = factory(testConfig);
185
-
186
- expect(promise = resource.get(123)).toBeDefined();
187
-
188
- promise.then(function (response) {
189
- result = response;
190
- });
191
-
192
- $httpBackend.flush();
193
-
194
- expect(result).toBeInstanceOf(resource);
195
- expect(result).toEqualData({id: 123, abcDef: 'xyz'});
196
- }));
197
-
198
168
  it('should be able to create new instance and save it', inject(function($httpBackend) {
199
169
  var data = new Test({abcDef: 'xyz'});
200
170
 
@@ -233,7 +203,7 @@ describe("railsResourceFactory", function () {
233
203
 
234
204
  expect(data).toEqualData({id: 123, abcDef: 'xyz'});
235
205
 
236
- $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: 'abc', abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
206
+ $httpBackend.expectPUT('/test/123', {test: {abc_def: 'xyz', id: 123, xyz: 'abc'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
237
207
  data.xyz = 'abc';
238
208
  data.update();
239
209
  $httpBackend.flush();
@@ -250,7 +220,7 @@ describe("railsResourceFactory", function () {
250
220
 
251
221
  expect(data).toEqualData({id: 123, abcDef: 'xyz'});
252
222
 
253
- $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: 'abc', abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
223
+ $httpBackend.expectPUT('/test/123', {test: {abc_def: 'xyz', id: 123, xyz: 'abc'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
254
224
  data.xyz = 'abc';
255
225
  data.save();
256
226
  $httpBackend.flush();
@@ -1,4 +1,4 @@
1
- describe("root wrapping", function () {
1
+ describe('root wrapping', function () {
2
2
  'use strict';
3
3
 
4
4
  beforeEach(module('rails'));