From 1741a0d2350ab39e20c69b54bccc25262ead6a6a Mon Sep 17 00:00:00 2001 From: Tommy Odom Date: Sun, 20 Mar 2016 08:54:53 -0400 Subject: [PATCH 1/5] Set angular dependency to 1.x --- bower.json | 4 ++-- package.json | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bower.json b/bower.json index 5c29a9d..a42a332 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angularjs-rails-resource", - "version": "2.0.0", + "version": "2.1.0", "main": "angularjs-rails-resource.js", "description": "A resource factory inspired by $resource from AngularJS", "repository": { @@ -8,7 +8,7 @@ "url": "https://github.com/FineLinePrototyping/dist-angularjs-rails-resource.git" }, "dependencies": { - "angular": "*" + "angular": "~1.0" }, "ignore": [ "node_modules", diff --git a/package.json b/package.json index 5ba3ec6..4260710 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "angularjs-rails-resource", "description" : "A resource factory inspired by $resource from AngularJS", - "version": "2.0.0", - "main" : "dist/angularjs-rails-resource.min.js", + "version": "2.1.0", + "main" : "angularjs-rails-resource.min.js", "homepage" : "https://github.com/FineLinePrototyping/angularjs-rails-resource.git", "author" : "", "repository": { @@ -13,7 +13,9 @@ "angular", "resources" ], - "dependencies": {}, + "dependencies": { + "angular": "~1.0" + }, "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-jshint": "~0.6.0", From 6766904ed0bfe0229eb7b7922fb958f8ad993485 Mon Sep 17 00:00:00 2001 From: Tommy Odom Date: Sun, 20 Mar 2016 08:56:11 -0400 Subject: [PATCH 2/5] Bump to 2.1.0 --- CHANGELOG.md | 6 ++++++ lib/angularjs-rails-resource/version.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a23c8a..d9b5335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ + +# 2.1.0 +## Features +- Added option for singular resources - #187 (@jnfeinstein) +- configure now returns the config object. - #176 (@poporul) + # 2.0.0 ## Breaking Changes diff --git a/lib/angularjs-rails-resource/version.rb b/lib/angularjs-rails-resource/version.rb index c801a6e..dcf38a2 100644 --- a/lib/angularjs-rails-resource/version.rb +++ b/lib/angularjs-rails-resource/version.rb @@ -1,7 +1,7 @@ module Angularjs module Rails module Resource - VERSION = '2.0.0' + VERSION = '2.1.0' end end end From 0be47f2d9c68d566d1f318d710f068b7ef22048f Mon Sep 17 00:00:00 2001 From: Sven Hoffman Date: Tue, 12 Apr 2016 14:52:46 +0200 Subject: [PATCH 3/5] #197 * added query params to post put and patch * added tests --- test/unit/angularjs/rails/resourceSpec.js | 20 +++++++++++++++++++ .../angularjs/rails/resource/resource.js | 9 ++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/test/unit/angularjs/rails/resourceSpec.js b/test/unit/angularjs/rails/resourceSpec.js index f5659a5..d119682 100644 --- a/test/unit/angularjs/rails/resourceSpec.js +++ b/test/unit/angularjs/rails/resourceSpec.js @@ -484,6 +484,19 @@ describe('railsResourceFactory', function () { expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}); }); + it('should be able to ' + method + ' to arbitrary url with query params', function () { + var promise, result = {}; + + promise = Test['$' + method]('/xyz', {abc: 'xyz', xyz: 'abc'}, {}, {def: 'ghi'}); + $httpBackend['expect' + angular.uppercase(method)]('/xyz?def=ghi', {test: {abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {abc: 'xyz', xyz: 'abc', extra: 'test'}}); + + promise.then(function (response) { + result = response; + }); + + $httpBackend.flush(); + }); + it('should be able to ' + method + ' instance to arbitrary url', function () { var test = new Test({id: 123, abc: 'xyz', xyz: 'abc'}); $httpBackend['expect' + angular.uppercase(method)]('/xyz', {test: {id: 123, abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}}); @@ -493,6 +506,13 @@ describe('railsResourceFactory', function () { // abc was originally set on the object so it should still be there after the update expect(test).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}); }); + + it('should be able to ' + method + ' instance to arbitrary url with query params', function () { + var test = new Test({abc: 'xyz', xyz: 'abc'}); + $httpBackend['expect' + angular.uppercase(method)]('/xyz?def=ghi', {test: {abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {abc: 'xyz', xyz: 'abc', extra: 'test'}}); + test['$' + method]('/xyz', {}, {def: 'ghi'}); + $httpBackend.flush(); + }); }); it('should be able to $post an array of resources', function () { diff --git a/vendor/assets/javascripts/angularjs/rails/resource/resource.js b/vendor/assets/javascripts/angularjs/rails/resource/resource.js index 5662886..b0c0c2d 100644 --- a/vendor/assets/javascripts/angularjs/rails/resource/resource.js +++ b/vendor/assets/javascripts/angularjs/rails/resource/resource.js @@ -720,17 +720,16 @@ }; angular.forEach(['post', 'put', 'patch'], function (method) { - RailsResource['$' + method] = function (url, data, resourceConfigOverrides) { + RailsResource['$' + method] = function (url, data, resourceConfigOverrides, queryParams) { // clone so we can manipulate w/o modifying the actual instance data = angular.copy(data); - return this.$http(angular.extend({method: method, url: url, data: data}, this.getHttpConfig()), null, resourceConfigOverrides); + return this.$http(angular.extend({method: method, url: url, data: data}, this.getHttpConfig(queryParams)), null, resourceConfigOverrides); }; - RailsResource.prototype['$' + method] = function (url) { + RailsResource.prototype['$' + method] = function (url, context, queryParams) { // clone so we can manipulate w/o modifying the actual instance var data = angular.copy(this, {}); - return this.constructor.$http(angular.extend({method: method, url: url, data: data}, this.constructor.getHttpConfig()), this); - + return this.constructor.$http(angular.extend({method: method, url: url, data: data}, this.constructor.getHttpConfig(queryParams)), this); }; }); From a34dc0381f0e719b362614f5b5aaf6d7700c1117 Mon Sep 17 00:00:00 2001 From: Sven Hoffman Date: Tue, 12 Apr 2016 15:02:05 +0200 Subject: [PATCH 4/5] #197 * updated readme --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7df1dcf..9c49a21 100644 --- a/README.md +++ b/README.md @@ -303,10 +303,11 @@ RailsResources have the following class methods available. * **queryParams** {object} (optional) - The set of query parameters to include in the GET request * **returns** {promise} A promise that will be resolved with a new Resource instance (or instances in the case of an array response). -* $post/$put/$patch(customUrl, data, resourceConfigOverrides) - Serializes the data parameter using the Resource's normal serialization process and submits the result as a POST / PUT / PATCH to the given URL. +* $post/$put/$patch(customUrl, data, resourceConfigOverrides, queryParams) - Serializes the data parameter using the Resource's normal serialization process and submits the result as a POST / PUT / PATCH to the given URL. * **customUrl** {string} - The url to POST / PUT / PATCH to * **data** {object} - The data to serialize and POST / PUT / PATCH * **resourceConfigOverrides** {object} (optional) - An optional set of RailsResource configuration option overrides to use for this request. Root wrapping and serialization for the request data can be bypassed using the `skipRequestProcessing` flag. This also bypasses the entire pre-request [interceptor](#interceptors) chain. + * **queryParams** {object} (optional) - The set of query parameters to include in the request * **returns** {promise} A promise that will be resolved with a new Resource instance (or instances in the case of an array response). * $delete(customUrl, queryParams) - Executes a DELETE to a custom URL. The main difference between this and $http.delete is that a server response that contains a body will be deserialized using the normal Resource deserialization process. @@ -375,8 +376,10 @@ All of the instance methods will update the instance in-place on response and wi * $http(httpConfig, resourceConfigOverrides) - Executes class method $http with the resource instance as the operation context. -* $post(customUrl), $put(customUrl), $patch(customUrl) - Serializes and submits the instance using an HTTP POST/PUT/PATCH to the given URL. +* $post(customUrl, context, queryParams), $put(customUrl, context, queryParams), $patch(customUrl, context, queryParams) - Serializes and submits the instance using an HTTP POST/PUT/PATCH to the given URL. * **customUrl** {string} - The url to POST / PUT / PATCH to + * **context** {object} - The instance that the operation is being run against. + * **queryParams** {object} (optional) - The set of query parameters to include in the POST / PUT / PATCH request * **returns** {promise} - A promise that will be resolved with the instance itself * $delete(customUrl, queryParams) - Executes a DELETE to a custom URL. The main difference between this and $http.delete is that a server response that contains a body will be deserialized using the normal Resource deserialization process. From 3b60d1d1ed884fe82d7dc10152ee034b2993a1bf Mon Sep 17 00:00:00 2001 From: Tommy Odom Date: Tue, 19 Apr 2016 18:08:35 -0400 Subject: [PATCH 5/5] Bump to 2.2.0 --- CHANGELOG.md | 5 ++ bower.json | 2 +- lib/angularjs-rails-resource/version.rb | 2 +- package.json | 2 +- .../unit/angularjs/rails/serializationSpec.js | 50 +++++++++++++++++-- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9b5335..242bca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ + +# 2.2.0 +## Features +- Allow queryParams for $post, $put and $patch - #197 (@herumtreiber ) + # 2.1.0 ## Features diff --git a/bower.json b/bower.json index a42a332..1ce5578 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angularjs-rails-resource", - "version": "2.1.0", + "version": "2.2.0", "main": "angularjs-rails-resource.js", "description": "A resource factory inspired by $resource from AngularJS", "repository": { diff --git a/lib/angularjs-rails-resource/version.rb b/lib/angularjs-rails-resource/version.rb index dcf38a2..95c1d03 100644 --- a/lib/angularjs-rails-resource/version.rb +++ b/lib/angularjs-rails-resource/version.rb @@ -1,7 +1,7 @@ module Angularjs module Rails module Resource - VERSION = '2.1.0' + VERSION = '2.2.0' end end end diff --git a/package.json b/package.json index 4260710..76dda9a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "angularjs-rails-resource", "description" : "A resource factory inspired by $resource from AngularJS", - "version": "2.1.0", + "version": "2.2.0", "main" : "angularjs-rails-resource.min.js", "homepage" : "https://github.com/FineLinePrototyping/angularjs-rails-resource.git", "author" : "", diff --git a/test/unit/angularjs/rails/serializationSpec.js b/test/unit/angularjs/rails/serializationSpec.js index f687f4d..bdf4cf7 100644 --- a/test/unit/angularjs/rails/serializationSpec.js +++ b/test/unit/angularjs/rails/serializationSpec.js @@ -454,6 +454,48 @@ describe('railsSerializer', function () { }); }); + describe('multiple levels of nested attributes', function () { + module('rails'); + + angular.module('rails').factory('Campaign', function (railsResourceFactory, railsSerializer) { + return railsResourceFactory({ + name: 'campaign', + serializer: railsSerializer(function() { + this.resource('emails', 'Email'); + this.nestedAttribute('emails'); + }) + }); + }); + angular.module('rails').factory('Email', function (railsResourceFactory, railsSerializer) { + return railsResourceFactory({ + name: 'email', + serializer: railsSerializer(function() { + this.resource('emailTemplate', 'EmailTemplate'); + this.nestedAttribute('emailTemplate'); + }) + }); + }); + angular.module('rails').factory('EmailTemplate', function (railsResourceFactory) { + return railsResourceFactory({name: 'emailTemplate'}); + }); + + it('should add email template as nested attribute', inject(function(Campaign, Email, EmailTemplate) { + var campaign = new Campaign({ + id: 1, + name: 'Test', + emails: [ + new Email({id: 1, name: '50% off', emailTemplate: new EmailTemplate({id: 1, name: 'Discount'})}) + ] + }); + var serializedCampaign = { + id: 1, name: 'Test', + emails_attributes: [ + {id: 1, name: '50% off', email_template_attributes: { id: 1, name: 'Discount' }}], + }; + + expect(Campaign.config.serializer.serialize(campaign)).toEqual(serializedCampaign); + })); + }); describe('nested resource collection serialization', function () { module('rails'); @@ -476,13 +518,11 @@ describe('railsSerializer', function () { serializer: railsSerializer(function() { this.resource('user', 'User'); this.resource('slot', 'Slot'); + this.nestedAttribute('slot'); this.add('user_id', function(member) { return member.user.id; }); - this.add('slot_id', function(member) { - return member.slot.id; - }); - this.exclude('user', 'slot'); + this.exclude('user'); }) }); }); @@ -509,7 +549,7 @@ describe('railsSerializer', function () { }); var serializedTeam1 = { id: 1, name: 'Team 1', vehicle_id: 123, - members_attributes: [{id: 352435, user_id: 100500, slot_id: 200425}, {id: 235433, user_id: 100501, slot_id: 200426}], + members_attributes: [{id: 352435, user_id: 100500, slot_attributes: { id: 200425, rank_id: 1 }}, {id: 235433, user_id: 100501, slot_attributes: { id: 200426, rank_id: 2 }}], }; expect(Team.config.serializer.serialize(team1)).toEqual(serializedTeam1);