Skip to content

Commit

Permalink
Working to get method and form drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Feb 20, 2024
1 parent 037b7e6 commit 9420541
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/assets/builds/@turbo-boost/commands.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/assets/builds/@turbo-boost/commands.js.map

Large diffs are not rendered by default.

33 changes: 28 additions & 5 deletions app/javascript/drivers/method.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
let activeElement, activePayload

document.addEventListener(
'submit',
event => {
try {
const form = event.target
const method = form.getAttribute('method')
const action = form.getAttribute('action')

if (method !== activeElement?.dataset?.turboMethod) return
if (action !== activeElement?.href) return

const input = document.createElement('input')
input.type = 'hidden'
input.name = 'turbo_boost_command'
input.value = JSON.stringify(activePayload)
form.appendChild(input)
} finally {
activeElement = null
activePayload = null
}
},
true
)

function invokeCommand(element, payload = {}) {
//const src = payload.src
//payload = { ...payload }
//delete payload.src
//delete payload.href
//element.setAttribute('href', urls.build(src, payload))
activeElement = element
activePayload = payload
}

export default { invokeCommand }
6 changes: 3 additions & 3 deletions lib/turbo_boost/commands/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def command_state
end

def command_requested?
controller.request.env.key? "turbo_boost.command"
controller.request.env.key?("turbo_boost.command") || controller.params.key?("turbo_boost_command")
end

def command_valid?
Expand Down Expand Up @@ -171,7 +171,7 @@ def update_response
end

def render_response(html: "", status: nil, status_header: nil)
controller.render html: html, layout: false, status: status || response_status
controller.render html: html, layout: false, status: status || response_status unless controller.performed?
append_to_response_headers status_header
end

Expand Down Expand Up @@ -200,7 +200,7 @@ def handle_command_event(*args)
def parsed_command_params
@parsed_command_params ||= begin
params = controller.request.env["turbo_boost.command"]
params ||= controller.params["turbo_boost_command"]
params ||= JSON.parse(controller.params["turbo_boost_command"])
params || {}
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Drivers
module Method
class AllowControllerActionCommand < ApplicationCommand
after_command -> { transfer_instance_variables controller }

def perform
count = state[self.class.name].to_i + 1
state[self.class.name] = count
@message = "#{self.class.name.demodulize} invoked #{count} times"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Drivers
module Method
class PreventControllerActionCommand < ApplicationCommand
prevent_controller_action

def perform
Current.template = "tests/drivers/method/_turbo_stream.html.erb"
count = state[self.class.name].to_i + 1
state[self.class.name] = count

# TODO: Assign the desired HTML to body and let the runner render
# TODO: Wire up AllowControllerActionCommand
# TODO: Add tests for method driver
# controller.render html: render("/tests/drivers/method", assigns: {message: "#{self.class.name.demodulize} invoked #{count} times"}), status: :multiple_choices
end
end
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def init_current
[], # .............................................. keys (locals that would be passed to the template)
formats: request.accepts.map(&:symbol).compact # ... the template formats accepted by the client
)&.short_identifier
rescue => error
Rails.logger.error "Error in ApplicationController#init_current! #{error.message}"
end

def cleanup
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

class TestsController < ApplicationController
layout "pico"

def destroy
end
end
5 changes: 2 additions & 3 deletions test/dummy/app/views/tests/drivers/_frame.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%# turbo_frame_tag "drivers-frame", data: { src: test_path(:frame) } do %>
<%= turbo_frame_tag "drivers-frame", data: { src: test_path(:frame) } do %>
<%= render "partials/details", id: "drivers-frame-details" do %>
<summary>
Expand All @@ -8,14 +7,14 @@

<article>
<section>
<button type="submit" data-test="append" data-turbo-command="Drivers::Frame::PreventControllerActionCommand">
<button type="submit" data-test="prevent" data-turbo-command="Drivers::Frame::PreventControllerActionCommand">
<article>Click to Invoke → <code>Drivers::Frame::<ins><u>Prevent</u></ins>ControllerActionCommand</code></article>
<small>Uses the <u>Append</u> rendering strategy</small>
</button>
</section>

<section>
<button type="submit" data-test="replace" data-turbo-command="Drivers::Frame::AllowControllerActionCommand">
<button type="submit" data-test="allow" data-turbo-command="Drivers::Frame::AllowControllerActionCommand">
<article>
Click to Invoke → <code>Drivers::Frame::<ins><u>Allow</u></ins>ControllerActionCommand</code>
<br>
Expand Down
27 changes: 27 additions & 0 deletions test/dummy/app/views/tests/drivers/_method.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= turbo_frame_tag "drivers-method", data: { src: test_path(:method) } do %>
<%= render "partials/details", id: "drivers-method" do %>
<summary>
Method
<small><code>→ app/javascript/drivers/method.js</code></small>
</summary>

<article>
<section>
<a role="button" href="<%= test_path :method %>"
data-test="append" data-turbo-method="delete" data-turbo-command="Drivers::Method::PreventControllerActionCommand">
<article>Click to Invoke ➜ <code>Drivers::Method::<ins><u>Prevent</u></ins>ControllerActionCommand</code></article>
<small>Uses the <u>Append</u> rendering strategy</small>
</a>
</section>

<section>
<button type="submit" data-test="replace" data-turbo-command="Drivers::Method::AllowControllerActionCommand">
<article>Click to Invoke ➜ <code>Drivers::Method::<ins><u>Allow</u></ins>ControllerActionCommand</code></article>
<small>Uses the <u>Replace</u> rendering strategy</small>
</button>
</section>

<%= render "/tests/drivers/footer" %>
</article>
<% end %>
<% end %>
3 changes: 3 additions & 0 deletions test/dummy/app/views/tests/drivers/_method.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.replace "drivers-method" do %>
<%= render partial: "/tests/drivers/method", formats: [:html] %>
<% end %>
4 changes: 2 additions & 2 deletions test/dummy/app/views/tests/drivers/_window.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<article>
<section>
<button type="submit" data-test="append" data-turbo-command="Drivers::Window::PreventControllerActionCommand">
<button type="submit" data-test="prevent" data-turbo-command="Drivers::Window::PreventControllerActionCommand">
<article>Click to Invoke ➜ <code>Drivers::Window::<ins><u>Prevent</u></ins>ControllerActionCommand</code></article>
<small>Uses the <u>Append</u> rendering strategy</small>
</button>
</section>

<section>
<button type="submit" data-test="replace" data-turbo-command="Drivers::Window::AllowControllerActionCommand">
<button type="submit" data-test="allow" data-turbo-command="Drivers::Window::AllowControllerActionCommand">
<article>Click to Invoke ➜ <code>Drivers::Window::<ins><u>Allow</u></ins>ControllerActionCommand</code></article>
<small>Uses the <u>Replace</u> rendering strategy</small>
</button>
Expand Down
4 changes: 1 addition & 3 deletions test/dummy/app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

<hr>

<details>
<summary>Method</summary>
</details>
<%= turbo_frame_tag "drivers-method", src: test_path(:method) %>

<hr>

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/routes/tests.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

resources :tests, only: [:index, :show]
resources :tests, only: [:index, :show, :destroy]
16 changes: 8 additions & 8 deletions test/system/tests/drivers/frame_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_prevent_controller_action_command
details_element.click
assert_equal "...", message_element.inner_text
count.times do
append_element.click
wait_for_detach append_element
prevent_element.click
wait_for_detach prevent_element
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end
Expand All @@ -23,8 +23,8 @@ def test_allow_controller_action_command
details_element.click
assert_equal "...", message_element.inner_text
count.times do
replace_element.click
wait_for_detach replace_element
allow_element.click
wait_for_detach allow_element
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end
Expand All @@ -39,11 +39,11 @@ def message_element
details_element.wait_for_selector "[data-test=message]"
end

def append_element
details_element.wait_for_selector "[data-test=append]"
def prevent_element
details_element.wait_for_selector "[data-test=prevent]"
end

def replace_element
details_element.wait_for_selector "[data-test=replace]"
def allow_element
details_element.wait_for_selector "[data-test=allow]"
end
end
16 changes: 8 additions & 8 deletions test/system/tests/drivers/window_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_prevent_controller_action_command
details_element.click
assert_equal "...", message_element.inner_text
count.times do
append_element.click
wait_for_detach append_element
prevent_element.click
wait_for_detach prevent_element
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end
Expand All @@ -23,8 +23,8 @@ def test_allow_controller_action_command
details_element.click
assert_equal "...", message_element.inner_text
count.times do
replace_element.click
wait_for_detach replace_element
allow_element.click
wait_for_detach allow_element
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end
Expand All @@ -39,11 +39,11 @@ def message_element
details_element.wait_for_selector "[data-test=message]"
end

def append_element
details_element.wait_for_selector "[data-test=append]"
def prevent_element
details_element.wait_for_selector "[data-test=prevent]"
end

def replace_element
details_element.wait_for_selector "[data-test=replace]"
def allow_element
details_element.wait_for_selector "[data-test=allow]"
end
end

0 comments on commit 9420541

Please sign in to comment.