angularjs-rails-resource 0.0.2

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.
@@ -0,0 +1,14 @@
1
+ beforeEach(function () {
2
+ this.addMatchers({
3
+ toEqualData: function (expected) {
4
+ return angular.equals(this.actual, expected);
5
+ },
6
+ toBeFunction: function () {
7
+ return angular.isFunction(this.actual);
8
+ },
9
+ toBeInstanceOf: function (expected) {
10
+ return this.actual instanceof expected;
11
+ }
12
+
13
+ });
14
+ });
@@ -0,0 +1,22 @@
1
+ basePath = './';
2
+
3
+ files = [
4
+ JASMINE,
5
+ JASMINE_ADAPTER,
6
+ 'test/lib/angular/angular.js',
7
+ 'test/lib/angular/angular-bootstrap.js',
8
+ 'test/lib/angular/angular-loader.js',
9
+ 'test/lib/angular/angular-sanitize.js',
10
+ 'test/lib/angular/angular-mocks.js',
11
+ 'vendor/assets/javascripts/**/*.js',
12
+ 'test/unit/**/*.js'
13
+ ];
14
+
15
+ autoWatch = true;
16
+
17
+ browsers = ['Chrome'];
18
+
19
+ junitReporter = {
20
+ outputFile: 'test_out/unit.xml',
21
+ suite: 'unit'
22
+ };
@@ -0,0 +1,228 @@
1
+ (function (undefined) {
2
+ angular.module('rails', ['ng']);
3
+
4
+ function transformObject(data, transform) {
5
+ var newKey;
6
+
7
+ if (data && angular.isObject(data)) {
8
+ angular.forEach(data, function (value, key) {
9
+ newKey = transform(key);
10
+
11
+ if (newKey !== key) {
12
+ data[newKey] = value;
13
+ delete data[key];
14
+ }
15
+
16
+ transformObject(value, transform);
17
+ });
18
+ }
19
+ }
20
+
21
+ function camelize(key) {
22
+ if (!angular.isString(key)) {
23
+ return key;
24
+ }
25
+
26
+ // should this match more than word and digit characters?
27
+ return key.replace(/_[\w\d]/g, function (match, index, string) {
28
+ return index === 0 ? match : string.charAt(index + 1).toUpperCase();
29
+ });
30
+ }
31
+
32
+ function underscore(key) {
33
+ if (!angular.isString(key)) {
34
+ return key;
35
+ }
36
+
37
+ return key.replace(/[A-Z]/g, function (match, index) {
38
+ return index === 0 ? match : '_' + match.toLowerCase();
39
+ });
40
+ }
41
+
42
+ angular.module('rails').factory('railsFieldRenamingTransformer', function () {
43
+
44
+ return function (data) {
45
+ transformObject(data, underscore);
46
+ return data;
47
+ };
48
+ });
49
+
50
+ angular.module('rails').factory('railsFieldRenamingInterceptor', function () {
51
+ return function (promise) {
52
+ return promise.then(function (response) {
53
+ transformObject(response.data, camelize);
54
+ return response;
55
+ });
56
+ };
57
+ });
58
+
59
+ angular.module('rails').factory('railsRootWrappingTransformer', function () {
60
+ return function (data, resource) {
61
+ var result = {};
62
+ result[angular.isArray(data) ? resource.rootPluralName : resource.rootName] = data;
63
+ return result;
64
+ };
65
+ });
66
+
67
+ angular.module('rails').factory('railsRootWrappingInterceptor', function () {
68
+ return function (promise) {
69
+ var resource = promise.resource;
70
+
71
+ if (!resource) {
72
+ return promise;
73
+ }
74
+
75
+ return promise.then(function (response) {
76
+ if (response.data && response.data.hasOwnProperty(resource.rootName)) {
77
+ response.data = response.data[resource.rootName];
78
+ } else if (response.data && response.data.hasOwnProperty(resource.rootPluralName)) {
79
+ response.data = response.data[resource.rootPluralName];
80
+ }
81
+
82
+ return response;
83
+ });
84
+ };
85
+ });
86
+
87
+ angular.module('rails').factory('railsResourceFactory', ['$http', '$q', '$injector', function ($http, $q, $injector) {
88
+
89
+ function railsResourceFactory(config) {
90
+ var transformers = config.requestTransformers || ['railsRootWrappingTransformer', 'railsFieldRenamingTransformer'],
91
+ interceptors = config.responseInterceptors || ['railsFieldRenamingInterceptor', 'railsRootWrappingInterceptor'];
92
+
93
+ function RailsResource(value) {
94
+ angular.extend(this, value || {});
95
+ }
96
+
97
+ RailsResource.url = config.url;
98
+ RailsResource.rootName = config.name;
99
+ RailsResource.rootPluralName = config.pluralName || config.name + 's';
100
+ RailsResource.httpConfig = config.httpConfig || {};
101
+ RailsResource.requestTransformers = [];
102
+ RailsResource.responseInterceptors = [];
103
+ RailsResource.defaultParams = config.defaultParams;
104
+
105
+ // copied from $HttpProvider to support interceptors being dependency names or anonymous factory functions
106
+ angular.forEach(interceptors, function (interceptor) {
107
+ RailsResource.responseInterceptors.push(
108
+ angular.isString(interceptor)
109
+ ? $injector.get(interceptor)
110
+ : $injector.invoke(interceptor)
111
+ );
112
+ });
113
+
114
+ angular.forEach(transformers, function (transformer) {
115
+ RailsResource.requestTransformers.push(
116
+ angular.isString(transformer)
117
+ ? $injector.get(transformer)
118
+ : $injector.invoke(transformer)
119
+ );
120
+ });
121
+
122
+ RailsResource.transformData = function (data) {
123
+ angular.forEach(RailsResource.requestTransformers, function (transformer) {
124
+ data = transformer(data, RailsResource);
125
+ });
126
+
127
+ return data;
128
+ };
129
+
130
+ RailsResource.callInterceptors = function (promise) {
131
+
132
+ angular.forEach(RailsResource.responseInterceptors, function (interceptor) {
133
+ promise.resource = RailsResource;
134
+ promise = interceptor(promise);
135
+ });
136
+
137
+ return promise;
138
+ };
139
+
140
+ RailsResource.processResponse = function (promise) {
141
+ promise = RailsResource.callInterceptors(promise);
142
+
143
+ return promise.then(function (response) {
144
+ var result;
145
+
146
+ if (angular.isArray(response.data)) {
147
+ result = [];
148
+
149
+ angular.forEach(response.data, function (value) {
150
+ result.push(new RailsResource(value));
151
+ });
152
+ } else if (angular.isObject(response.data)) {
153
+ result = new RailsResource(response.data);
154
+ } else {
155
+ result = response.data;
156
+ }
157
+
158
+ return result;
159
+ });
160
+ };
161
+
162
+ RailsResource.getHttpConfig = function (queryParams) {
163
+ var config = angular.copy(RailsResource.httpConfig, {});
164
+
165
+ if (RailsResource.defaultParams) {
166
+ config.params = RailsResource.defaultParams;
167
+ }
168
+
169
+ if (queryParams) {
170
+ config.params = angular.extend(config.params || {}, queryParams);
171
+ }
172
+
173
+ return config;
174
+ };
175
+
176
+ RailsResource.resourceUrl = function (id) {
177
+ return RailsResource.url + '/' + id;
178
+ };
179
+
180
+ RailsResource.query = function (queryParams) {
181
+ return RailsResource.processResponse($http.get(RailsResource.url, RailsResource.getHttpConfig(queryParams)));
182
+ };
183
+
184
+ RailsResource.get = function (id) {
185
+ return RailsResource.processResponse($http.get(RailsResource.resourceUrl(id), RailsResource.getHttpConfig()));
186
+ };
187
+
188
+ RailsResource.prototype.processResponse = function (promise) {
189
+ promise = promise.then(function (response) {
190
+ // store off the data in case something (like our root unwrapping) assigns data as a new object
191
+ response.originalData = response.data;
192
+ return response;
193
+ });
194
+
195
+ promise = RailsResource.callInterceptors(promise);
196
+
197
+ return promise.then(angular.bind(this, function (response) {
198
+ // we may not have response data
199
+ if (response.hasOwnProperty('data') && angular.isObject(response.data)) {
200
+ angular.extend(this, response.data);
201
+ }
202
+
203
+ return this;
204
+ }));
205
+ };
206
+
207
+ RailsResource.prototype.create = function () {
208
+ // clone so we can manipulate w/o modifying our instance
209
+ var data = RailsResource.transformData(angular.copy(this, {}));
210
+ return this.processResponse($http.post(RailsResource.url, data, RailsResource.getHttpConfig()));
211
+ };
212
+
213
+ RailsResource.prototype.update = function () {
214
+ // clone so we can manipulate w/o modifying our instance
215
+ var data = RailsResource.transformData(angular.copy(this, {}));
216
+ return this.processResponse($http.put(RailsResource.resourceUrl(this.id), data, RailsResource.getHttpConfig()));
217
+ };
218
+
219
+ RailsResource.prototype.remove = RailsResource.prototype.delete = function (id) {
220
+ return this.processResponse($http.delete(RailsResource.resourceUrl(this.id), RailsResource.getHttpConfig()));
221
+ };
222
+
223
+ return RailsResource;
224
+ }
225
+
226
+ return railsResourceFactory;
227
+ }]);
228
+ }());
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: angularjs-rails-resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tommy Odom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A small AngularJS add-on for integrating with Rails via JSON more easily.
15
+ email:
16
+ - odom@finelineprototyping.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - angularjs-rails-resource.gemspec
27
+ - lib/angularjs-rails-resource.rb
28
+ - lib/angularjs-rails-resource/version.rb
29
+ - test/lib/angular/angular-bootstrap-prettify.js
30
+ - test/lib/angular/angular-bootstrap.js
31
+ - test/lib/angular/angular-cookies.js
32
+ - test/lib/angular/angular-loader.js
33
+ - test/lib/angular/angular-mocks.js
34
+ - test/lib/angular/angular-resource.js
35
+ - test/lib/angular/angular-sanitize.js
36
+ - test/lib/angular/angular.js
37
+ - test/unit/angularjs/rails/resourceSpec.js
38
+ - test/unit/helpers/spec_helper.js
39
+ - testacular.conf.js
40
+ - vendor/assets/javascripts/angularjs/rails/resource.js
41
+ homepage: https://github.com/tpodom/angularjs-rails-resource
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: ''
65
+ test_files:
66
+ - test/lib/angular/angular-bootstrap-prettify.js
67
+ - test/lib/angular/angular-bootstrap.js
68
+ - test/lib/angular/angular-cookies.js
69
+ - test/lib/angular/angular-loader.js
70
+ - test/lib/angular/angular-mocks.js
71
+ - test/lib/angular/angular-resource.js
72
+ - test/lib/angular/angular-sanitize.js
73
+ - test/lib/angular/angular.js
74
+ - test/unit/angularjs/rails/resourceSpec.js
75
+ - test/unit/helpers/spec_helper.js