Implemented Quantity
This commit is contained in:
parent
9a34cc0b95
commit
54ab1c60dc
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -5,5 +5,11 @@
|
||||
|
||||
<h3><%= t ".heading_common" %></h3>
|
||||
<ul>
|
||||
<li><%= link_to t(".link_quantities"), project_quantities_path(@project) %></li>
|
||||
<li><%= link_to t(".link_units"), project_units_path(@project) %></li>
|
||||
<% if User.current.allowed_to?(:manage_common, @project) %>
|
||||
<li> </li>
|
||||
<li><p><%= link_to t(".link_defaults"), defaults_project_body_trackers_path(@project),
|
||||
method: :post, data: {confirm: t(".confirm_defaults")} %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
13
app/views/quantities/_form.html.erb
Normal file
13
app/views/quantities/_form.html.erb
Normal file
@ -0,0 +1,13 @@
|
||||
<%= error_messages_for @quantity %>
|
||||
|
||||
<div class="box tabular">
|
||||
<div class="splitcontent">
|
||||
<div class="splitcontentleft">
|
||||
<p><%= f.text_field :name, size: 50, required: true %></p>
|
||||
</div>
|
||||
<div class="splitcontentright">
|
||||
<p><%= f.select :domain, domain_options, required: true %></p>
|
||||
</div>
|
||||
</div>
|
||||
<p><%= f.text_field :description, size: 200 %></p>
|
||||
</div>
|
@ -1 +0,0 @@
|
||||
<h2>QuantitiesController#create</h2>
|
@ -1 +0,0 @@
|
||||
<h2>QuantitiesController#destroy</h2>
|
@ -1 +1,49 @@
|
||||
<h2>QuantitiesController#index</h2>
|
||||
<% content_for :sidebar do %>
|
||||
<%= render :partial => 'body_trackers/sidebar' %>
|
||||
<% end %>
|
||||
|
||||
<div class="contextual">
|
||||
<% 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 %>
|
||||
</div>
|
||||
|
||||
<div id="add-quantity" <%= 'style=display:none;' if @quantity.errors.empty? %>>
|
||||
<h2><%= t ".heading_new_quantity" %></h2>
|
||||
|
||||
<%= 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 %>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<h2><%= t ".heading" %></h2>
|
||||
<% if @quantities.any? %>
|
||||
<table class="list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= l(:field_name) %></th>
|
||||
<th><%= l(:field_domain) %></th>
|
||||
<th><%= l(:field_description) %></th>
|
||||
<th style="width:15%"><%= l(:field_action) %></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @quantities.each do |q| %>
|
||||
<tr id="quantity-<%= q.id %>" class="quantity">
|
||||
<td class="quantityname"><%= q.name %></td>
|
||||
<td class="domain"><%= q.domain %></td>
|
||||
<td class="description"><%= q.description %></td>
|
||||
<td><%= delete_link quantity_path(q), data: {} %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p class="nodata"><%= l(:label_no_data) %></p>
|
||||
<% end %>
|
||||
|
@ -3,10 +3,10 @@
|
||||
<div class="box tabular">
|
||||
<div class="splitcontent">
|
||||
<div class="splitcontentleft">
|
||||
<p><%= f.text_field :shortname, required: true, size: 10 %></p>
|
||||
<p><%= f.text_field :shortname, required: true, size: 20 %></p>
|
||||
</div>
|
||||
<div class="splitcontentright">
|
||||
<p><%= f.text_field :name, size: 40 %></p>
|
||||
<p><%= f.text_field :name, size: 60 %></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1 +0,0 @@
|
||||
<h2>UnitsController#create</h2>
|
@ -1 +0,0 @@
|
||||
<h2>UnitsController#destroy</h2>
|
@ -3,11 +3,9 @@
|
||||
<% end %>
|
||||
|
||||
<div class="contextual">
|
||||
<% 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 %>
|
||||
</div>
|
||||
|
||||
@ -39,9 +37,7 @@
|
||||
<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), data: {} %>
|
||||
</td>
|
||||
<td><%= delete_link unit_path(u), data: {} %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
@ -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'
|
||||
|
@ -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
|
||||
end
|
||||
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
|
||||
|
14
init.rb
14
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'},
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user