Skip to content

Commit

Permalink
Merge remote-tracking branch 'FineLinePrototyping/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rothlis committed Sep 7, 2016
2 parents 6931523 + 3b60d1d commit 49bf582
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="2.2.0"></a>
# 2.2.0
## Features
- Allow queryParams for $post, $put and $patch - #197 (@herumtreiber )

<a name="2.1.0"></a>
# 2.1.0
## Features
- Added option for singular resources - #187 (@jnfeinstein)
- <code>configure</code> now returns the config object. - #176 (@poporul)

<a name="2.0.0"></a>
# 2.0.0
## Breaking Changes
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "angularjs-rails-resource",
"version": "2.2.1",
"main": "dist/angularjs-rails-resource.js",
"version": "2.3.0",
"main": "angularjs-rails-resource.js",
"description": "A resource factory inspired by $resource from AngularJS",
"repository": {
"type": "git",
"url": "https://github.com/FineLinePrototyping/dist-angularjs-rails-resource.git"
},
"dependencies": {
"angular": "*"
"angular": "~1.0"
},
"ignore": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion lib/angularjs-rails-resource/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Angularjs
module Rails
module Resource
VERSION = '2.2.1'
VERSION = '2.3.0'
end
end
end
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "angularjs-rails-resource",
"description" : "A resource factory inspired by $resource from AngularJS",
"version": "2.2.1",
"main" : "dist/angularjs-rails-resource.js",
"version": "2.3.0",
"main" : "angularjs-rails-resource.min.js",
"homepage" : "https://github.com/FineLinePrototyping/angularjs-rails-resource.git",
"author" : "",
"repository": {
Expand All @@ -13,7 +13,9 @@
"angular",
"resources"
],
"dependencies": {},
"dependencies": {
"angular": "~1.0"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.0",
Expand Down
20 changes: 20 additions & 0 deletions test/unit/angularjs/rails/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}});
Expand All @@ -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 () {
Expand Down
50 changes: 45 additions & 5 deletions test/unit/angularjs/rails/serializationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
})
});
});
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,13 @@
};

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, queryParams) {
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(queryParams)), this);
Expand Down

0 comments on commit 49bf582

Please sign in to comment.