joosy 1.2.0.alpha.58 → 1.2.0.alpha.59
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bower.json +1 -1
- data/build/joosy/extensions/resources-form.js +3 -0
- data/build/joosy/extensions/resources.js +167 -123
- data/build/joosy.js +4 -3
- data/generators/templates/application/base/application.coffee +0 -1
- data/package.json +4 -7
- data/source/joosy/extensions/resources/base.coffee +25 -15
- data/source/joosy/extensions/resources/rest.coffee +124 -101
- data/source/joosy/extensions/resources/rest_collection.coffee +7 -1
- data/source/joosy/extensions/resources-form/form.coffee +2 -0
- data/source/joosy/helpers/routes.coffee +3 -3
- data/source/joosy/joosy.coffee +1 -1
- data/source/joosy/module.coffee +1 -1
- data/source/joosy/router.coffee +1 -1
- data/spec/joosy/core/helpers/routes_spec.coffee +9 -0
- data/spec/joosy/extensions/form/form_spec.coffee +5 -1
- data/spec/joosy/extensions/resources/rest_spec.coffee +62 -35
- data/tasks/joosy.coffee +7 -265
- metadata +3 -2
@@ -18,31 +18,52 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
18
18
|
@__source = location
|
19
19
|
|
20
20
|
#
|
21
|
-
#
|
21
|
+
# Makes needed changes with clone/wrapper for @at method to extend its' path
|
22
22
|
#
|
23
|
-
@
|
24
|
-
#
|
25
|
-
# Class inheritance used to create proxy
|
26
|
-
#
|
27
|
-
# @private
|
28
|
-
#
|
29
|
-
class Clone extends this
|
30
|
-
|
23
|
+
@__atWrapper: (definer, args...) ->
|
31
24
|
if args.length == 1 && Object.isArray(args[0])
|
32
|
-
@
|
25
|
+
@__atWrapper(definer, args[0]...)
|
33
26
|
else
|
34
|
-
|
35
|
-
|
36
|
-
arg.
|
37
|
-
|
38
|
-
|
27
|
+
definer (clone) =>
|
28
|
+
clone.__source = args.reduce (path, arg) ->
|
29
|
+
path += if arg instanceof Joosy.Resources.REST
|
30
|
+
arg.memberPath()
|
31
|
+
else
|
32
|
+
arg.replace(/^\/?/, '/')
|
33
|
+
, ''
|
34
|
+
clone.__source += '/' + @::__entityName.pluralize()
|
39
35
|
|
40
|
-
|
36
|
+
#
|
37
|
+
# Creates the proxy of current resource binded as a child of given entity
|
38
|
+
#
|
39
|
+
# @param [Array] args Array of parent entities. Can be a string or another REST resource.
|
40
|
+
#
|
41
|
+
# @example Basic usage
|
42
|
+
# Comment.at(['admin', @blog, @post]).collectionPath() # => '/admin/blogs/555/posts/666/comments'
|
43
|
+
#
|
44
|
+
# @note accepts both array notation (Comment.at(['admin', @blog, @post])) and args notation (Comment.at('admin', @blog, @post))
|
45
|
+
#
|
46
|
+
@at: (args...) ->
|
47
|
+
@__atWrapper (callback) =>
|
48
|
+
class Clone extends @
|
49
|
+
callback(@)
|
50
|
+
, args...
|
41
51
|
|
42
|
-
if @::__entityName && args[args.length - 1] instanceof Joosy.Resources.Base
|
43
|
-
Clone.__source += '/' + @::__entityName.pluralize()
|
44
52
|
|
45
|
-
|
53
|
+
#
|
54
|
+
# Creates the proxy of current resource instance binded as a child of given entity
|
55
|
+
#
|
56
|
+
# @param [Array] args Array of parent entities. Can be a string or another REST resource.
|
57
|
+
#
|
58
|
+
# @example Basic usage
|
59
|
+
# Comment.build(1).at(['admin', @blog, @post]).memberPath() # => '/admin/blogs/555/posts/666/comments/1'
|
60
|
+
#
|
61
|
+
# @note accepts both array notation (comment.at(['admin', @blog, @post])) and args notation (comment.at('admin', @blog, @post))
|
62
|
+
#
|
63
|
+
at: (args...) ->
|
64
|
+
@constructor.__atWrapper (callback) =>
|
65
|
+
Object.tap @constructor.__makeShim(@), callback
|
66
|
+
, args...
|
46
67
|
|
47
68
|
#
|
48
69
|
# Implements `@collection` default behavior.
|
@@ -52,54 +73,53 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
52
73
|
named = @__entityName.camelize().pluralize() + 'Collection'
|
53
74
|
if window[named] then window[named] else Joosy.Resources.RESTCollection
|
54
75
|
|
76
|
+
|
55
77
|
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# @param [Array] parents Array of parents
|
59
|
-
#
|
60
|
-
# @example Basic usage
|
61
|
-
# Resource.__parentsPath([otherResource, '/bars/1']) # /other_resources/1/bars/1
|
78
|
+
# Interpolates path with masks by given array of params
|
62
79
|
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
, ''
|
80
|
+
__interpolatePath: (source, ids) ->
|
81
|
+
ids = [ids] unless Object.isArray(ids)
|
82
|
+
ids.reduce (path, id) ->
|
83
|
+
id = id.id() if id instanceof Joosy.Resources.REST
|
84
|
+
path.replace /:[^\/]+/, id
|
85
|
+
, source
|
70
86
|
|
71
87
|
#
|
72
88
|
# Builds collection path
|
73
89
|
#
|
90
|
+
# @param [Array] id IDs for interpolation for masked sources
|
74
91
|
# @param [Hash] options See {Joosy.Resources.REST.find} for possible options
|
75
92
|
#
|
76
93
|
# @example Basic usage
|
77
94
|
# Resource.collectionPath() # /resources/
|
78
95
|
#
|
79
|
-
@collectionPath: (
|
96
|
+
@collectionPath: (args...) ->
|
97
|
+
@::collectionPath args...
|
98
|
+
|
99
|
+
#
|
100
|
+
# Builds collection path
|
101
|
+
#
|
102
|
+
# @see Joosy.Resources.REST.collectionPath
|
103
|
+
#
|
104
|
+
collectionPath: (ids=[], options={}) ->
|
105
|
+
if Object.isObject(ids)
|
106
|
+
options = ids
|
107
|
+
ids = []
|
108
|
+
|
80
109
|
return options.url if options.url
|
81
110
|
|
82
|
-
|
83
|
-
|
111
|
+
source = @__source || @constructor.__source
|
112
|
+
|
113
|
+
if source
|
114
|
+
path = @__interpolatePath source, ids
|
84
115
|
else
|
85
116
|
path = '/'
|
86
|
-
path += @__namespace__.map(
|
87
|
-
path +=
|
88
|
-
|
89
|
-
if options.parent?
|
90
|
-
path = @__parentsPath(if Object.isArray(options.parent) then options.parent else [options.parent]) + path
|
91
|
-
|
92
|
-
path += "/#{options.from}" if options.from?
|
117
|
+
path += @constructor.__namespace__.map(String::underscore).join('/') + '/' if @constructor.__namespace__.length > 0
|
118
|
+
path += @__entityName.pluralize()
|
93
119
|
|
120
|
+
path += "/#{options.from}" if options.from
|
94
121
|
path
|
95
122
|
|
96
|
-
#
|
97
|
-
# Builds collection path
|
98
|
-
#
|
99
|
-
# @see Joosy.Resources.REST.collectionPath
|
100
|
-
#
|
101
|
-
collectionPath: (options={}) ->
|
102
|
-
@constructor.collectionPath options
|
103
123
|
|
104
124
|
#
|
105
125
|
# Builds member path based on the given id.
|
@@ -110,13 +130,8 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
110
130
|
# @example Basic usage
|
111
131
|
# Resource.memberPath(1, from: 'foo') # /resources/1/foo
|
112
132
|
#
|
113
|
-
@memberPath: (
|
114
|
-
|
115
|
-
|
116
|
-
from = options.from
|
117
|
-
path = @collectionPath(Object.merge(options, from: undefined)) + "/#{id}"
|
118
|
-
path += "/#{from}" if from?
|
119
|
-
path
|
133
|
+
@memberPath: (args...) ->
|
134
|
+
@::memberPath args...
|
120
135
|
|
121
136
|
#
|
122
137
|
# Builds member path
|
@@ -126,8 +141,22 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
126
141
|
# @example Basic usage
|
127
142
|
# resource.memberPath(from: 'foo') # /resources/1/foo
|
128
143
|
#
|
129
|
-
memberPath: (options={}) ->
|
130
|
-
|
144
|
+
memberPath: (ids=[], options={}) ->
|
145
|
+
if Object.isObject(ids)
|
146
|
+
options = ids
|
147
|
+
ids = []
|
148
|
+
|
149
|
+
return options.url if options.url
|
150
|
+
|
151
|
+
ids = [ids] unless Object.isArray(ids)
|
152
|
+
id = @id() || ids.pop()
|
153
|
+
|
154
|
+
from = options.from
|
155
|
+
|
156
|
+
ids.push @id()
|
157
|
+
path = @collectionPath(ids, Object.merge(options, from: undefined)) + "/#{id}"
|
158
|
+
path += "/#{from}" if from?
|
159
|
+
path
|
131
160
|
|
132
161
|
#
|
133
162
|
# Sends the GET query using collectionPath.
|
@@ -138,9 +167,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
138
167
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
139
168
|
#
|
140
169
|
@get: (options, callback) ->
|
141
|
-
|
142
|
-
callback = options
|
143
|
-
options = {}
|
170
|
+
[options, callback] = @::__extractOptionsAndCallback(options, callback)
|
144
171
|
@__query @collectionPath(options), 'GET', options.params, callback
|
145
172
|
|
146
173
|
#
|
@@ -152,9 +179,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
152
179
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
153
180
|
#
|
154
181
|
@post: (options, callback) ->
|
155
|
-
|
156
|
-
callback = options
|
157
|
-
options = {}
|
182
|
+
[options, callback] = @::__extractOptionsAndCallback(options, callback)
|
158
183
|
@__query @collectionPath(options), 'POST', options.params, callback
|
159
184
|
|
160
185
|
#
|
@@ -166,9 +191,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
166
191
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
167
192
|
#
|
168
193
|
@put: (options, callback) ->
|
169
|
-
|
170
|
-
callback = options
|
171
|
-
options = {}
|
194
|
+
[options, callback] = @::__extractOptionsAndCallback(options, callback)
|
172
195
|
@__query @collectionPath(options), 'PUT', options.params, callback
|
173
196
|
|
174
197
|
#
|
@@ -180,9 +203,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
180
203
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
181
204
|
#
|
182
205
|
@delete: (options, callback) ->
|
183
|
-
|
184
|
-
callback = options
|
185
|
-
options = {}
|
206
|
+
[options, callback] = @::__extractOptionsAndCallback(options, callback)
|
186
207
|
@__query @collectionPath(options), 'DELETE', options.params, callback
|
187
208
|
|
188
209
|
#
|
@@ -194,9 +215,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
194
215
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
195
216
|
#
|
196
217
|
get: (options, callback) ->
|
197
|
-
|
198
|
-
callback = options
|
199
|
-
options = {}
|
218
|
+
[options, callback] = @__extractOptionsAndCallback(options, callback)
|
200
219
|
@constructor.__query @memberPath(options), 'GET', options.params, callback
|
201
220
|
|
202
221
|
#
|
@@ -208,9 +227,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
208
227
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
209
228
|
#
|
210
229
|
post: (options, callback) ->
|
211
|
-
|
212
|
-
callback = options
|
213
|
-
options = {}
|
230
|
+
[options, callback] = @__extractOptionsAndCallback(options, callback)
|
214
231
|
@constructor.__query @memberPath(options), 'POST', options.params, callback
|
215
232
|
|
216
233
|
#
|
@@ -222,9 +239,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
222
239
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
223
240
|
#
|
224
241
|
put: (options, callback) ->
|
225
|
-
|
226
|
-
callback = options
|
227
|
-
options = {}
|
242
|
+
[options, callback] = @__extractOptionsAndCallback(options, callback)
|
228
243
|
@constructor.__query @memberPath(options), 'PUT', options.params, callback
|
229
244
|
|
230
245
|
#
|
@@ -235,9 +250,7 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
235
250
|
# @param [Function] callback Resulting callback
|
236
251
|
#
|
237
252
|
delete: (options, callback) ->
|
238
|
-
|
239
|
-
callback = options
|
240
|
-
options = {}
|
253
|
+
[options, callback] = @__extractOptionsAndCallback(options, callback)
|
241
254
|
@constructor.__query @memberPath(options), 'DELETE', options.params, callback
|
242
255
|
|
243
256
|
#
|
@@ -251,33 +264,36 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
251
264
|
# @param [Function] callback Resulting callback
|
252
265
|
# (will receive retrieved Collection/Resource)
|
253
266
|
#
|
254
|
-
# @option options [Joosy.Resources.REST] parent Sets the given resource as a base path
|
255
|
-
# i.e. /parents/1/resources
|
256
|
-
# @option options [String] parent Sets the given staring as a base path
|
257
|
-
# i.e. /trololo/resources
|
258
267
|
# @option options [String] from Adds the given string as a last path element
|
259
268
|
# i.e. /resources/trololo
|
269
|
+
# @option options [String] url Sets url for request instead of generated
|
270
|
+
# i.e. /some/custom/url
|
260
271
|
# @option options [Hash] params Passes the given params to the query
|
261
272
|
#
|
262
273
|
@find: (where, options={}, callback=false) ->
|
263
|
-
|
264
|
-
callback = options
|
265
|
-
options = {}
|
274
|
+
[options, callback] = @::__extractOptionsAndCallback(options, callback)
|
266
275
|
|
267
|
-
if where
|
268
|
-
result = new (@::__collection()) this, options
|
276
|
+
id = if Object.isArray(where) then where.last() else where
|
269
277
|
|
270
|
-
|
271
|
-
|
272
|
-
|
278
|
+
if id == 'all'
|
279
|
+
result = new (@::__collection()) this, options
|
280
|
+
path = @collectionPath where, options
|
273
281
|
else
|
274
|
-
result = @build
|
275
|
-
|
276
|
-
|
277
|
-
|
282
|
+
result = @build id
|
283
|
+
path = @memberPath where, options
|
284
|
+
|
285
|
+
if Object.isArray(where) && where.length > 1
|
286
|
+
result.__source = @collectionPath where
|
287
|
+
|
288
|
+
@__query path, 'GET', options.params, (data) =>
|
289
|
+
result.load data
|
290
|
+
callback?(result, data)
|
278
291
|
|
279
292
|
result
|
280
293
|
|
294
|
+
#
|
295
|
+
# Wrapper for AJAX request
|
296
|
+
#
|
281
297
|
@__query: (path, method, params, callback) ->
|
282
298
|
options =
|
283
299
|
data: params
|
@@ -300,10 +316,17 @@ class Joosy.Resources.REST extends Joosy.Resources.Base
|
|
300
316
|
# @param [Object] callback Success and Error callbacks to run `{ success: () ->, error: () -> }`
|
301
317
|
#
|
302
318
|
reload: (options={}, callback=false) ->
|
303
|
-
|
304
|
-
callback = options
|
305
|
-
options = {}
|
319
|
+
[options, callback] = @__extractOptionsAndCallback(options, callback)
|
306
320
|
|
307
321
|
@constructor.__query @memberPath(options), 'GET', options.params, (data) =>
|
308
322
|
@load data
|
309
323
|
callback? this
|
324
|
+
|
325
|
+
#
|
326
|
+
# utility function for better API support for unrequired first options parameter
|
327
|
+
#
|
328
|
+
__extractOptionsAndCallback: (options, callback) ->
|
329
|
+
if Object.isFunction(options)
|
330
|
+
callback = options
|
331
|
+
options = {}
|
332
|
+
[options, callback]
|
@@ -22,6 +22,12 @@ class Joosy.Resources.RESTCollection extends Joosy.Resources.Collection
|
|
22
22
|
callback = options
|
23
23
|
options = {}
|
24
24
|
|
25
|
-
@model.__query @model.collectionPath(options), 'GET', options.params, (data) =>
|
25
|
+
@model.__query @model.collectionPath(options, @__source), 'GET', options.params, (data) =>
|
26
26
|
@load data
|
27
27
|
callback?(data)
|
28
|
+
|
29
|
+
load: (args...) ->
|
30
|
+
res = super(args...)
|
31
|
+
@data.each (x) =>
|
32
|
+
x.__source = @__source
|
33
|
+
res
|
@@ -330,6 +330,8 @@ class Joosy.Form extends Joosy.Module
|
|
330
330
|
__stringifyErrors: (errors) ->
|
331
331
|
result = {}
|
332
332
|
|
333
|
+
errors = errors.errors if Object.isObject(errors?.errors)
|
334
|
+
|
333
335
|
Object.each errors, (field, notifications) =>
|
334
336
|
if @substitutions[field]?
|
335
337
|
field = @substitutions[field]
|
@@ -8,8 +8,8 @@ Joosy.helpers 'Routes', ->
|
|
8
8
|
|
9
9
|
@linkTo = (name='', url='', tagOptions={}) ->
|
10
10
|
if Object.isFunction tagOptions
|
11
|
-
|
12
|
-
tagOptions = url
|
13
|
-
name =
|
11
|
+
block = tagOptions
|
12
|
+
[url, tagOptions] = [name, url]
|
13
|
+
name = block()
|
14
14
|
|
15
15
|
Joosy.Helpers.Application.contentTag 'a', name, Joosy.Module.merge(tagOptions, 'data-joosy': true, href: url)
|
data/source/joosy/joosy.coffee
CHANGED
data/source/joosy/module.coffee
CHANGED
@@ -13,7 +13,7 @@ class Joosy.Module
|
|
13
13
|
# Gets Object/Class class name.
|
14
14
|
#
|
15
15
|
# @note Never use this to make some magical auto-suggestions!!!
|
16
|
-
# Remember:
|
16
|
+
# Remember: minification will rename your classes. Therefore it
|
17
17
|
# is only intended for development debugging purposes.
|
18
18
|
#
|
19
19
|
# @return [String]
|
data/source/joosy/router.coffee
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
# Internal storage rules:
|
10
10
|
#
|
11
11
|
# * HTML5 Prefix option is stored with both leading and trailing slashes
|
12
|
-
# * Helpers
|
12
|
+
# * Helpers' paths are stored without leading trailing slash
|
13
13
|
# * Route matchers declare leading and trailing slashes as optional
|
14
14
|
#
|
15
15
|
# @example
|
@@ -0,0 +1,9 @@
|
|
1
|
+
describe "Joosy.Helpers.Routes", ->
|
2
|
+
|
3
|
+
it "renders data-joosy links", ->
|
4
|
+
link = Joosy.Helpers.Routes.linkTo 'test', '/app/link', nice: true
|
5
|
+
expect(link).toEqual '<a nice="true" data-joosy="true" href="/app/link">test</a>'
|
6
|
+
|
7
|
+
it "renders data-joosy links yielding block", ->
|
8
|
+
link = Joosy.Helpers.Routes.linkTo '/app/link', nice: true, -> 'test'
|
9
|
+
expect(link).toEqual '<a nice="true" data-joosy="true" href="/app/link">test</a>'
|
@@ -241,6 +241,10 @@ describe "Joosy.Form", ->
|
|
241
241
|
@target.respond 422, 'Content-Type': 'application/json', '{"foo": "error!"}'
|
242
242
|
expect($(@nudeForm.$fields()[0]).attr 'class').toEqual 'field_with_errors'
|
243
243
|
|
244
|
+
it "should fill class for invalidated fields by default", ->
|
245
|
+
@target.respond 422, 'Content-Type': 'application/json', '{"errors":{"foo": "error!"}}'
|
246
|
+
expect($(@nudeForm.$fields()[0]).attr 'class').toEqual 'field_with_errors'
|
247
|
+
|
244
248
|
it "should trigger 'error' and complete default action if it returned true", ->
|
245
249
|
@nudeForm.error = sinon.spy ->
|
246
250
|
true
|
@@ -312,7 +316,7 @@ describe "Joosy.Form", ->
|
|
312
316
|
|
313
317
|
expect(result).toEqual { "test[zombie]": ['suck'], "test[puppies]": ['rock'] }
|
314
318
|
|
315
|
-
it "should prepare
|
319
|
+
it "should prepare complex response", ->
|
316
320
|
@nudeForm.fill @resource
|
317
321
|
errors = {fluffies: {zombie: {mumbas: ['ololo']}}}
|
318
322
|
result = @nudeForm.__stringifyErrors(errors)
|
@@ -13,6 +13,10 @@ describe "Joosy.Resources.REST", ->
|
|
13
13
|
@entity 'fluffy'
|
14
14
|
@map 'fluffy_inlines', FluffyInline
|
15
15
|
|
16
|
+
class Interpolated extends Joosy.Resources.REST
|
17
|
+
@entity 'test'
|
18
|
+
@source '/grand_parents/:grand_parent_id/parents/:parent_id/tests'
|
19
|
+
|
16
20
|
Joosy.namespace 'Animal', ->
|
17
21
|
class @Cat extends Joosy.Resources.REST
|
18
22
|
@entity 'cat'
|
@@ -38,54 +42,87 @@ describe "Joosy.Resources.REST", ->
|
|
38
42
|
|
39
43
|
it 'returns base class child', ->
|
40
44
|
clone = @Test.at 'rumbas'
|
41
|
-
expect(Joosy.Module.hasAncestor
|
45
|
+
expect(Joosy.Module.hasAncestor(clone, @Test)).toBeTruthy()
|
42
46
|
|
43
47
|
it 'accepts string', ->
|
44
48
|
clone = @Test.at 'rumbas'
|
45
|
-
expect(clone.
|
49
|
+
expect(clone.collectionPath()).toEqual '/rumbas/tests'
|
50
|
+
|
51
|
+
it 'makes a class whose instances get new source too', ->
|
52
|
+
clone = @Test.at 'rumbas'
|
53
|
+
expect(clone.build(1).memberPath()).toEqual '/rumbas/tests/1'
|
46
54
|
|
47
55
|
it 'accepts another resource instance', ->
|
48
56
|
clone = @Test.at Fluffy.build(1)
|
49
|
-
expect(clone.
|
57
|
+
expect(clone.collectionPath()).toEqual '/fluffies/1/tests'
|
50
58
|
|
51
59
|
it 'accepts array', ->
|
52
60
|
clone = @Test.at ['rumbas', Fluffy.build(1), 'salsas']
|
53
|
-
expect(clone.
|
61
|
+
expect(clone.collectionPath()).toEqual '/rumbas/fluffies/1/salsas/tests'
|
54
62
|
|
55
63
|
it 'accepts sequential attributes', ->
|
56
64
|
clone = @Test.at 'rumbas', 'salsas', Fluffy.build(1)
|
57
|
-
expect(clone.
|
65
|
+
expect(clone.collectionPath()).toEqual '/rumbas/salsas/fluffies/1/tests'
|
58
66
|
|
59
|
-
describe '
|
67
|
+
describe '::at', ->
|
60
68
|
beforeEach ->
|
61
|
-
@
|
62
|
-
|
69
|
+
class @Test extends Joosy.Resources.REST
|
70
|
+
@entity 'test'
|
71
|
+
|
72
|
+
it 'returns base class instance', ->
|
73
|
+
original = @Test.build id: 1, name: 'foobar'
|
74
|
+
clone = original.at 'rumbas'
|
75
|
+
expect(clone instanceof @Test).toBeTruthy()
|
76
|
+
expect(clone.data).toEqual original.data
|
77
|
+
|
78
|
+
it 'accepts string', ->
|
79
|
+
clone = @Test.build(1).at('rumbas')
|
80
|
+
expect(clone.memberPath()).toEqual '/rumbas/tests/1'
|
81
|
+
|
82
|
+
it 'accepts another resource instance', ->
|
83
|
+
clone = @Test.build(1).at(Fluffy.build(1))
|
84
|
+
expect(clone.memberPath()).toEqual '/fluffies/1/tests/1'
|
63
85
|
|
86
|
+
it 'accepts array', ->
|
87
|
+
clone = @Test.build(1).at ['rumbas', Fluffy.build(1), 'salsas']
|
88
|
+
expect(clone.memberPath()).toEqual '/rumbas/fluffies/1/salsas/tests/1'
|
89
|
+
|
90
|
+
it 'accepts sequential attributes', ->
|
91
|
+
clone = @Test.build(1).at 'rumbas', 'salsas', Fluffy.build(1)
|
92
|
+
expect(clone.memberPath()).toEqual '/rumbas/salsas/fluffies/1/tests/1'
|
93
|
+
|
94
|
+
describe '@memberPath', ->
|
64
95
|
it 'builds member path', ->
|
65
96
|
expect(Fluffy.memberPath 1).toEqual '/fluffies/1'
|
66
97
|
|
67
|
-
it 'builds member path with
|
68
|
-
expect(Fluffy.memberPath 1,
|
98
|
+
it 'builds member path with from', ->
|
99
|
+
expect(Fluffy.memberPath 1, from: 'test').toEqual '/fluffies/1/test'
|
69
100
|
|
70
|
-
|
71
|
-
|
101
|
+
describe 'with interpolation', ->
|
102
|
+
it 'builds member path', ->
|
103
|
+
expect(Interpolated.memberPath([1,2,3])).toEqual '/grand_parents/1/parents/2/tests/3'
|
72
104
|
|
73
|
-
|
74
|
-
|
105
|
+
it 'saves member path for single instance', ->
|
106
|
+
item = Interpolated.find [1,2,3]
|
107
|
+
checkAndRespond @server.requests[0], 'GET', /^\/grand_parents\/1\/parents\/2\/tests\/3\?_=\d+/, '{"test": {"id": 3}}'
|
108
|
+
expect(item.memberPath()).toEqual '/grand_parents/1/parents/2/tests/3'
|
75
109
|
|
76
|
-
|
77
|
-
|
110
|
+
it 'saves member path for instances collection', ->
|
111
|
+
items = Interpolated.find [1,2,'all']
|
112
|
+
checkAndRespond @server.requests[0], 'GET', /^\/grand_parents\/1\/parents\/2\/tests\?_=\d+/, '{"tests": [{"test": {"id": 3}}]}'
|
113
|
+
expect(items.at(0).memberPath()).toEqual '/grand_parents/1/parents/2/tests/3'
|
78
114
|
|
115
|
+
describe '@collectionPath', ->
|
116
|
+
it 'builds collection path', ->
|
117
|
+
expect(Fluffy.collectionPath()).toEqual '/fluffies'
|
79
118
|
|
80
|
-
|
81
|
-
|
82
|
-
|
119
|
+
it 'builds collection path with from', ->
|
120
|
+
expect(Fluffy.collectionPath from: 'test').toEqual '/fluffies/test'
|
121
|
+
|
122
|
+
describe 'with interpolation', ->
|
123
|
+
it 'builds collection path', ->
|
124
|
+
expect(Interpolated.collectionPath([1,2])).toEqual '/grand_parents/1/parents/2/tests'
|
83
125
|
|
84
|
-
expect(Fluffy.collectionPath()).toEqual '/fluffies'
|
85
|
-
expect(Fluffy.collectionPath parent: parent).toEqual '/fluffy_parents/1/fluffies'
|
86
|
-
expect(Fluffy.collectionPath parent: [grandParent, parent]).toEqual '/fluffy_parents/666/fluffy_parents/1/fluffies'
|
87
|
-
expect(Fluffy.collectionPath parent: parent, from: 'test').toEqual '/fluffy_parents/1/fluffies/test'
|
88
|
-
expect(Fluffy.collectionPath parent: parent, from: 'test', params: {foo: 'bar'}).toEqual '/fluffy_parents/1/fluffies/test'
|
89
126
|
|
90
127
|
describe '@find(:id)', ->
|
91
128
|
rawData = '{"fluffy": {"id": 1, "name": "test1"}}'
|
@@ -107,11 +144,6 @@ describe "Joosy.Resources.REST", ->
|
|
107
144
|
checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/1\/action\?_=\d+/, rawData
|
108
145
|
expect(@callback.callCount).toEqual 1
|
109
146
|
|
110
|
-
it "gets item with from and parent", ->
|
111
|
-
resource = Fluffy.find 1, {parent: FluffyParent.build(1), from: 'action'}, @callback
|
112
|
-
checkAndRespond @server.requests[0], 'GET', /^\/fluffy_parents\/1\/fluffies\/1\/action\?_=\d+/, rawData
|
113
|
-
expect(@callback.callCount).toEqual 1
|
114
|
-
|
115
147
|
it "gets item with params", ->
|
116
148
|
resource = Fluffy.find 1, params: {foo: 'bar'}, @callback
|
117
149
|
checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/1\?foo=bar&_=\d+/, rawData
|
@@ -127,7 +159,7 @@ describe "Joosy.Resources.REST", ->
|
|
127
159
|
expect(resource).toBe cbResource
|
128
160
|
checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/1\?_=\d+/, rawData
|
129
161
|
|
130
|
-
describe
|
162
|
+
describe "@find('all')", ->
|
131
163
|
rawData = '{"page": 42, "fluffies": [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]}'
|
132
164
|
|
133
165
|
beforeEach ->
|
@@ -147,11 +179,6 @@ describe "Joosy.Resources.REST", ->
|
|
147
179
|
checkAndRespond @server.requests[0], 'GET', /^\/fluffies\/action\?_=\d+/, rawData
|
148
180
|
expect(@callback.callCount).toEqual 1
|
149
181
|
|
150
|
-
it "gets collection with from and parent", ->
|
151
|
-
resource = Fluffy.find 'all', {parent: FluffyParent.build(1), from: 'action'}, @callback
|
152
|
-
checkAndRespond @server.requests[0], 'GET', /^\/fluffy_parents\/1\/fluffies\/action\?_=\d+/, rawData
|
153
|
-
expect(@callback.callCount).toEqual 1
|
154
|
-
|
155
182
|
it "gets collection with params", ->
|
156
183
|
resource = Fluffy.find 'all', params: {foo: 'bar'}, @callback
|
157
184
|
checkAndRespond @server.requests[0], 'GET', /^\/fluffies\?foo=bar&_=\d+/, rawData
|