diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index ac78ff7..eab0563 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -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 diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 540c6ad..61f7d3f 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -3,9 +3,11 @@ <% end %>
<%= l(:field_shortname) %> | +<%= l(:field_name) %> | +<%= l(:field_action) %> | +
---|---|---|
<%= u.shortname %> | +<%= u.name %> | ++ <%= delete_link unit_path(u), :remote => true %> + | +
<%= l(:label_no_data) %>
+<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 48bcbe2..1d1926e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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' diff --git a/config/routes.rb b/config/routes.rb index 61e26b2..4b13a3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index ba81eb1..9ddc44b 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -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" diff --git a/init.rb b/init.rb index 31053a1..a628e05 100644 --- a/init.rb +++ b/init.rb @@ -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'}, diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb new file mode 100644 index 0000000..d7bc621 --- /dev/null +++ b/lib/body_tracking/project_patch.rb @@ -0,0 +1,8 @@ +module BodyTracking + module ProjectPatch + Project.class_eval do + has_many :units, dependent: :destroy + end + end +end +