Enable creation of subunits

This commit is contained in:
cryptogopher 2024-01-22 02:09:46 +01:00
parent 917764fd71
commit 0e8bc18620
6 changed files with 30 additions and 15 deletions

View File

@ -1,19 +1,19 @@
class UnitsController < ApplicationController
before_action only: [:new] do
find_unit if params[:id].present?
end
before_action :find_unit, only: [:edit, :update, :destroy]
before_action except: :index do
raise AccessForbidden unless current_user.at_least(:active)
end
before_action only: [:edit, :update, :destroy] do
raise ParameterInvalid unless current_user == @unit.user
end
def index
@units = current_user.units
end
def new
@unit = current_user.units.new
@unit = current_user.units.new(base: @unit)
end
def create
@ -52,6 +52,6 @@ class UnitsController < ApplicationController
end
def find_unit
@unit = Unit.find(params[:id])
@unit = Unit.find_by!(id: params[:id], user: current_user)
end
end

View File

@ -1,6 +1,6 @@
<%= fields_for @unit do |form| %>
<tr id="<%= dom_id(@unit) %>" onkeydown="processKey(event)">
<td>
<td class="<%= class_names({subunit: @unit.base}) %>">
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 12,
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
</td>
@ -10,8 +10,9 @@
</td>
<td>
<% unless @unit.base.nil? %>
<%= form.number_field :multiplier, form: :unit_form, step: "any", size: 10,
autocomplete: "off" %>
<%= form.hidden_field :base_id, form: :unit_form %>
<%= form.number_field :multiplier, form: :unit_form, required: true, step: "any",
size: 10, autocomplete: "off" %>
<% end %>
</td>
@ -23,4 +24,3 @@
</tr>
<!-- TODO: display error_messages_for unit -->
<% end %>

View File

@ -1,11 +1,17 @@
<tr>
<td class="link">
<%= link_to unit.symbol, edit_unit_path(unit), class: unit.base.nil? ? '' : 'subunit' %>
<tr id="<%= dom_id(unit) %>">
<td class="<%= class_names('link', {subunit: unit.base}) %>">
<%= link_to unit.symbol, edit_unit_path(unit) %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<% if unit.base.nil? %>
<%= image_link_to t(".add_subunit"), "plus-outline", new_unit_path(unit),
onclick: 'this.blur();', data: {turbo_stream: true} %>
<% end %>
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
method: :delete %>
</td>

View File

@ -1,5 +1,13 @@
<%= turbo_stream.prepend :units do %>
<%= render partial: 'form' %>
<%= turbo_stream.disable :add_unit %>
<%= turbo_stream.disable_all 'td.actions .button' %>
<% case %>
<% when @unit.errors.present? %>
<%= turbo_stream.replace @unit, partial: 'form' %>
<% when @unit.base.nil? %>
<%= turbo_stream.prepend :units, partial: 'form' %>
<% else %>
<%= turbo_stream.after @unit.base, partial: 'form' %>
<% end %>
<%= turbo_stream.update :unit_form_frame do %>

View File

@ -34,6 +34,7 @@ en:
users: Users
units:
unit:
add_subunit: Subunit
delete_unit: Delete
index:
add_unit: Add unit

View File

@ -2,7 +2,7 @@ Rails.application.routes.draw do
devise_for :users, path: '', path_names: {registration: 'profile'},
controllers: {registrations: :registrations}
resources :units, except: [:show]
resources :units, except: [:show], path_names: {new: '(/:id)/new'}
resources :users, only: [:index, :show, :update] do
member do