1
0

Added ingredient index and form

This commit is contained in:
cryptogopher 2019-09-13 14:48:43 +02:00
parent c916e7fd3c
commit c82f903360
9 changed files with 77 additions and 22 deletions

View File

@ -1,6 +1,10 @@
class IngredientsController < ApplicationController class IngredientsController < ApplicationController
before_action :find_project_by_project_id, only: [:index, :create]
before_action :authorize
def index def index
@ingredient = Ingredient.new
@ingredients = @project.ingredients
end end
def create def create

View File

@ -1,2 +1,14 @@
module IngredientsHelper 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 end

View File

@ -1,2 +1,13 @@
class Ingredient < ActiveRecord::Base 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 end

View 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>

View File

@ -4,40 +4,45 @@
<div class="contextual"> <div class="contextual">
<% if User.current.allowed_to?(:manage_common, @project) %> <% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".heading_new_unit"), '#', :class => 'icon icon-add', <%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add',
:onclick => 'showAndScrollTo("add-unit", "unit_shortname"); return false;' %> :onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
<% end %> <% end %>
</div> </div>
<div id="add-unit" <%= 'style=display:none;' if @unit.errors.empty? %>> <div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
<h2><%= t ".heading_new_unit" %></h2> <h2><%= t ".heading_new_ingredient" %></h2>
<%= labelled_form_for @unit, <%= labelled_form_for @ingredient,
:url => project_units_path(@project), :url => project_ingredients_path(@project),
:html => {:id => 'unit-form'} do |f| %> :html => {:id => 'ingredient-form'} do |f| %>
<%= render :partial => 'units/form', :locals => { :f => f } %> <%= render :partial => 'ingredients/form', :locals => { :f => f } %>
<%= submit_tag l(:button_create) %> <%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %> <%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %>
<% end %> <% end %>
<hr> <hr>
</div> </div>
<h2><%= t ".heading" %></h2> <h2><%= t ".heading" %></h2>
<% if @units.any? %> <% if @ingredients.any? %>
<table class="list"> <table class="list">
<thead> <thead>
<tr> <tr>
<th><%= l(:field_shortname) %></th>
<th><%= l(:field_name) %></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> <th style="width:15%"><%= l(:field_action) %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @units.each do |u| %> <% @ingredients.each do |i| %>
<tr id="unit-<%= u.id %>" class="unit"> <tr id="ingredient-<%= i.id %>" class="ingredient <%= 'hidden' if i.hidden %>">
<td class="shortname"><%= u.shortname %></td> <td class="ingredientname"><%= i.name %></td>
<td class="unitname"><%= u.name %></td> <td class="ref_amount"><%= i.ref_amount %> [<%= i.ref_unit %>]</td>
<td><%= delete_link unit_path(u), data: {} %></td> <td class="group"><%= i.group %></td>
<td class="source"><%= i.source %></td>
<td><%= delete_link ingredient_path(i), data: {} %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@ -1,10 +1,12 @@
# English strings go here for Rails i18n # English strings go here for Rails i18n
en: en:
body_trackers_menu_caption: 'Body trackers' body_trackers_menu_caption: 'Body trackers'
field_shortname: 'Short name'
field_action: 'Action' field_action: 'Action'
field_ref_amount: 'Reference amount'
field_group: 'Group'
field_domain: 'Domain' field_domain: 'Domain'
field_parent_quantity: 'Parent' field_parent_quantity: 'Parent'
field_shortname: 'Short name'
activerecord: activerecord:
errors: errors:
models: models:
@ -25,6 +27,13 @@ en:
link_units: 'Units' link_units: 'Units'
link_defaults: 'Load defaults' link_defaults: 'Load defaults'
confirm_defaults: 'This will load default quantities and units. Continue?' confirm_defaults: 'This will load default quantities and units. Continue?'
ingredients:
index:
heading: 'Ingredients'
heading_new_ingredient: 'New ingredient'
form:
groups:
meat: 'meat'
quantities: quantities:
index: index:
heading: 'Quantities' heading: 'Quantities'

View File

@ -22,9 +22,9 @@ class CreateUnits < ActiveRecord::Migration
t.string :name t.string :name
t.decimal :ref_amount t.decimal :ref_amount
t.references :ref_unit t.references :ref_unit
t.boolean :hidden
t.references :source
t.integer :group t.integer :group
t.references :source
t.boolean :hidden
end end
create_table :nutrients do |t| create_table :nutrients do |t|

View File

@ -13,13 +13,15 @@ Redmine::Plugin.register :body_tracking do
project_module :body_tracking do project_module :body_tracking do
permission :view_body_trackers, { permission :view_body_trackers, {
:body_trackers => [:index], :body_trackers => [:index],
:ingredients => [:index],
:quantities => [:index],
:units => [:index], :units => [:index],
:quantities => [:index]
}, read: true }, read: true
permission :manage_common, { permission :manage_common, {
:body_trackers => [:defaults], :body_trackers => [:defaults],
:ingredients => [:create, :destroy],
:quantities => [:create, :destroy],
:units => [:create, :destroy], :units => [:create, :destroy],
:quantities => [:create, :destroy]
}, require: :loggedin }, require: :loggedin
end end

View File

@ -1,8 +1,10 @@
module BodyTracking module BodyTracking
module ProjectPatch module ProjectPatch
Project.class_eval do Project.class_eval do
has_many :units, dependent: :destroy has_many :ingredients, dependent: :destroy
has_many :quantities, -> { order "lft" }, dependent: :destroy has_many :quantities, -> { order "lft" }, dependent: :destroy
has_many :units, dependent: :destroy
end end
end end
end end