angularjs-rails-resource 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ (function (undefined) {
2
+ angular.module('rails').factory('RailsInflector', function() {
3
+ function camelize(key) {
4
+ if (!angular.isString(key)) {
5
+ return key;
6
+ }
7
+
8
+ // should this match more than word and digit characters?
9
+ return key.replace(/_[\w\d]/g, function (match, index, string) {
10
+ return index === 0 ? match : string.charAt(index + 1).toUpperCase();
11
+ });
12
+ }
13
+
14
+ function underscore(key) {
15
+ if (!angular.isString(key)) {
16
+ return key;
17
+ }
18
+
19
+ // TODO match the latest logic from Active Support
20
+ return key.replace(/[A-Z]/g, function (match, index) {
21
+ return index === 0 ? match : '_' + match.toLowerCase();
22
+ });
23
+ }
24
+
25
+ function pluralize(value) {
26
+ // TODO match Active Support
27
+ return value + 's';
28
+ }
29
+
30
+ return {
31
+ camelize: camelize,
32
+ underscore: underscore,
33
+ pluralize: pluralize
34
+ }
35
+ });
36
+ }());
@@ -0,0 +1,38 @@
1
+ (function (undefined) {
2
+ angular.module('rails').factory('RailsResourceInjector', ['$injector', function($injector) {
3
+ /**
4
+ * Allow dependencies to be referenced by name or instance. If referenced by name AngularJS $injector
5
+ * is used to retrieve the dependency.
6
+ *
7
+ * @param dependency (string | function) The dependency to retrieve
8
+ * @returns {*} The dependency
9
+ */
10
+ function getDependency(dependency) {
11
+ if (dependency) {
12
+ return angular.isString(dependency) ? $injector.get(dependency) : dependency
13
+ }
14
+
15
+ return undefined;
16
+ }
17
+
18
+ /**
19
+ * Looks up and instantiates an instance of the requested service. If the service is not a string then it is
20
+ * assumed to be a constuctor function.
21
+ *
22
+ * @param service (string | function) The service to instantiate
23
+ * @returns {*} A new instance of the requested service
24
+ */
25
+ function createService(service) {
26
+ if (service) {
27
+ return $injector.instantiate(getDependency(service));
28
+ }
29
+
30
+ return undefined;
31
+ }
32
+
33
+ return {
34
+ createService: createService,
35
+ getDependency: getDependency
36
+ }
37
+ }]);
38
+ }());
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @ngdoc function
3
+ * @name rails.railsUrlBuilder
4
+ * @function
5
+ * @requires $interpolate
6
+ *
7
+ * @description
8
+ *
9
+ * Compiles a URL template string into an interpolation function using $interpolate. If no interpolation bindings
10
+ * found then {{id}} is appended to the url string.
11
+ *
12
+ <pre>
13
+ expect(railsUrlBuilder('/books')()).toEqual('/books')
14
+ expect(railsUrlBuilder('/books')({id: 1})).toEqual('/books/1')
15
+ expect(railsUrlBuilder('/authors/{{authorId}}/books/{{id}}')({id: 1, authorId: 2})).toEqual('/authors/2/books/1')
16
+ </pre>
17
+ *
18
+ * If the $interpolate startSymbol and endSymbol have been customized those values should be used instead of {{ and }}
19
+ *
20
+ * @param {string|function} url If the url is a function then that function is returned. Otherwise the url string
21
+ * is passed to $interpolate as an expression.
22
+ *
23
+ * @returns {function(context)} As stated by $interpolate documentation:
24
+ * An interpolation function which is used to compute the interpolated
25
+ * string. The function has these parameters:
26
+ *
27
+ * * `context`: an object against which any expressions embedded in the strings are evaluated
28
+ * against.
29
+ *
30
+ */
31
+ (function (undefined) {
32
+ angular.module('rails').factory('railsUrlBuilder', ['$interpolate', function($interpolate) {
33
+ return function (url) {
34
+ var expression;
35
+
36
+ if (angular.isFunction(url)) {
37
+ return url;
38
+ }
39
+
40
+ if (url.indexOf($interpolate.startSymbol()) === -1) {
41
+ url = url + '/' + $interpolate.startSymbol() + 'id' + $interpolate.endSymbol();
42
+ }
43
+
44
+ expression = $interpolate(url);
45
+
46
+ return function (params) {
47
+ url = expression(params);
48
+
49
+ if (url.charAt(url.length - 1) === '/') {
50
+ url = url.substr(0, url.length - 1);
51
+ }
52
+
53
+ return url;
54
+ };
55
+ };
56
+
57
+ }])
58
+ }());
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: 0.1.7
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -22,6 +22,8 @@ extra_rdoc_files: []
22
22
  files:
23
23
  - .gitignore
24
24
  - .travis.yml
25
+ - CHANGELOG.md
26
+ - EXAMPLES.md
25
27
  - Gemfile
26
28
  - LICENSE
27
29
  - README.md
@@ -42,13 +44,22 @@ files:
42
44
  - test/lib/angular/angular-sanitize.js
43
45
  - test/lib/angular/angular-scenario.js
44
46
  - test/lib/angular/angular.js
45
- - test/unit/angularjs/rails/fieldRenamingSpec.js
46
47
  - test/unit/angularjs/rails/httpSettingsSpec.js
48
+ - test/unit/angularjs/rails/interceptorsSpec.js
47
49
  - test/unit/angularjs/rails/nestedUrlsSpec.js
50
+ - test/unit/angularjs/rails/resourceSerializerSpec.js
48
51
  - test/unit/angularjs/rails/resourceSpec.js
49
52
  - test/unit/angularjs/rails/rootWrappingSpec.js
53
+ - test/unit/angularjs/rails/serializationSpec.js
54
+ - test/unit/angularjs/rails/transformersSpec.js
55
+ - test/unit/angularjs/rails/utils/urlBuilderSpec.js
50
56
  - test/unit/helpers/spec_helper.js
51
- - vendor/assets/javascripts/angularjs/rails/resource.js
57
+ - vendor/assets/javascripts/angularjs/rails/resource/index.js
58
+ - vendor/assets/javascripts/angularjs/rails/resource/resource.js
59
+ - vendor/assets/javascripts/angularjs/rails/resource/serialization.js
60
+ - vendor/assets/javascripts/angularjs/rails/resource/utils/inflector.js
61
+ - vendor/assets/javascripts/angularjs/rails/resource/utils/injector.js
62
+ - vendor/assets/javascripts/angularjs/rails/resource/utils/url_builder.js
52
63
  homepage: https://github.com/finelineprototyping/angularjs-rails-resource
53
64
  licenses: []
54
65
  post_install_message:
@@ -85,9 +96,13 @@ test_files:
85
96
  - test/lib/angular/angular-sanitize.js
86
97
  - test/lib/angular/angular-scenario.js
87
98
  - test/lib/angular/angular.js
88
- - test/unit/angularjs/rails/fieldRenamingSpec.js
89
99
  - test/unit/angularjs/rails/httpSettingsSpec.js
100
+ - test/unit/angularjs/rails/interceptorsSpec.js
90
101
  - test/unit/angularjs/rails/nestedUrlsSpec.js
102
+ - test/unit/angularjs/rails/resourceSerializerSpec.js
91
103
  - test/unit/angularjs/rails/resourceSpec.js
92
104
  - test/unit/angularjs/rails/rootWrappingSpec.js
105
+ - test/unit/angularjs/rails/serializationSpec.js
106
+ - test/unit/angularjs/rails/transformersSpec.js
107
+ - test/unit/angularjs/rails/utils/urlBuilderSpec.js
93
108
  - test/unit/helpers/spec_helper.js
@@ -1,91 +0,0 @@
1
- describe("field renaming", function () {
2
- 'use strict';
3
-
4
- beforeEach(module('rails'));
5
-
6
- var q, rootScope,
7
- transformer, interceptor;
8
-
9
- function testTransform(underscoreData, camelizeData) {
10
- var result, resultPromise,
11
- deferred = q.defer();
12
-
13
- expect(transformer(angular.copy(camelizeData, {}))).toEqualData(underscoreData);
14
- expect(resultPromise = interceptor(deferred.promise)).toBeDefined();
15
-
16
- resultPromise.then(function (response) {
17
- result = response;
18
- });
19
-
20
- deferred.resolve(angular.copy(underscoreData, {}));
21
- rootScope.$digest(); // needed for $q to actually run callbacks
22
- expect(result).toEqualData(camelizeData);
23
- }
24
-
25
- beforeEach(inject(function ($rootScope, $q, railsFieldRenamingTransformer, railsFieldRenamingInterceptor) {
26
- q = $q;
27
- rootScope = $rootScope;
28
- transformer = railsFieldRenamingTransformer;
29
- interceptor = railsFieldRenamingInterceptor;
30
- }));
31
-
32
- it('should ignore empty response', function() {
33
- testTransform({}, {});
34
- });
35
-
36
- it('should ignore null response data', function() {
37
- testTransform({data: null}, {data: null});
38
- });
39
-
40
- it('should leave non-data response fields untouched', function() {
41
- testTransform({data: null, test_value: 'xyz'}, {data: null, test_value: 'xyz'});
42
- });
43
-
44
- it('should transform abc_def <-> abcDef', function() {
45
- testTransform({data: {abc_def: 'xyz'}}, {data: {abcDef: 'xyz'}});
46
- });
47
-
48
- it('should transform abc_def, ghi_jkl <-> abcDef, ghiJkl', function() {
49
- testTransform({data: {abc_def: 'xyz', ghi_jkl: 'abc'}}, {data: {abcDef: 'xyz', ghiJkl: 'abc'}});
50
- });
51
-
52
- it('should transform abc <-> abc', function() {
53
- testTransform({data: {abc: 'xyz'}}, {data: {abc: 'xyz'}});
54
- });
55
-
56
- it('should transform _abc <-> _abc', function() {
57
- testTransform({data: {_abc: 'xyz'}}, {data: {_abc: 'xyz'}});
58
- });
59
-
60
- it('should transform abc_ <-> abc_', function() {
61
- testTransform({data: {abc_: 'xyz'}}, {data: {abc_: 'xyz'}});
62
- });
63
-
64
- it('should transform nested abc_def.abc_def <-> abcDef.abcDef', function() {
65
- testTransform({data: {abc_def: {abc_def: 'xyz'}}}, {data: {abcDef: {abcDef: 'xyz'}}});
66
- });
67
-
68
- it('should transform nested abc_def.abc_def, abc_def.ghi_jkl <-> abcDef.abcDef, abcDef.ghiJkl', function() {
69
- testTransform({data: {abc_def: {abc_def: 'xyz', ghi_jkl: 'abc'}}}, {data: {abcDef: {abcDef: 'xyz', ghiJkl: 'abc'}}});
70
- });
71
-
72
- it('should transform nested abc.abc_def <-> abc.abcDef', function() {
73
- testTransform({data: {abc: {abc_def: 'xyz'}}}, {data: {abc: {abcDef: 'xyz'}}});
74
- });
75
-
76
- it('should handle empty root array', function() {
77
- testTransform({data: []}, {data: []});
78
- });
79
-
80
- it('should camelize array of objects', function() {
81
- testTransform({data: [{abc_def: 'xyz'}, {ghi_jkl: 'abc'}]}, {data: [{abcDef: 'xyz'}, {ghiJkl: 'abc'}]});
82
- });
83
-
84
- it('should handle array of strings', function() {
85
- testTransform({data: ['abc', 'def']}, {data: ['abc', 'def']});
86
- });
87
-
88
- it('should handle array of numbers', function() {
89
- testTransform({data: [1, 2, 3]}, {data: [1, 2, 3]});
90
- });
91
- });