angularjs-rails-resource 1.0.2 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e03c98a5ea5807396c9246d9a2183c8861fbf6ec
4
- data.tar.gz: 06ddc4ec3b0d2c5549783a738f35f7b28fa583ac
3
+ metadata.gz: 4bfbacbc10e68b92315a301e1419852f77b4c028
4
+ data.tar.gz: 73c370919edead2bd08430a3551513196595389d
5
5
  SHA512:
6
- metadata.gz: 047426f09ba305b7d347623406e95d1c04b94726b8e3d3bd26fbc8b92bdb3c69a4d5a08821d1bc98625fd4630bd13c4fc5cbf8148d7e3f3a3ca4bb93e0adf44c
7
- data.tar.gz: 386dfb97cb7fec33ae06a5b91f9bb0925750090f748005e55a1d6e8af1ff0f8cdf690487d873234670106182f232b5d7e9fccb94e92f214217b52cfa45d190ea
6
+ metadata.gz: 11b7134336f0441bdd6ee5cec43b5d683f82d1d1e965d55f50a174c69eca431ee7b927dc903bb43f57f1038fcd5fd75a54734ffb46cfa53c7a62bc27f8b30bc7
7
+ data.tar.gz: 805dbd40dcc11ce20b3773c04fb974a5b2b2ae2c5f6a0e452a455c1c222da60f32ff3b8378413d5f12fe4dcaf530bb030beae592741e3889dce3a556b86b53f3
data/.gitignore CHANGED
@@ -19,4 +19,5 @@ test_out
19
19
  atlassian-ide-plugin.xml
20
20
  node_modules
21
21
  build/
22
+ .idea/
22
23
 
@@ -1,3 +1,11 @@
1
+ <a name="1.1.0"></a>
2
+ # 1.1.0
3
+ ## Bug Fixes
4
+
5
+ ## Features
6
+ - Added <code>idAttribute</code> configuration option to allow customizing the unique id field on resources. - #114 (@inlineblock)
7
+ - Added <code>get</code> instance method to refresh resources from the server. - #115 (@wwilkins)
8
+
1
9
  <a name="1.0.1"></a>
2
10
  # 1.0.1
3
11
  ## Bug Fixes
data/README.md CHANGED
@@ -197,6 +197,7 @@ defined on the resource can be called multiple times to adjust properties as nee
197
197
  * **name** - This is the name used for root wrapping when dealing with singular instances.
198
198
  * **pluralName** *(optional)* - If specified this name will be used for unwrapping array results. If not specified then the serializer's [pluralize](#serializers) method is used to calculate
199
199
  the plural name from the singular name.
200
+ * **idAttribute** *(optional)* - (Default: 'id') Configures what field on the record represents the unique identifier.
200
201
  * **httpConfig** *(optional)* - By default we will add the following headers to ensure that the request is processed as JSON by Rails. You can specify additional http config options or override any of the defaults by setting this property. See the [AngularJS $http API](http://docs.angularjs.org/api/ng.$http) for more information.
201
202
  * **headers**
202
203
  * **Accept** - application/json
@@ -352,6 +353,9 @@ All of the instance methods will update the instance in-place on response and wi
352
353
  * $url(path) - Returns this Resource instance's URL with the optional path appended if provided.
353
354
  * **path** {string} (optional) - A path to append to the resource's URL.
354
355
 
356
+ * get() - Refreshes the instance from the server.
357
+ * **returns** {promise} - A promise that will be resolved with the instance itself
358
+
355
359
  * create() - Submits the resource instance to the resource's base URL (e.g. /books) using a POST
356
360
  * **returns** {promise} - A promise that will be resolved with the instance itself
357
361
 
data/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angularjs-rails-resource",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "main": "angularjs-rails-resource.js",
5
5
  "description": "A resource factory inspired by $resource from AngularJS",
6
6
  "repository": {
@@ -1,7 +1,7 @@
1
1
  module Angularjs
2
2
  module Rails
3
3
  module Resource
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.1'
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "angularjs-rails-resource",
3
3
  "description" : "A resource factory inspired by $resource from AngularJS",
4
- "version": "1.0.2",
4
+ "version": "1.1.1",
5
5
  "main" : "dist/angularjs-rails-resource.min.js",
6
6
  "homepage" : "https://github.com/FineLinePrototyping/angularjs-rails-resource.git",
7
7
  "author" : "",
@@ -359,6 +359,30 @@ describe('railsResourceFactory', function () {
359
359
  expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
360
360
  });
361
361
 
362
+ it('should be able to get an existing resource to retrieve server-side updates', function () {
363
+ var promise, result;
364
+
365
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abcd'}});
366
+
367
+ expect(promise = Test.get(123)).toBeDefined();
368
+
369
+ promise.then(function (response) {
370
+ result = response;
371
+ });
372
+
373
+ $httpBackend.flush();
374
+
375
+ expect(result).toBeInstanceOf(Test);
376
+ expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abcd'});
377
+
378
+ var updatedData = {test: {id: 123, abc: 'zed', xyz: 'wcw', extra: 'test'}};
379
+ $httpBackend.expectGET('/test/123').respond(200, updatedData);
380
+
381
+ result.get();
382
+ $httpBackend.flush();
383
+ expect(result).toEqualData(updatedData.test);
384
+ });
385
+
362
386
  it('update should handle 204 response', function () {
363
387
  var promise, result;
364
388
 
@@ -4,25 +4,47 @@ describe("railsUrlBuilder", function () {
4
4
  beforeEach(module('rails'));
5
5
 
6
6
  it('should return custom function', inject(function (railsUrlBuilder) {
7
- expect(railsUrlBuilder(function () { return 'test' })()).toEqualData('test')
7
+ expect(railsUrlBuilder({
8
+ url: function () { return 'test' }
9
+ })()).toEqualData('test')
8
10
  }));
9
11
 
10
12
  it('should return base url when no context object', inject(function (railsUrlBuilder) {
11
- expect(railsUrlBuilder('/books')()).toEqualData('/books');
13
+ expect(railsUrlBuilder({
14
+ url: '/books'
15
+ })()).toEqualData('/books');
12
16
  }));
13
17
 
14
18
  it('should append id', inject(function (railsUrlBuilder) {
15
- expect(railsUrlBuilder('/books')({id: 1})).toEqualData('/books/1');
19
+ expect(railsUrlBuilder({
20
+ url: '/books',
21
+ idAttribute: 'id'
22
+ })({id: 1})).toEqualData('/books/1');
16
23
  }));
17
24
 
18
25
  it('should use author id for book list', inject(function (railsUrlBuilder) {
19
- expect(railsUrlBuilder('/authors/{{authorId}}/books/{{id}}')({authorId: 1})).toEqualData('/authors/1/books');
26
+ expect(railsUrlBuilder({
27
+ url: '/authors/{{authorId}}/books/{{id}}',
28
+ idAttribute: 'id'
29
+ })({authorId: 1})).toEqualData('/authors/1/books');
20
30
  }));
21
31
 
22
32
  it('should use author id and book id', inject(function (railsUrlBuilder) {
23
- expect(railsUrlBuilder('/authors/{{authorId}}/books/{{id}}')({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
33
+ expect(railsUrlBuilder({
34
+ url: '/authors/{{authorId}}/books/{{id}}',
35
+ idAttribute: 'id'
36
+ })({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
24
37
  }));
25
38
 
39
+ describe('custom idAttribute', function () {
40
+ it('should use different id attribute', inject(function (railsUrlBuilder) {
41
+ expect(railsUrlBuilder({
42
+ url: '/books',
43
+ idAttribute: 'other_id'
44
+ })({id: 1, other_id: 30})).toEqualData('/books/30');
45
+ }));
46
+ });
47
+
26
48
  describe('custom interpolation symbols', function() {
27
49
  beforeEach(module(function($interpolateProvider) {
28
50
  $interpolateProvider.startSymbol('--');
@@ -30,11 +52,17 @@ describe("railsUrlBuilder", function () {
30
52
  }));
31
53
 
32
54
  it('should append id', inject(function (railsUrlBuilder) {
33
- expect(railsUrlBuilder('/books')({id: 1})).toEqualData('/books/1');
55
+ expect(railsUrlBuilder({
56
+ url: '/books',
57
+ idAttribute: 'id'
58
+ })({id: 1})).toEqualData('/books/1');
34
59
  }));
35
60
 
36
61
  it('should use author id and book id', inject(function (railsUrlBuilder) {
37
- expect(railsUrlBuilder('/authors/--authorId--/books/--id--')({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
62
+ expect(railsUrlBuilder({
63
+ url: '/authors/--authorId--/books/--id--',
64
+ idAttribute: 'id'
65
+ })({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
38
66
  }));
39
67
  });
40
68
  });
@@ -209,6 +209,7 @@
209
209
  }
210
210
 
211
211
  this.config = {};
212
+ this.config.idAttribute = cfg.idAttribute || 'id';
212
213
  this.config.url = cfg.url;
213
214
  this.config.rootWrapping = booleanParam(cfg.rootWrapping, defaultOptions.rootWrapping); // using undefined check because config.rootWrapping || true would be true when config.rootWrapping === false
214
215
  this.config.httpConfig = cfg.httpConfig || defaultOptions.httpConfig;
@@ -232,7 +233,7 @@
232
233
  this.config.pluralName = this.config.serializer.underscore(cfg.pluralName || this.config.serializer.pluralize(this.config.name));
233
234
  }
234
235
 
235
- this.config.urlBuilder = railsUrlBuilder(this.config.url);
236
+ this.config.urlBuilder = railsUrlBuilder(this.config);
236
237
  this.config.resourceConstructor = this;
237
238
 
238
239
  this.extend.apply(this, loadExtensions((cfg.extensions || []).concat(defaultOptions.extensions)));
@@ -732,9 +733,14 @@
732
733
  return this['$' + this.constructor.config.updateMethod](this.$url(), this);
733
734
  };
734
735
 
736
+ RailsResource.prototype.get = function () {
737
+ return this.constructor.$http(angular.extend({method: 'GET', url: this.$url()}, this.constructor.getHttpConfig()), this);
738
+ };
739
+
735
740
  RailsResource.prototype.isNew = function () {
736
- return angular.isUndefined(this.id) ||
737
- this.id === null;
741
+ var idAttribute = this.constructor.config.idAttribute;
742
+ return angular.isUndefined(this[idAttribute]) ||
743
+ this[idAttribute] === null;
738
744
  };
739
745
 
740
746
  RailsResource.prototype.save = function () {
@@ -30,15 +30,17 @@
30
30
  */
31
31
  (function (undefined) {
32
32
  angular.module('rails').factory('railsUrlBuilder', ['$interpolate', function($interpolate) {
33
- return function (url) {
34
- var expression;
33
+ return function (config) {
34
+ var url = config.url,
35
+ idAttribute = config.idAttribute,
36
+ expression;
35
37
 
36
38
  if (angular.isFunction(url) || angular.isUndefined(url)) {
37
39
  return url;
38
40
  }
39
41
 
40
42
  if (url.indexOf($interpolate.startSymbol()) === -1) {
41
- url = url + '/' + $interpolate.startSymbol() + 'id' + $interpolate.endSymbol();
43
+ url = url + '/' + $interpolate.startSymbol() + idAttribute + $interpolate.endSymbol();
42
44
  }
43
45
 
44
46
  expression = $interpolate(url);
@@ -54,4 +56,4 @@
54
56
  };
55
57
  };
56
58
  }]);
57
- }());
59
+ }());
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angularjs-rails-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Odom
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-01 00:00:00.000000000 Z
12
+ date: 2014-04-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A small AngularJS add-on for integrating with Rails via JSON more easily.
15
15
  email:
@@ -19,8 +19,8 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - ".gitignore"
23
- - ".travis.yml"
22
+ - .gitignore
23
+ - .travis.yml
24
24
  - CHANGELOG.md
25
25
  - EXAMPLES.md
26
26
  - Gemfile
@@ -71,17 +71,17 @@ require_paths:
71
71
  - lib
72
72
  required_ruby_version: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - ">="
79
+ - - '>='
80
80
  - !ruby/object:Gem::Version
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.2.2
84
+ rubygems_version: 2.1.9
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: AngularJS add-on resource add-on for integrating with Rails