1
0

Created common contextual menu for ingredient/nutrients views

Restored sidebar for nutrients view
This commit is contained in:
cryptogopher 2019-10-28 19:51:46 +01:00
parent 67efb6c6bc
commit c4e89def0a
7 changed files with 113 additions and 93 deletions

View File

@ -9,10 +9,31 @@ class IngredientsController < ApplicationController
@ingredient = @project.ingredients.new
# passing attr for Nutrient after_initialize
@ingredient.nutrients.new(ingredient: @ingredient)
@ingredients = @project.ingredients.includes(:ref_unit, :source)
@ingredients << @ingredient
end
def nutrients
@ingredient = @project.ingredients.new
@ingredient.nutrients.new(ingredient: @ingredient)
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
@header = @project.quantities.where(primary: true)
@nutrients = Hash.new { |h,k| h[k] = {} }
@descriptions = Hash.new { |h,k| h[k] = [] }
ingredients.each do |i|
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
if @header.include?(n.quantity)
@nutrients[i.name][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
else
@descriptions[i.name] << "#{n.quantity.name}: #{n.amount} [#{n.unit.shortname}]"
end
end
end
@descriptions.each { |k, v| @descriptions[k] = v.join(", ") }
end
def create
@ingredient = @project.ingredients.new(ingredient_params)
if @ingredient.save
@ -138,23 +159,6 @@ class IngredientsController < ApplicationController
redirect_to project_ingredients_url(@project)
end
def nutrients
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
@header = @project.quantities.where(primary: true)
@nutrients = Hash.new { |h,k| h[k] = {} }
@descriptions = Hash.new { |h,k| h[k] = [] }
ingredients.each do |i|
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
if @header.include?(n.quantity)
@nutrients[i.name][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
else
@descriptions[i.name] << "#{n.quantity.name}: #{n.amount} [#{n.unit.shortname}]"
end
end
end
@descriptions.each { |k, v| @descriptions[k] = v.join(", ") }
end
private
def ingredient_params

View File

@ -0,0 +1,6 @@
<% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-multiple',
:onclick => 'showAndScrollTo("import-ingredients", "filename"); return false;' %>
<%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add',
:onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
<% end %>

View File

@ -1,37 +1,53 @@
<%= error_messages_for @ingredient %>
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
<h2><%= t ".heading_new_ingredient" %></h2>
<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, label: :field_reference %>
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
<p><%= f.select :group, group_options, required: true %></p>
<div class="splitcontent">
<div class="splitcontentleft">
<p><%= f.select :source_id, source_options, required: false, include_blank: true %></p>
</div>
<div class="splitcontentright">
<p><%= f.text_field :source_ident, size: 25, required: false %></p>
</div>
</div>
<% @ingredient.nutrients.each_with_index do |n, index| %>
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient">
<%= ff.select :quantity_id, quantity_options,
{include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %>
<%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %>
<%= ff.select :unit_id, unit_options, {label: ''} %>
<%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
<%= link_to t(".button_delete_nutrient"), '#',
:class => 'icon icon-del',
:style => (@ingredient.nutrients.length > 1 ? "" : "display:none"),
:onclick => "deleteNutrient(); return false;" %>
<%= labelled_form_for @ingredient,
:url => project_ingredients_path(@project),
:html => {:id => 'ingredient-form'} do |f| %>
<%= 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,
label: :field_reference %>
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
<% end %>
<p><%= f.select :group, group_options, required: true %></p>
<div class="splitcontent">
<div class="splitcontentleft">
<p>
<%= f.select :source_id, source_options, required: false, include_blank: true %>
</p>
</div>
<div class="splitcontentright">
<p><%= f.text_field :source_ident, size: 25, required: false %></p>
</div>
</div>
<% @ingredient.nutrients.each_with_index do |n, index| %>
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient">
<%= ff.select :quantity_id, quantity_options,
{include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %>
<%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %>
<%= ff.select :unit_id, unit_options, {label: ''} %>
<%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
<%= link_to t(".button_delete_nutrient"), '#',
:class => 'icon icon-del',
:style => (@ingredient.nutrients.length > 1 ? "" : "display:none"),
:onclick => "deleteNutrient(); return false;" %>
</p>
<% end %>
<% end %>
<p>
<%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add',
:onclick => 'addNutrient(); return false;' %>
</p>
</div>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %>
<% end %>
<p><%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add',
:onclick => 'addNutrient(); return false;' %></p>
<hr>
</div>
<%= javascript_tag do %>

View File

@ -0,0 +1,20 @@
<div id="import-ingredients" style="display:none;">
<h2><%= t ".heading_import_ingredients" %></h2>
<%= form_tag import_project_ingredients_path(@project),
:multipart => true,
:id => 'import-form' do %>
<div class="box tabular">
<p>
<label><%= t ".label_import_select_csv_file" %></label>
<%= file_field_tag 'file' %>
</p>
<p>
<%= t ".import_hints" %>
</p>
</div>
<%= submit_tag l(:button_import) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#import-ingredients").hide()' %>
<% end %>
<hr>
</div>

View File

@ -5,47 +5,12 @@
<div class="contextual">
<%= link_to t(".heading_nutrient_view"), nutrients_project_ingredients_path(@project),
:class => 'icon icon-stats' %>
<% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-multiple',
:onclick => 'showAndScrollTo("import-ingredients", "filename"); return false;' %>
<%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add',
:onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
<% end %>
<%= render :partial => 'ingredients/contextual' %>
</div>
<div id="import-ingredients" style="display:none;">
<h2><%= t ".heading_import_ingredients" %></h2>
<%= render :partial => 'ingredients/import' %>
<%= form_tag import_project_ingredients_path(@project),
:multipart => true,
:id => 'import-form' do %>
<div class="box tabular">
<p>
<label><%= t ".label_import_select_csv_file" %></label>
<%= file_field_tag 'file' %>
</p>
<p>
<%= t ".import_hints" %>
</p>
</div>
<%= submit_tag l(:button_import) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#import-ingredients").hide()' %>
<% end %>
<hr>
</div>
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
<h2><%= t ".heading_new_ingredient" %></h2>
<%= 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-ingredient").hide()' %>
<% end %>
<hr>
</div>
<%= render :partial => 'ingredients/form' %>
<h2><%= t ".heading" %></h2>
<div id='ingredients'>

View File

@ -1,12 +1,17 @@
<% content_for :sidebar do %>
<%#= render :partial => 'body_trackers/sidebar' %>
<%= render :partial => 'body_trackers/sidebar' %>
<% end %>
<div class="contextual">
<%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project),
:class => 'icon icon-list' %>
<%= render :partial => 'ingredients/contextual' %>
</div>
<%= render :partial => 'ingredients/import' %>
<%= render :partial => 'ingredients/form' %>
<h2><%= t ".heading" %></h2>
<% if @nutrients.any? %>
<table class="nutrients list">

View File

@ -39,11 +39,11 @@ en:
link_defaults: 'Load defaults'
confirm_defaults: 'This will load default quantities and units. Continue?'
ingredients:
index:
heading: 'Ingredients'
heading_nutrient_view: 'Nutrient view'
contextual:
heading_import_ingredients: 'Import'
heading_new_ingredient: 'New ingredient'
import:
heading_import_ingredients: 'Import'
label_import_select_csv_file: 'Select CSV file'
import_hints: 'CSV file has to include header with column names. Recognized column
names are:
@ -54,15 +54,19 @@ en:
Sample header: "Name,Reference,Group,Proteins[g],Fats[g],Carbohydrates[g]".
Sample data row: "Brussels,100[g],other,3.4,300[mg],9".
Unit given in data row has precedence over that specified in header.'
nutrients:
heading: 'Nutrients'
heading_ingredient_list: 'Ingredient list'
form:
heading_new_ingredient: 'New ingredient'
button_add_nutrient: 'Add nutrient'
button_delete_nutrient: 'Delete'
groups:
other: 'other'
meat: 'meat'
index:
heading: 'Ingredients'
heading_nutrient_view: 'Nutrient view'
nutrients:
heading: 'Nutrients'
heading_ingredient_list: 'Ingredient list'
sources:
index:
heading: 'Data sources'