1
0

Completed UnitsController#import and Unit index view

This commit is contained in:
cryptogopher
2019-08-22 01:12:37 +02:00
parent 8e7385cdcb
commit e9fc9c6475
7 changed files with 54 additions and 11 deletions

View File

@@ -1,13 +1,10 @@
class UnitsController < ApplicationController
before_action :find_project, only: [:new, :index, :create, :import]
before_action :find_project, only: [:index, :create, :import]
before_action :authorize
def new
@unit = Unit.new
end
def index
@unit = Unit.new
@units = @project.units
end
def create
@@ -17,6 +14,11 @@ class UnitsController < ApplicationController
end
def import
defaults = Unit.where(project: nil).pluck(:name, :shortname)
missing = defaults - Unit.where(project: @project).pluck(:name, :shortname)
@project.units.create(missing.map { |n, s| {name: n, shortname: s} })
redirect_to project_units_url(@project)
end
private

View File

@@ -3,9 +3,11 @@
<% end %>
<div class="contextual">
<% if @project && User.current.allowed_to?(:manage_units, @project) %>
<% if User.current.allowed_to?(:manage_units, @project) %>
<%= link_to t(".heading_new_unit"), '#', :class => 'icon icon-add',
:onclick => 'showAndScrollTo("add-unit", "unit_name"); return false;' %>
:onclick => 'showAndScrollTo("add-unit", "unit_shortname"); return false;' %>
<%= link_to t(".heading_import"), import_project_units_path(@project), method: :post,
:class => 'icon icon-duplicate' %>
<% end %>
</div>
@@ -23,3 +25,27 @@
</div>
<h2><%= t ".heading" %></h2>
<% if @units.any? %>
<table class="list">
<thead>
<tr>
<th><%= l(:field_shortname) %></th>
<th><%= l(:field_name) %></th>
<th style="width:15%"><%= l(:field_action) %></th>
</tr>
</thead>
<tbody>
<% @units.each do |u| %>
<tr id="unit-<%= u.id %>" class="unit">
<td class="shortname"><%= u.shortname %></td>
<td class="unitname"><%= u.name %></td>
<td>
<%= delete_link unit_path(u), :remote => true %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>