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 class UnitsController < ApplicationController
before_action :find_project, only: [:new, :index, :create, :import] before_action :find_project, only: [:index, :create, :import]
before_action :authorize before_action :authorize
def new
@unit = Unit.new
end
def index def index
@unit = Unit.new @unit = Unit.new
@units = @project.units
end end
def create def create
@ -17,6 +14,11 @@ class UnitsController < ApplicationController
end end
def import 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 end
private private

View File

@ -3,9 +3,11 @@
<% end %> <% end %>
<div class="contextual"> <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', <%= 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 %> <% end %>
</div> </div>
@ -23,3 +25,27 @@
</div> </div>
<h2><%= t ".heading" %></h2> <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: en:
body_trackers_menu_caption: 'Body trackers' body_trackers_menu_caption: 'Body trackers'
field_shortname: 'Short name' field_shortname: 'Short name'
field_action: 'Action'
body_trackers: body_trackers:
index: index:
heading: 'Summary' heading: 'Summary'
@ -14,3 +15,4 @@ en:
index: index:
heading: 'Units' heading: 'Units'
heading_new_unit: 'New unit' heading_new_unit: 'New unit'
heading_import: 'Import'

View File

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

View File

@ -8,7 +8,6 @@ class CreateUnits < ActiveRecord::Migration
reversible do |dir| reversible do |dir|
dir.up do dir.up do
Unit.create project: nil, shortname: "", name: "count"
Unit.create project: nil, shortname: "%", name: "percent" Unit.create project: nil, shortname: "%", name: "percent"
Unit.create project: nil, shortname: "g", name: "gram" Unit.create project: nil, shortname: "g", name: "gram"
Unit.create project: nil, shortname: "kg", name: "kilogram" 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 Redmine::Plugin.register :body_tracking do
name 'Body tracking plugin' name 'Body tracking plugin'
author 'cryptogopher' author 'cryptogopher'
@ -7,8 +11,10 @@ Redmine::Plugin.register :body_tracking do
author_url 'https://github.com/cryptogopher' author_url 'https://github.com/cryptogopher'
project_module :body_tracking do project_module :body_tracking do
permission :view_body_trackers, {:body_trackers => [:index], :units => [:index]}, read: true permission :view_body_trackers, {:body_trackers => [:index], :units => [:index]},
permission :manage_units, {:units => [:new, :create, :destroy]}, require: :loggedin read: true
permission :manage_units, {:units => [:create, :destroy, :import]},
require: :loggedin
end end
menu :project_menu, :body_trackers, {:controller => 'body_trackers', :action => 'index'}, 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