Skip to content

Commit

Permalink
Convert legacy page urls panel into Turbo Frame
Browse files Browse the repository at this point in the history
Uses turbo frames instead of a remote form to get rid js.erb templates
and use turbo streams instead.
  • Loading branch information
tvdeyen committed May 10, 2024
1 parent e88798d commit 520fd8e
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 71 deletions.
16 changes: 12 additions & 4 deletions app/controllers/alchemy/admin/legacy_page_urls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ def new
end

def create
@legacy_page_url = @page.legacy_urls.build(legacy_page_url_params)
@legacy_page_url.save
@legacy_page_url = @page.legacy_urls.create(legacy_page_url_params)
@message = message_for_resource_action
end

def show
end

def update
@legacy_page_url = LegacyPageUrl.find(params[:id])
if @legacy_page_url.update(legacy_page_url_params)
@message = message_for_resource_action
render :update
else
render :edit
end
end

def destroy
@legacy_page_url = LegacyPageUrl.find(params[:id])
@legacy_page_url.destroy
@page.legacy_urls.destroy(@legacy_page_url)
@message = message_for_resource_action
end

private
Expand All @@ -33,6 +37,10 @@ def load_page
@page = Page.find(params[:page_id])
end

def load_resource
@legacy_page_url = LegacyPageUrl.find(params[:id])
end

def legacy_page_url_params
params.require(:legacy_page_url).permit(:urlname)
end
Expand Down
5 changes: 0 additions & 5 deletions app/views/alchemy/admin/legacy_page_urls/_form.html.erb

This file was deleted.

21 changes: 10 additions & 11 deletions app/views/alchemy/admin/legacy_page_urls/_legacy_page_url.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<tr class="even" id="<%= dom_id(legacy_page_url) %>">
<td class="name"><%= legacy_page_url.urlname %></td>
<td class="tools">
<%= turbo_frame_tag(legacy_page_url, class: ["row", cycle("even", "odd")]) do %>
<div class="col name"><%= legacy_page_url.urlname %></div>
<div class="col tools">
<sl-tooltip content="<%= Alchemy.t(:edit) %>">
<%= link_to_dialog render_icon(:edit),
<%= link_to render_icon(:edit),
edit_admin_legacy_page_url_path(legacy_page_url, page_id: @page.id),
{size: '400x125', title: Alchemy.t('Edit link')},
class: "icon_button" %>
class: "icon_button", data: { turbo_frame: dom_id(legacy_page_url)} %>
</sl-tooltip>
<sl-tooltip content="<%= Alchemy.t(:remove) %>">
<%= link_to_confirm_dialog render_icon(:minus),
Alchemy.t('Are you sure?'),
<%= link_to render_icon(:minus),
admin_legacy_page_url_path(legacy_page_url, page_id: @page.id),
class: "icon_button" %>
class: "icon_button",
data: { turbo_method: :delete, turbo_confirm: Alchemy.t('Are you sure?') } %>
</sl-tooltip>
</td>
</tr>
</div>
<% end %>
32 changes: 15 additions & 17 deletions app/views/alchemy/admin/legacy_page_urls/_new.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<%= alchemy_form_for [:admin, @legacy_page_url ||= @page.legacy_urls.build],
id: 'new_legacy_page_url' do |f| %>
<% if @legacy_page_url.errors.any? %>
<%= render_message :error do %>
<%= @legacy_page_url.errors.full_messages.join %>
<%= turbo_frame_tag "new_legacy_page_url" do %>
<%= alchemy_form_for [:admin, legacy_page_url] do |f| %>
<% if legacy_page_url.errors.any? %>
<%= render_message :error do %>
<%= legacy_page_url.errors.full_messages.join %>
<% end %>
<% end %>
<% end %>
<%= hidden_field_tag :page_id, @page.id %>
<div class="inline-input">
<div class="left-column">
<%= f.text_field :urlname, placeholder: Alchemy::LegacyPageUrl.human_attribute_name(:urlname) %>
</div>
<div class="right-column">
<button class="with_icon">
<%= render_icon(:plus) %>
<%= Alchemy.t(:add) %>
</button>
<%= hidden_field_tag :page_id, @page.id %>
<div class="inline-input">
<div class="left-column">
<%= f.text_field :urlname, placeholder: Alchemy::LegacyPageUrl.human_attribute_name(:urlname) %>
</div>
<div class="right-column">
<button><%= Alchemy.t(:add) %></button>
</div>
</div>
</div>
<% end %>
<% end %>
16 changes: 16 additions & 0 deletions app/views/alchemy/admin/legacy_page_urls/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="table">
<% if legacy_page_urls.any? %>
<header>
<div class="col name">
<%= Alchemy::LegacyPageUrl.human_attribute_name(:urlname) %>
</div>
</header>
<%= render partial: 'alchemy/admin/legacy_page_urls/legacy_page_url',
collection: legacy_page_urls %>
<% else %>
<div class="row even">
<div class="col"><%= Alchemy.t('No page links for this page found') %></div>
<div class="col tools"></div>
</div>
<% end %>
</div>
12 changes: 12 additions & 0 deletions app/views/alchemy/admin/legacy_page_urls/_update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= turbo_stream.update "legacy_urls_label" do %>
<%= render "label", count: @page.legacy_urls.size %>
<% end %>
<%= turbo_stream.update "legacy_page_urls" do %>
<%= render "table", legacy_page_urls: @page.legacy_urls %>
<% end %>
<%= turbo_stream.update "new_legacy_page_url" do %>
<%= render 'new', legacy_page_url: @page.legacy_urls.build %>
<% end %>
<alchemy-growl>
<%= @message %>
</alchemy-growl>
9 changes: 0 additions & 9 deletions app/views/alchemy/admin/legacy_page_urls/create.js.erb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if @legacy_page_url.valid? %>
<%= render "update" %>
<% else %>
<%= turbo_stream.update "new_legacy_page_url" do %>
<%= render 'new',
legacy_page_url: @legacy_page_url %>
<% end %>
<% end %>
6 changes: 0 additions & 6 deletions app/views/alchemy/admin/legacy_page_urls/destroy.js.erb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "update" %>
27 changes: 27 additions & 0 deletions app/views/alchemy/admin/legacy_page_urls/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= turbo_frame_tag(@legacy_page_url, class: "row") do %>
<div class="col p-0">
<%= form_for [:admin, @legacy_page_url] do |f| %>
<%= content_tag :div, class: @legacy_page_url.errors.any? ? "field_with_errors" : nil do %>
<%= f.text_field :urlname, placeholder: Alchemy::LegacyPageUrl.human_attribute_name(:urlname), autofocus: true %>
<% if @legacy_page_url.errors.any? %>
<small class="error">
<%= @legacy_page_url.errors.full_messages_for(:urlname).join %>
</small>
<% end %>
<% end %>
<%= hidden_field_tag :page_id, @legacy_page_url.page_id %>
<% end %>
</div>
<div class="col tools">
<sl-tooltip content="<%= Alchemy.t(:save) %>">
<%= button_tag type: "submit", form: dom_id(@legacy_page_url, :edit), class: "icon_button" do %>
<%= render_icon :check %>
<% end %>
</sl-tooltip>
<sl-tooltip content="<%= Alchemy.t(:cancel) %>">
<%= link_to admin_legacy_page_url_path(@legacy_page_url, page_id: @legacy_page_url.page_id), class: "icon_button" do %>
<%= render_icon :close %>
<% end %>
</sl-tooltip>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/alchemy/admin/legacy_page_urls/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "legacy_page_url", legacy_page_url: @legacy_page_url %>
2 changes: 0 additions & 2 deletions app/views/alchemy/admin/legacy_page_urls/update.js.erb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "update" %>
19 changes: 4 additions & 15 deletions app/views/alchemy/admin/pages/_legacy_urls.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@
<p><%== Alchemy.t(:legacy_url_info_text) %></p>
<% end %>
<table class="list" id="legacy_page_urls">
<tr>
<th class="name">
<%= Alchemy::LegacyPageUrl.human_attribute_name(:urlname) %>
</th>
<th class="tools"></th>
</tr>
<%= render partial: 'alchemy/admin/legacy_page_urls/legacy_page_url',
collection: @page.legacy_urls %>
<tr class="even" id="no_page_links_notice" style="display: <%= @page.legacy_urls.any? ? 'none' : 'table-row' %>">
<td><%= Alchemy.t('No page links for this page found') %></td>
<td class="tools"></td>
</tr>
</table>
<%= turbo_frame_tag("legacy_page_urls") do %>
<%= render "alchemy/admin/legacy_page_urls/table", legacy_page_urls: @page.legacy_urls %>
<% end %>

<fieldset>
<legend><%= Alchemy.t('Add page link') %></legend>
<%= render 'alchemy/admin/legacy_page_urls/new' %>
<%= render 'alchemy/admin/legacy_page_urls/new', legacy_page_url: @page.legacy_urls.build %>
</fieldset>
20 changes: 18 additions & 2 deletions spec/features/admin/legacy_page_url_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,25 @@ def open_page_properties
find("[panel='legacy_urls']").click
end

it "lets a user update a page link" do
within "#legacy_page_urls" do
click_link_with_tooltip("Edit")
page.find("input#legacy_page_url_urlname").set("updated-link")
click_button_with_tooltip "Save"
end
within "sl-tab-panel[name='legacy_urls']" do
expect(page).to have_button("Add")
end
within "#legacy_page_urls" do
expect(page).to_not have_content("a-page-link")
expect(page).to have_content("updated-link")
end
end

it "lets a user remove a page link" do
click_link_with_tooltip("Remove")
click_button "Yes"
page.accept_alert do
click_link_with_tooltip("Remove")
end
within "#legacy_page_urls" do
expect(page).to_not have_content("a-page-link")
expect(page).to have_content(Alchemy.t("No page links for this page found"))
Expand Down

0 comments on commit 520fd8e

Please sign in to comment.