Added ingredient index and form
This commit is contained in:
parent
c916e7fd3c
commit
c82f903360
@ -1,6 +1,10 @@
|
||||
class IngredientsController < ApplicationController
|
||||
before_action :find_project_by_project_id, only: [:index, :create]
|
||||
before_action :authorize
|
||||
|
||||
def index
|
||||
@ingredient = Ingredient.new
|
||||
@ingredients = @project.ingredients
|
||||
end
|
||||
|
||||
def create
|
||||
|
@ -1,2 +1,14 @@
|
||||
module IngredientsHelper
|
||||
def unit_options
|
||||
@project.units.map do |u|
|
||||
[u.shortname, u.id]
|
||||
end
|
||||
end
|
||||
|
||||
def group_options
|
||||
translations = t('.groups')
|
||||
Ingredient.groups.map do |k,v|
|
||||
[translations[k.to_sym], k]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,2 +1,13 @@
|
||||
class Ingredient < ActiveRecord::Base
|
||||
enum group: {
|
||||
meat: 0
|
||||
}
|
||||
|
||||
belongs_to :project
|
||||
|
||||
validates :project, associated: true
|
||||
validates :name, presence: true, uniqueness: {scope: :project_id}
|
||||
validates :ref_amount, numericality: {greater_than: 0}
|
||||
validates :ref_unit, presence: true, associated: true
|
||||
validates :group, inclusion: {in: groups.keys}
|
||||
end
|
||||
|
10
app/views/ingredients/_form.html.erb
Normal file
10
app/views/ingredients/_form.html.erb
Normal file
@ -0,0 +1,10 @@
|
||||
<%= error_messages_for @ingredient %>
|
||||
|
||||
<div class="box tabular">
|
||||
<p><%= f.text_field :name, size: 40, required: true %></p>
|
||||
<p>
|
||||
<%= f.number_field :ref_amount, size: 8, required: true, min: 0 %>
|
||||
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
|
||||
</p>
|
||||
<p><%= f.select :group, group_options, required: true %></p>
|
||||
</div>
|
@ -4,40 +4,45 @@
|
||||
|
||||
<div class="contextual">
|
||||
<% 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_new_ingredient"), '#', :class => 'icon icon-add',
|
||||
:onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div id="add-unit" <%= 'style=display:none;' if @unit.errors.empty? %>>
|
||||
<h2><%= t ".heading_new_unit" %></h2>
|
||||
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
|
||||
<h2><%= t ".heading_new_ingredient" %></h2>
|
||||
|
||||
<%= labelled_form_for @unit,
|
||||
:url => project_units_path(@project),
|
||||
:html => {:id => 'unit-form'} do |f| %>
|
||||
<%= render :partial => 'units/form', :locals => { :f => f } %>
|
||||
<%= labelled_form_for @ingredient,
|
||||
:url => project_ingredients_path(@project),
|
||||
:html => {:id => 'ingredient-form'} do |f| %>
|
||||
<%= render :partial => 'ingredients/form', :locals => { :f => f } %>
|
||||
<%= submit_tag l(:button_create) %>
|
||||
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %>
|
||||
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %>
|
||||
<% end %>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<h2><%= t ".heading" %></h2>
|
||||
<% if @units.any? %>
|
||||
<% if @ingredients.any? %>
|
||||
<table class="list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= l(:field_shortname) %></th>
|
||||
<th><%= l(:field_name) %></th>
|
||||
<th><%= l(:field_ref_amount) %></th>
|
||||
<th><%= l(:field_ref_unit) %></th>
|
||||
<th><%= l(:field_group) %></th>
|
||||
<th><%= l(:field_source) %></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), data: {} %></td>
|
||||
<% @ingredients.each do |i| %>
|
||||
<tr id="ingredient-<%= i.id %>" class="ingredient <%= 'hidden' if i.hidden %>">
|
||||
<td class="ingredientname"><%= i.name %></td>
|
||||
<td class="ref_amount"><%= i.ref_amount %> [<%= i.ref_unit %>]</td>
|
||||
<td class="group"><%= i.group %></td>
|
||||
<td class="source"><%= i.source %></td>
|
||||
<td><%= delete_link ingredient_path(i), data: {} %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
@ -1,10 +1,12 @@
|
||||
# English strings go here for Rails i18n
|
||||
en:
|
||||
body_trackers_menu_caption: 'Body trackers'
|
||||
field_shortname: 'Short name'
|
||||
field_action: 'Action'
|
||||
field_ref_amount: 'Reference amount'
|
||||
field_group: 'Group'
|
||||
field_domain: 'Domain'
|
||||
field_parent_quantity: 'Parent'
|
||||
field_shortname: 'Short name'
|
||||
activerecord:
|
||||
errors:
|
||||
models:
|
||||
@ -25,6 +27,13 @@ en:
|
||||
link_units: 'Units'
|
||||
link_defaults: 'Load defaults'
|
||||
confirm_defaults: 'This will load default quantities and units. Continue?'
|
||||
ingredients:
|
||||
index:
|
||||
heading: 'Ingredients'
|
||||
heading_new_ingredient: 'New ingredient'
|
||||
form:
|
||||
groups:
|
||||
meat: 'meat'
|
||||
quantities:
|
||||
index:
|
||||
heading: 'Quantities'
|
||||
|
@ -22,9 +22,9 @@ class CreateUnits < ActiveRecord::Migration
|
||||
t.string :name
|
||||
t.decimal :ref_amount
|
||||
t.references :ref_unit
|
||||
t.boolean :hidden
|
||||
t.references :source
|
||||
t.integer :group
|
||||
t.references :source
|
||||
t.boolean :hidden
|
||||
end
|
||||
|
||||
create_table :nutrients do |t|
|
||||
|
6
init.rb
6
init.rb
@ -13,13 +13,15 @@ Redmine::Plugin.register :body_tracking do
|
||||
project_module :body_tracking do
|
||||
permission :view_body_trackers, {
|
||||
:body_trackers => [:index],
|
||||
:ingredients => [:index],
|
||||
:quantities => [:index],
|
||||
:units => [:index],
|
||||
:quantities => [:index]
|
||||
}, read: true
|
||||
permission :manage_common, {
|
||||
:body_trackers => [:defaults],
|
||||
:ingredients => [:create, :destroy],
|
||||
:quantities => [:create, :destroy],
|
||||
:units => [:create, :destroy],
|
||||
:quantities => [:create, :destroy]
|
||||
}, require: :loggedin
|
||||
end
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
module BodyTracking
|
||||
module ProjectPatch
|
||||
Project.class_eval do
|
||||
has_many :units, dependent: :destroy
|
||||
has_many :ingredients, dependent: :destroy
|
||||
|
||||
has_many :quantities, -> { order "lft" }, dependent: :destroy
|
||||
has_many :units, dependent: :destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user