Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode HTTP method into Request body as _method #370

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,428 changes: 921 additions & 507 deletions app/assets/javascripts/turbo.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions app/assets/javascripts/turbo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions app/javascript/turbo/fetch_requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function encodeMethodIntoRequestBody(event) {
if (event.target instanceof HTMLFormElement) {
const { target: form, detail: { fetchOptions } } = event

form.addEventListener("turbo:submit-start", ({ detail: { formSubmission: { submitter } } }) => {
const method = (submitter && submitter.formMethod) || fetchOptions.body.get("_method") || form.getAttribute("method")

if (!/get/i.test(method)) {
if (/post/i.test(method)) {
fetchOptions.body.delete("_method")
} else {
fetchOptions.body.set("_method", method)
}

fetchOptions.method = "post"
}
}, { once: true })
}
}
5 changes: 0 additions & 5 deletions app/javascript/turbo/form_submissions.js

This file was deleted.

5 changes: 3 additions & 2 deletions app/javascript/turbo/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "./cable_stream_source_element"
import { overrideMethodWithFormmethod } from "./form_submissions"

import * as Turbo from "@hotwired/turbo"
export { Turbo }

import * as cable from "./cable"
export { cable }

addEventListener("turbo:submit-start", overrideMethodWithFormmethod)
import { encodeMethodIntoRequestBody } from "./fetch_requests"

addEventListener("turbo:before-fetch-request", encodeMethodIntoRequestBody)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release": "npm publish && git commit -am \"$npm_package_name v$npm_package_version\" && git push"
},
"dependencies": {
"@hotwired/turbo": "^7.1.0",
"@hotwired/turbo": "hotwired/dev-builds#@hotwired/turbo/28a5dc4",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be ^7.2.0 once a release is cut.

"@rails/actioncable": "^7.0"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions test/dummy/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ArticlesController < ApplicationController
before_action :assert_param_method!, only: [:update, :destroy]

def index
@articles = Article.all
end
Expand All @@ -23,9 +25,21 @@ def create
redirect_to articles_url
end

def destroy
@article = Article.find params[:id]

@article.destroy!

redirect_to articles_url
end

private

def article_params
params.require(:article).permit(:body)
end

def assert_param_method!
raise unless params[:_method].present?
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
<button>Submit as PATCH</button>
<button formmethod="post" formaction="<%= articles_path %>">Submit as POST</button>
<% end %>

<%= link_to "Submit as DELETE", article_path(@article.id), data: { turbo_method: :delete } %>
10 changes: 10 additions & 0 deletions test/system/form_submissions_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
require "application_system_test_case"

class FormSubmissionsTest < ApplicationSystemTestCase
test "form submission method is encoded as _method" do
article = Article.create! body: "My article"

visit edit_article_path(article.id)
click_on "Submit as DELETE"

assert_text "Articles"
assert_no_text "My article"
end

test "form submissions override _method when [formmethod] is present" do
article = Article.create! body: "My article"

Expand Down
7 changes: 3 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@hotwired/turbo@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@hotwired/turbo/-/turbo-7.1.0.tgz#27e44e0e3dc5bd1d4bda0766d579cf5a14091cd7"
integrity sha512-Q8kGjqwPqER+CtpQudbH+3Zgs2X4zb6pBAlr6NsKTXadg45pAOvxI9i4QpuHbwSzR2+x87HUm+rot9F/Pe8rxA==
"@hotwired/turbo@hotwired/dev-builds#@hotwired/turbo/28a5dc4":
version "7.2.0-beta.2"
resolved "https://codeload.github.com/hotwired/dev-builds/tar.gz/932208825cd54cd40d0430e1fa85da2a425aa752"

"@rails/actioncable@^7.0":
version "7.0.1"
Expand Down