forked from fixin.me/fixin.me
Enable creation of subunits
This commit is contained in:
parent
917764fd71
commit
0e8bc18620
@ -1,19 +1,19 @@
|
|||||||
class UnitsController < ApplicationController
|
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 :find_unit, only: [:edit, :update, :destroy]
|
||||||
|
|
||||||
before_action except: :index do
|
before_action except: :index do
|
||||||
raise AccessForbidden unless current_user.at_least(:active)
|
raise AccessForbidden unless current_user.at_least(:active)
|
||||||
end
|
end
|
||||||
before_action only: [:edit, :update, :destroy] do
|
|
||||||
raise ParameterInvalid unless current_user == @unit.user
|
|
||||||
end
|
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@units = current_user.units
|
@units = current_user.units
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@unit = current_user.units.new
|
@unit = current_user.units.new(base: @unit)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ -52,6 +52,6 @@ class UnitsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_unit
|
def find_unit
|
||||||
@unit = Unit.find(params[:id])
|
@unit = Unit.find_by!(id: params[:id], user: current_user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<%= fields_for @unit do |form| %>
|
<%= fields_for @unit do |form| %>
|
||||||
<tr id="<%= dom_id(@unit) %>" onkeydown="processKey(event)">
|
<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,
|
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 12,
|
||||||
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
|
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
|
||||||
</td>
|
</td>
|
||||||
@ -10,8 +10,9 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<% unless @unit.base.nil? %>
|
<% unless @unit.base.nil? %>
|
||||||
<%= form.number_field :multiplier, form: :unit_form, step: "any", size: 10,
|
<%= form.hidden_field :base_id, form: :unit_form %>
|
||||||
autocomplete: "off" %>
|
<%= form.number_field :multiplier, form: :unit_form, required: true, step: "any",
|
||||||
|
size: 10, autocomplete: "off" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
@ -23,4 +24,3 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<!-- TODO: display error_messages_for unit -->
|
<!-- TODO: display error_messages_for unit -->
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
<tr>
|
<tr id="<%= dom_id(unit) %>">
|
||||||
<td class="link">
|
<td class="<%= class_names('link', {subunit: unit.base}) %>">
|
||||||
<%= link_to unit.symbol, edit_unit_path(unit), class: unit.base.nil? ? '' : 'subunit' %>
|
<%= link_to unit.symbol, edit_unit_path(unit) %>
|
||||||
</td>
|
</td>
|
||||||
<td><%= unit.name %></td>
|
<td><%= unit.name %></td>
|
||||||
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
|
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
|
||||||
|
|
||||||
<% if current_user.at_least(:active) %>
|
<% if current_user.at_least(:active) %>
|
||||||
<td class="actions">
|
<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),
|
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
|
||||||
method: :delete %>
|
method: :delete %>
|
||||||
</td>
|
</td>
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
<%= turbo_stream.prepend :units do %>
|
<%= turbo_stream.disable :add_unit %>
|
||||||
<%= render partial: 'form' %>
|
<%= 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 %>
|
<% end %>
|
||||||
|
|
||||||
<%= turbo_stream.update :unit_form_frame do %>
|
<%= turbo_stream.update :unit_form_frame do %>
|
||||||
|
@ -34,6 +34,7 @@ en:
|
|||||||
users: Users
|
users: Users
|
||||||
units:
|
units:
|
||||||
unit:
|
unit:
|
||||||
|
add_subunit: Subunit
|
||||||
delete_unit: Delete
|
delete_unit: Delete
|
||||||
index:
|
index:
|
||||||
add_unit: Add unit
|
add_unit: Add unit
|
||||||
|
@ -2,7 +2,7 @@ Rails.application.routes.draw do
|
|||||||
devise_for :users, path: '', path_names: {registration: 'profile'},
|
devise_for :users, path: '', path_names: {registration: 'profile'},
|
||||||
controllers: {registrations: :registrations}
|
controllers: {registrations: :registrations}
|
||||||
|
|
||||||
resources :units, except: [:show]
|
resources :units, except: [:show], path_names: {new: '(/:id)/new'}
|
||||||
|
|
||||||
resources :users, only: [:index, :show, :update] do
|
resources :users, only: [:index, :show, :update] do
|
||||||
member do
|
member do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user