Render units from collection, without redirects

This commit is contained in:
cryptogopher 2024-01-15 18:45:30 +01:00
parent de995aa586
commit 5047d4909f
9 changed files with 37 additions and 22 deletions

View File

@ -361,6 +361,12 @@ table.items td:not(:first-child) {
color: #909090;
fill: #909090;
}
table.items td.hint {
color: #909090;
font-style: italic;
font-size: 0.8rem;
padding: 1em;
}
table.items svg {
height: 1.2rem;
vertical-align: middle;

View File

@ -44,4 +44,9 @@ class ApplicationController < ActionController::Base
flash.alert = t("actioncontroller.exceptions.status.#{message_id}")
redirect_to request.referer
end
def run_and_render(action)
send action
render action
end
end

View File

@ -5,7 +5,7 @@ class UnitsController < ApplicationController
raise AccessForbidden unless current_user.at_least(:active)
end
before_action only: [:edit, :update, :destroy] do
raise ArgumentError unless current_user == @unit.user
raise ParameterInvalid unless current_user == @unit.user
end
def index
@ -20,7 +20,7 @@ class UnitsController < ApplicationController
@unit = current_user.units.new(unit_params)
if @unit.save
flash[:notice] = t(".success")
@units = current_user.units
run_and_render :index
else
render :new
end
@ -32,7 +32,7 @@ class UnitsController < ApplicationController
def update
if @unit.update(unit_params)
flash[:notice] = t(".success")
redirect_to units_url
run_and_render :index
else
render :edit
end
@ -42,7 +42,7 @@ class UnitsController < ApplicationController
if @unit.destroy
flash[:notice] = t(".success")
end
redirect_to units_url
run_and_render :index
end
private

View File

@ -98,6 +98,10 @@ module ApplicationHelper
end.join.html_safe
end
def render_no_items
tag.tr tag.td t('.no_items'), colspan: 10, class: 'hint'
end
private
def image_element_to(type, name, image = nil, options = nil, html_options = {})

View File

@ -1,15 +0,0 @@
<% Unit.each_with_level(@units) do |unit, level| %>
<tr>
<td class="link">
<%= link_to unit.symbol, edit_unit_path(unit), class: level > 0 ? 'subunit' : '' %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
method: :delete %>
</td>
<% end %>
</tr>
<% end %>

View File

@ -0,0 +1,13 @@
<tr>
<td class="link">
<%= link_to unit.symbol, edit_unit_path(unit), class: unit.base.nil? ? '' : 'subunit' %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
method: :delete %>
</td>
<% end %>
</tr>

View File

@ -21,7 +21,7 @@
</tr>
</thead>
<tbody id="units" is="turbo-frame">
<%= render partial: 'index' %>
<%= render(@units) || render_no_items %>
</tbody>
</table>

View File

@ -1,3 +1,3 @@
<%= turbo_stream.update :units do %>
<%= render partial: 'index' %>
<%= render(@units) || render_no_items %>
<% end %>

View File

@ -33,9 +33,11 @@ en:
units: Units
users: Users
units:
unit:
delete_unit: Delete
index:
add_unit: Add unit
delete_unit: Delete
no_items: There are no configured units. You can try to import some defaults.
new:
none: none
create: