diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb
index 064726c..bae6949 100644
--- a/app/controllers/units_controller.rb
+++ b/app/controllers/units_controller.rb
@@ -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
diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb
index b1c22ce..3f268dd 100644
--- a/app/views/units/_form.html.erb
+++ b/app/views/units/_form.html.erb
@@ -1,6 +1,6 @@
<%= fields_for @unit do |form| %>
-
+ |
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 12,
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
|
@@ -10,8 +10,9 @@
<% 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 %>
|
@@ -23,4 +24,3 @@
<% end %>
-
diff --git a/app/views/units/_unit.html.erb b/app/views/units/_unit.html.erb
index e4e2b77..88fab3e 100644
--- a/app/views/units/_unit.html.erb
+++ b/app/views/units/_unit.html.erb
@@ -1,11 +1,17 @@
-
-
- <%= link_to unit.symbol, edit_unit_path(unit), class: unit.base.nil? ? '' : 'subunit' %>
+ |
+
+ <%= link_to unit.symbol, edit_unit_path(unit) %>
|
<%= unit.name %> |
<%= scientifize(unit.multiplier) if unit.multiplier %> |
+
<% if current_user.at_least(:active) %>
+ <% 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 %>
|
diff --git a/app/views/units/new.turbo_stream.erb b/app/views/units/new.turbo_stream.erb
index 6cbfd9b..6f347f4 100644
--- a/app/views/units/new.turbo_stream.erb
+++ b/app/views/units/new.turbo_stream.erb
@@ -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 %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index f1d264a..c44f31e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -34,6 +34,7 @@ en:
users: Users
units:
unit:
+ add_subunit: Subunit
delete_unit: Delete
index:
add_unit: Add unit
diff --git a/config/routes.rb b/config/routes.rb
index 2535626..0aa8f2d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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