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

refactor: improve has_many interactions + add & add another #3269

Merged
merged 36 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d605380
refactor: improve has_many interactions + add & add another
adrianthedev Sep 24, 2024
fe29867
lint
Paul-Bob Sep 27, 2024
838a503
revert destroy turbo stream
Paul-Bob Sep 27, 2024
1c900da
Merge branch 'main' into refactor/improve-has-many-interations
Paul-Bob Sep 27, 2024
057c26e
fix attachments streams
Paul-Bob Sep 27, 2024
b38cd3b
revert html format
Paul-Bob Sep 27, 2024
e227a45
lint
Paul-Bob Sep 27, 2024
cabf65f
rm false turbo_frame from params
Paul-Bob Sep 27, 2024
f4a43af
rm false turbo_frame from params
Paul-Bob Sep 27, 2024
9841ed2
Merge branch 'refactor/improve-has-many-interations' of github.com:av…
Paul-Bob Sep 27, 2024
84e3f38
revert previous commits
Paul-Bob Sep 27, 2024
2e6cdcd
Merge branch 'main' into refactor/improve-has-many-interations
Paul-Bob Sep 28, 2024
8a6870b
fix destroy
Paul-Bob Sep 28, 2024
6d3a98a
extract test that uses turbo to system test
Paul-Bob Sep 28, 2024
2f12b20
lint
Paul-Bob Sep 28, 2024
26ead90
fix attach message on create
Paul-Bob Sep 28, 2024
1bffab5
fix for attribute spec
Paul-Bob Sep 28, 2024
0a4751b
fix test
Paul-Bob Sep 28, 2024
c27af3d
lint
Paul-Bob Sep 28, 2024
cb3d82b
lint
Paul-Bob Sep 28, 2024
797be21
move turbo test to system
Paul-Bob Sep 28, 2024
9050690
improve test
Paul-Bob Sep 28, 2024
80a5663
fix requests without turbo_frame
Paul-Bob Sep 28, 2024
3a79a09
fix tests
Paul-Bob Sep 28, 2024
d73e1b8
fix attach on linkable: true scenarios
Paul-Bob Sep 28, 2024
08db5a5
refactor complexity
Paul-Bob Sep 29, 2024
3779038
fix associations responses
Paul-Bob Sep 29, 2024
de3fb3a
test
Paul-Bob Sep 29, 2024
8786eb7
refactor reload_frame_turbo_streams
Paul-Bob Sep 29, 2024
d25fdee
Merge branch 'main' into refactor/improve-has-many-interations
Paul-Bob Sep 29, 2024
1b09ce2
Merge branch 'main' into refactor/improve-has-many-interations
Paul-Bob Sep 29, 2024
43916c4
fix create_success_action
Paul-Bob Sep 29, 2024
bd88de3
reduce complexity
Paul-Bob Sep 29, 2024
5945604
Merge branch 'main' into refactor/improve-has-many-interations
Paul-Bob Sep 29, 2024
cd6b4fe
Merge branch 'refactor/improve-has-many-interations' of github.com:av…
Paul-Bob Sep 29, 2024
98c6097
reduce complexity
Paul-Bob Sep 29, 2024
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
2 changes: 1 addition & 1 deletion app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def render_action_link(action)
def action_data_attributes(action)
{
action_name: action.action_name,
"turbo-frame": Avo::ACTIONS_TURBO_FRAME_ID,
"turbo-frame": Avo::MODAL_FRAME_ID,
action: "click->actions-picker#visitAction",
"actions-picker-target": action.standalone ? "standaloneAction" : "resourceAction",
disabled: is_disabled?(action),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
icon: 'heroicons/outline/link',
color: :primary,
style: :text,
'data-turbo-frame': 'attach_modal' do %>
data_turbo_frame: Avo::MODAL_FRAME_ID do %>
<%= t('avo.attach_item', item: @field.name.humanize(capitalize: false)) %>
<% end %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def render_attach_button(control)
color: :primary,
style: :text,
data: {
turbo_frame: :attach_modal,
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do
control.label
Expand Down Expand Up @@ -282,7 +282,7 @@ def render_action(action)
title: action.title,
size: action.size,
data: {
turbo_frame: Avo::ACTIONS_TURBO_FRAME_ID,
turbo_frame: Avo::MODAL_FRAME_ID,
action_name: action.action.action_name,
tippy: action.title ? :tooltip : nil,
action: "click->actions-picker#visitAction",
Expand Down
1 change: 1 addition & 0 deletions app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def attach_path
paths: [*current_path, "new"],
query: {
view: @parent_resource&.view&.to_s,
for_turbo_frame: field&.try(:turbo_frame),
for_attribute: field&.try(:for_attribute)
}.compact
)
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ def respond
# Trigger download, removes modal and flash the messages
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
when :navigate_to_action
frame_id = Avo::ACTIONS_TURBO_FRAME_ID
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])

turbo_stream.turbo_frame_set_src(frame_id, src)
turbo_stream.turbo_frame_set_src(Avo::MODAL_FRAME_ID, src)
when :redirect
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
Expand All @@ -118,7 +117,7 @@ def respond
when :close_modal
# Close the modal and flash the messages
[
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
else
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ def create
respond_to do |format|
if create_association
format.html {
redirect_back fallback_location: resource_view_response_path,
notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name)
redirect_back fallback_location: resource_view_response_path, notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name)
}
format.turbo_stream {
actions = [turbo_stream.turbo_frame_reload(params[:turbo_frame])]

# We want to close the modal if the user wants to add just one record
actions << turbo_stream.close_modal if params[:button] != "attach_another"

render turbo_stream: actions
}
else
flash[:error] = t("avo.attachment_failed", attachment_class: @related_resource.name)
Expand Down Expand Up @@ -104,6 +111,7 @@ def destroy

respond_to do |format|
format.html { redirect_to params[:referrer] || resource_view_response_path, notice: t("avo.attachment_class_detached", attachment_class: @attachment_class) }
format.turbo_stream { render turbo_stream: turbo_stream.turbo_frame_reload(params[:turbo_frame]) }
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def new

# Handle special cases when creating a new record via a belongs_to relationship
if params[:via_belongs_to_resource_class].present?
return render turbo_stream: turbo_stream.append("attach_modal", partial: "avo/base/new_via_belongs_to")
return render turbo_stream: turbo_stream.append(Avo::MODAL_FRAME_ID, partial: "avo/base/new_via_belongs_to")
end

set_actions
Expand Down Expand Up @@ -532,6 +532,7 @@ def resource_view_response_path
def destroy_success_action
respond_to do |format|
format.html { redirect_to after_destroy_path, notice: destroy_success_message }
format.turbo_stream { render turbo_stream: turbo_stream.turbo_frame_reload(params[:turbo_frame]) }
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def flash_alerts
template: @view_context.render(Avo::FlashAlertsComponent.new(flashes: @view_context.flash.discard))
end

def close_action_modal
def close_modal
turbo_stream_action_tag :replace,
target: Avo::ACTIONS_TURBO_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1})
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/js/controllers/actions_picker_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class extends Controller {
}

get actionsShowTurboFrame() {
return document.querySelector('turbo-frame#actions_show')
return document.querySelector(`turbo-frame#${window.Avo.configuration.modal_frame_id}`)
}

enableTarget() {
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= render Avo::LoadingComponent.new(title: "...") %>
<% end %>

<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID do %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<div
data-controller="<%= ["action", @action.get_stimulus_controllers].join(" ") %>"
data-action-no-confirmation-value="<%= @action.no_confirmation %>"
Expand Down
6 changes: 5 additions & 1 deletion app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag 'attach_modal' do %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<%
url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths('resources', params[:resource_name], params[:id], params[:related_name])
Expand Down Expand Up @@ -53,6 +53,7 @@
stacked: true,
classes: 'w-full'
%>
<%= hidden_field_tag :turbo_frame, params[:for_turbo_frame] %>
<% end %>
<% @attach_fields&.each_with_index do |field, index| %>
<%= render(Avo::Items::SwitcherComponent.new(
Expand All @@ -74,6 +75,9 @@
<%= a_button 'data-action': 'click->modal#close', type: :button, size: :sm, style: :outline, color: :gray do %>
<%= t('avo.cancel') %>
<% end %>
<%= a_button type: :submit, value: :attach_another, style: :outline, color: :green, size: :sm do %>
<%= t('avo.attach_and_attach_another') %>
<% end %>
<%= a_button type: :submit, style: :primary, color: :green, size: :sm do %>
<%= t('avo.attach') %>
<% end %>
Expand Down
1 change: 0 additions & 1 deletion app/views/avo/partials/_attach_modal.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/avo/partials/_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
param_key: '<%= Avo::DynamicFilters.configuration.param_key %>'
}
<% end %>
Avo.configuration.modal_frame_id = '<%= ::Avo::MODAL_FRAME_ID %>'
<% end %>
3 changes: 1 addition & 2 deletions app/views/layouts/avo/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
</div>
</div>
</div>
<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/attach_modal" %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/alerts" %>
<%= render partial: "avo/partials/scripts" %>
<%= render partial: "avo/partials/confirm_dialog" %>
Expand Down
2 changes: 1 addition & 1 deletion lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Avo
IN_DEVELOPMENT = ENV["AVO_IN_DEVELOPMENT"] == "1"
PACKED = !IN_DEVELOPMENT
COOKIES_KEY = "avo"
ACTIONS_TURBO_FRAME_ID = :actions_show
MODAL_FRAME_ID = :modal_frame
ACTIONS_BACKGROUND_FRAME = :actions_background

class LicenseVerificationTemperedError < StandardError; end
Expand Down
6 changes: 1 addition & 5 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ def link_arguments(resource:, arguments: {}, **args)
)
.to_s

data = {
turbo_frame: Avo::ACTIONS_TURBO_FRAME_ID,
}

[path, data]
[path, {turbo_frame: Avo::MODAL_FRAME_ID}]
end

# Encrypt the arguments so we can pass sensible data as a query param.
Expand Down
4 changes: 2 additions & 2 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@

click_on "Actions"
click_on "Do Nothing"
expect(page).to have_css('turbo-frame#actions_show')
expect(page).to have_css('turbo-frame#modal_frame')
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
Expand All @@ -214,7 +214,7 @@

click_on "Actions"
click_on "Close modal"
expect(page).to have_css('turbo-frame#actions_show')
expect(page).to have_css('turbo-frame#modal_frame')
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
Expand Down
Loading