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 %>

View File

@ -2,6 +2,7 @@
en:
body_trackers_menu_caption: 'Body trackers'
field_shortname: 'Short name'
field_action: 'Action'
body_trackers:
index:
heading: 'Summary'
@ -14,3 +15,4 @@ en:
index:
heading: 'Units'
heading_new_unit: 'New unit'
heading_import: 'Import'

View File

@ -4,7 +4,7 @@
resources :projects do
shallow do
resources :body_trackers, :only => [:index]
resources :units, :only => [:new, :index, :create, :destroy] do
resources :units, :only => [:index, :create, :destroy] do
post 'import', on: :collection
end
end

View File

@ -8,7 +8,6 @@ class CreateUnits < ActiveRecord::Migration
reversible do |dir|
dir.up do
Unit.create project: nil, shortname: "", name: "count"
Unit.create project: nil, shortname: "%", name: "percent"
Unit.create project: nil, shortname: "g", name: "gram"
Unit.create project: nil, shortname: "kg", name: "kilogram"

10
init.rb
View File

@ -1,3 +1,7 @@
(Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do
Project.include BodyTracking::ProjectPatch
end
Redmine::Plugin.register :body_tracking do
name 'Body tracking plugin'
author 'cryptogopher'
@ -7,8 +11,10 @@ Redmine::Plugin.register :body_tracking do
author_url 'https://github.com/cryptogopher'
project_module :body_tracking do
permission :view_body_trackers, {:body_trackers => [:index], :units => [:index]}, read: true
permission :manage_units, {:units => [:new, :create, :destroy]}, require: :loggedin
permission :view_body_trackers, {:body_trackers => [:index], :units => [:index]},
read: true
permission :manage_units, {:units => [:create, :destroy, :import]},
require: :loggedin
end
menu :project_menu, :body_trackers, {:controller => 'body_trackers', :action => 'index'},

View File

@ -0,0 +1,8 @@
module BodyTracking
module ProjectPatch
Project.class_eval do
has_many :units, dependent: :destroy
end
end
end