Skip to content

Commit cfa5e88

Browse files
committed
feat: allow formAttributes method to work without accepting a method
1 parent 8d65622 commit cfa5e88

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

providers/edge_provider.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,38 +167,44 @@ export default class EdgeServiceProvider {
167167
*/
168168
edge.global('qs', qs)
169169

170-
edge.global(
171-
'formAttributes',
172-
function (route: string, method: string, params: any, options: URLOptions) {
173-
/**
174-
* Normalize method and keep a reference to the original method
175-
*/
176-
options = options ?? {}
177-
method = method.toUpperCase()
178-
const original = method
179-
180-
/**
181-
* If method if not GET and POST, then use the querystring _method
182-
* to and force update the method to "POST"
183-
*/
184-
if (method !== 'GET' && method !== 'POST') {
185-
method = 'POST'
186-
options = { ...options, qs: { _method: original, ...options.qs } }
187-
}
188-
189-
const { action } = (router.urlBuilder.urlFor.method as any)(
190-
original,
191-
route,
192-
params,
193-
options
194-
).form
195-
196-
return {
197-
action,
198-
method,
199-
}
170+
edge.global('formAttributes', function (route: string, params: any, options: URLOptions) {
171+
const matchingRoute = router.findOrFail(route)
172+
173+
/**
174+
* Normalize method and keep a reference to the original method
175+
*/
176+
options = options ?? {}
177+
let method = matchingRoute.methods[0].toUpperCase()
178+
const original = method
179+
180+
/**
181+
* In case of HEAD, we must use the GET method
182+
*/
183+
if (method === 'HEAD') {
184+
method = 'GET'
200185
}
201-
)
186+
187+
/**
188+
* If method if not GET and POST, then use the querystring _method
189+
* to and force update the method to "POST"
190+
*/
191+
if (method !== 'GET' && method !== 'POST') {
192+
method = 'POST'
193+
options = { ...options, qs: { _method: original, ...options.qs } }
194+
}
195+
196+
const { action } = (router.urlBuilder.urlFor.method as any)(
197+
original,
198+
route,
199+
params,
200+
options
201+
).form
202+
203+
return {
204+
action,
205+
method,
206+
}
207+
})
202208

203209
/**
204210
* Creating a isolated instance of edge renderer

tests/bindings/edge.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test.group('Bindings | Edge', () => {
102102
router.get('/users/:id', () => {}).as('users.show')
103103
router.commit()
104104

105-
assert.deepEqual(edge.globals.formAttributes('users.show', 'get', { id: 1 }), {
105+
assert.deepEqual(edge.globals.formAttributes('users.show', { id: 1 }), {
106106
action: '/users/1',
107107
method: 'GET',
108108
})
@@ -129,7 +129,7 @@ test.group('Bindings | Edge', () => {
129129
router.put('/users/:id', () => {}).as('users.update')
130130
router.commit()
131131

132-
assert.deepEqual(edge.globals.formAttributes('users.update', 'put', { id: 1 }), {
132+
assert.deepEqual(edge.globals.formAttributes('users.update', { id: 1 }), {
133133
action: '/users/1?_method=PUT',
134134
method: 'POST',
135135
})
@@ -163,7 +163,7 @@ test.group('Bindings | Edge', () => {
163163
},
164164
}
165165

166-
assert.deepEqual(edge.globals.formAttributes('users.update', 'put', { id: 1 }, options), {
166+
assert.deepEqual(edge.globals.formAttributes('users.update', { id: 1 }, options), {
167167
action: '/users/1?_method=PUT&view=card',
168168
method: 'POST',
169169
})

0 commit comments

Comments
 (0)