diff --git a/app/controllers/body_trackers_controller.rb b/app/controllers/body_trackers_controller.rb
index 77f94be..2a14983 100644
--- a/app/controllers/body_trackers_controller.rb
+++ b/app/controllers/body_trackers_controller.rb
@@ -1,17 +1,16 @@
class BodyTrackersController < ApplicationController
- before_action :find_project, only: [:index]
+ before_action :find_project_by_project_id, only: [:index, :defaults]
before_action :authorize
def index
end
- private
+ def defaults
+ available = Unit.where(project: @project).pluck(:shortname)
+ defaults = Unit.where(project: nil).pluck(:name, :shortname)
+ defaults.delete_if { |n, s| available.include?(s) }
+ @project.units.create(defaults.map { |n, s| {name: n, shortname: s} })
- # :find_* methods are called before :authorize,
- # @project is required for :authorize to succeed
- def find_project
- @project = Project.find(params[:project_id])
- rescue ActiveRecord::RecordNotFound
- render_404
+ redirect_to :back
end
end
diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb
index 79d2e55..bd0b097 100644
--- a/app/controllers/quantities_controller.rb
+++ b/app/controllers/quantities_controller.rb
@@ -1,11 +1,47 @@
class QuantitiesController < ApplicationController
+ before_action :find_project_by_project_id, only: [:index, :create]
+ before_action :find_quantity, only: [:destroy]
+ before_action :authorize
def index
+ @quantity = Quantity.new
+ @quantities = @project.quantities
end
def create
+ @quantity = Quantity.new(quantity_params.update(project: @project))
+ if @quantity.save
+ flash[:notice] = 'Created new quantity'
+ redirect_to project_quantities_url(@project)
+ else
+ @quantities = @project.quantities
+ render :index
+ end
end
def destroy
+ if @quantity.destroy
+ flash[:notice] = 'Deleted quantity'
+ end
+ redirect_to project_quantities_url(@project)
+ end
+
+ private
+
+ def quantity_params
+ params.require(:quantity).permit(
+ :name,
+ :description,
+ :domain
+ )
+ end
+
+ # :find_* methods are called before :authorize,
+ # @project is required for :authorize to succeed
+ def find_quantity
+ @quantity = Quantity.find(params[:id])
+ @project = @quantity.project
+ rescue ActiveRecord::RecordNotFound
+ render_404
end
end
diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb
index 3af4472..2fcc0e9 100644
--- a/app/controllers/units_controller.rb
+++ b/app/controllers/units_controller.rb
@@ -1,5 +1,5 @@
class UnitsController < ApplicationController
- before_action :find_project, only: [:index, :create, :import]
+ before_action :find_project_by_project_id, only: [:index, :create]
before_action :find_unit, only: [:destroy]
before_action :authorize
@@ -26,15 +26,6 @@ class UnitsController < ApplicationController
redirect_to project_units_url(@project)
end
- def import
- available = Unit.where(project: @project).pluck(:shortname)
- defaults = Unit.where(project: nil).pluck(:name, :shortname)
- defaults.delete_if { |n, s| available.include?(s) }
- @project.units.create(defaults.map { |n, s| {name: n, shortname: s} })
-
- redirect_to project_units_url(@project)
- end
-
private
def unit_params
@@ -46,12 +37,6 @@ class UnitsController < ApplicationController
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
- def find_project
- @project = Project.find(params[:project_id])
- rescue ActiveRecord::RecordNotFound
- render_404
- end
-
def find_unit
@unit = Unit.find(params[:id])
@project = @unit.project
diff --git a/app/helpers/quantities_helper.rb b/app/helpers/quantities_helper.rb
index 9e6aa8b..853aa12 100644
--- a/app/helpers/quantities_helper.rb
+++ b/app/helpers/quantities_helper.rb
@@ -1,2 +1,8 @@
module QuantitiesHelper
+ def domain_options
+ translations = t('.domains')
+ Quantity.domains.map do |k,v|
+ [translations[k.to_sym], k]
+ end
+ end
end
diff --git a/app/models/quantity.rb b/app/models/quantity.rb
index b135928..7343451 100644
--- a/app/models/quantity.rb
+++ b/app/models/quantity.rb
@@ -1,2 +1,13 @@
class Quantity < ActiveRecord::Base
+ enum domain: {
+ diet: 0,
+ measurement: 1,
+ exercise: 2
+ }
+
+ belongs_to :project
+
+ validates :project, associated: true
+ validates :name, presence: true, uniqueness: {scope: :project_id}
+ validates :domain, inclusion: {in: domains.keys}
end
diff --git a/app/views/body_trackers/_sidebar.html.erb b/app/views/body_trackers/_sidebar.html.erb
index 1a3c95f..d3c965b 100644
--- a/app/views/body_trackers/_sidebar.html.erb
+++ b/app/views/body_trackers/_sidebar.html.erb
@@ -5,5 +5,11 @@
<%= t ".heading_common" %>
+ - <%= link_to t(".link_quantities"), project_quantities_path(@project) %>
- <%= link_to t(".link_units"), project_units_path(@project) %>
+ <% if User.current.allowed_to?(:manage_common, @project) %>
+ -  
+ <%= link_to t(".link_defaults"), defaults_project_body_trackers_path(@project),
+ method: :post, data: {confirm: t(".confirm_defaults")} %>
+ <% end %>
diff --git a/app/views/quantities/_form.html.erb b/app/views/quantities/_form.html.erb
new file mode 100644
index 0000000..8c2b137
--- /dev/null
+++ b/app/views/quantities/_form.html.erb
@@ -0,0 +1,13 @@
+<%= error_messages_for @quantity %>
+
+
+
+
+
<%= f.text_field :name, size: 50, required: true %>
+
+
+
<%= f.select :domain, domain_options, required: true %>
+
+
+
<%= f.text_field :description, size: 200 %>
+
diff --git a/app/views/quantities/create.html.erb b/app/views/quantities/create.html.erb
deleted file mode 100644
index 574941d..0000000
--- a/app/views/quantities/create.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-QuantitiesController#create
diff --git a/app/views/quantities/destroy.html.erb b/app/views/quantities/destroy.html.erb
deleted file mode 100644
index 9682839..0000000
--- a/app/views/quantities/destroy.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-QuantitiesController#destroy
diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb
index 64ddebe..abfb4c9 100644
--- a/app/views/quantities/index.html.erb
+++ b/app/views/quantities/index.html.erb
@@ -1 +1,49 @@
-QuantitiesController#index
+<% content_for :sidebar do %>
+ <%= render :partial => 'body_trackers/sidebar' %>
+<% end %>
+
+
+ <% if User.current.allowed_to?(:manage_common, @project) %>
+ <%= link_to t(".heading_new_quantity"), '#', :class => 'icon icon-add',
+ :onclick => 'showAndScrollTo("add-quantity", "quantity_name"); return false;' %>
+ <% end %>
+
+
+>
+
<%= t ".heading_new_quantity" %>
+
+ <%= labelled_form_for @quantity,
+ :url => project_quantities_path(@project),
+ :html => {:id => 'quantity-form'} do |f| %>
+ <%= render :partial => 'quantities/form', :locals => { :f => f } %>
+ <%= submit_tag l(:button_create) %>
+ <%= link_to l(:button_cancel), "#", :onclick => '$("#add-quantity").hide()' %>
+ <% end %>
+
+
+
+<%= t ".heading" %>
+<% if @quantities.any? %>
+
+
+
+ <%= l(:field_name) %> |
+ <%= l(:field_domain) %> |
+ <%= l(:field_description) %> |
+ <%= l(:field_action) %> |
+
+
+
+ <% @quantities.each do |q| %>
+
+ <%= q.name %> |
+ <%= q.domain %> |
+ <%= q.description %> |
+ <%= delete_link quantity_path(q), data: {} %> |
+
+ <% end %>
+
+
+<% else %>
+ <%= l(:label_no_data) %>
+<% end %>
diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb
index 3479d6f..ec4e99d 100644
--- a/app/views/units/_form.html.erb
+++ b/app/views/units/_form.html.erb
@@ -3,10 +3,10 @@
-
<%= f.text_field :shortname, required: true, size: 10 %>
+
<%= f.text_field :shortname, required: true, size: 20 %>
-
<%= f.text_field :name, size: 40 %>
+
<%= f.text_field :name, size: 60 %>
diff --git a/app/views/units/create.html.erb b/app/views/units/create.html.erb
deleted file mode 100644
index e763536..0000000
--- a/app/views/units/create.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-UnitsController#create
diff --git a/app/views/units/destroy.html.erb b/app/views/units/destroy.html.erb
deleted file mode 100644
index 160ce8e..0000000
--- a/app/views/units/destroy.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-UnitsController#destroy
diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb
index 44d0713..01c25b5 100644
--- a/app/views/units/index.html.erb
+++ b/app/views/units/index.html.erb
@@ -3,11 +3,9 @@
<% end %>
- <% if User.current.allowed_to?(:manage_units, @project) %>
+ <% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".heading_new_unit"), '#', :class => 'icon icon-add',
: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 %>
@@ -39,9 +37,7 @@
<%= u.shortname %> |
<%= u.name %> |
-
- <%= delete_link unit_path(u), data: {} %>
- |
+ <%= delete_link unit_path(u), data: {} %> |
<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 93ada7e..276aad5 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3,6 +3,7 @@ en:
body_trackers_menu_caption: 'Body trackers'
field_shortname: 'Short name'
field_action: 'Action'
+ field_domain: 'Domain'
body_trackers:
index:
heading: 'Summary'
@@ -10,9 +11,20 @@ en:
heading_body_trackers: 'Body trackers'
heading_common: 'Common'
link_summary: 'Summary'
+ link_quantities: 'Quantities'
link_units: 'Units'
+ link_defaults: 'Load defaults'
+ confirm_defaults: 'This will load default quantities and units. Continue?'
+ quantities:
+ index:
+ heading: 'Quantities'
+ heading_new_quantity: 'New quantity'
+ form:
+ domains:
+ diet: 'diet'
+ measurement: 'measurement'
+ exercise: 'exercise'
units:
index:
heading: 'Units'
heading_new_unit: 'New unit'
- heading_import: 'Import defaults'
diff --git a/config/routes.rb b/config/routes.rb
index 4b13a3a..938673d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -3,9 +3,10 @@
resources :projects do
shallow do
- resources :body_trackers, :only => [:index]
- resources :units, :only => [:index, :create, :destroy] do
- post 'import', on: :collection
+ resources :body_trackers, :only => [:index] do
+ post 'defaults', on: :collection
end
+ resources :units, :only => [:index, :create, :destroy]
+ resources :quantities, :only => [:index, :create, :destroy]
end
end
diff --git a/init.rb b/init.rb
index a628e05..a59b41b 100644
--- a/init.rb
+++ b/init.rb
@@ -11,10 +11,16 @@ 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 => [:create, :destroy, :import]},
- require: :loggedin
+ permission :view_body_trackers, {
+ :body_trackers => [:index],
+ :units => [:index],
+ :quantities => [:index]
+ }, read: true
+ permission :manage_common, {
+ :body_trackers => [:defaults],
+ :units => [:create, :destroy],
+ :quantities => [:create, :destroy]
+ }, 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
index d7bc621..28a05b8 100644
--- a/lib/body_tracking/project_patch.rb
+++ b/lib/body_tracking/project_patch.rb
@@ -2,6 +2,7 @@ module BodyTracking
module ProjectPatch
Project.class_eval do
has_many :units, dependent: :destroy
+ has_many :quantities, dependent: :destroy
end
end
end