diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb
index ea0eae5..643d4c7 100644
--- a/app/controllers/concerns/finders.rb
+++ b/app/controllers/concerns/finders.rb
@@ -1,9 +1,9 @@
module Concerns::Finders
private
- def find_ingredient
- @ingredient = Ingredient.find(params[:id])
- @project = @ingredient.project
+ def find_food
+ @food = Food.find(params[:id])
+ @project = @food.project
rescue ActiveRecord::RecordNotFound
render_404
end
diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/foods_controller.rb
similarity index 71%
rename from app/controllers/ingredients_controller.rb
rename to app/controllers/foods_controller.rb
index 491369e..bbacaff 100644
--- a/app/controllers/ingredients_controller.rb
+++ b/app/controllers/foods_controller.rb
@@ -1,4 +1,4 @@
-class IngredientsController < ApplicationController
+class FoodsController < ApplicationController
require 'csv'
layout 'body_tracking'
@@ -11,25 +11,25 @@ class IngredientsController < ApplicationController
before_action :find_project_by_project_id,
only: [:index, :new, :create, :nutrients, :filter, :import]
before_action :find_quantity_by_quantity_id, only: [:toggle_column]
- before_action :find_ingredient, only: [:edit, :update, :destroy, :toggle]
+ before_action :find_food, only: [:edit, :update, :destroy, :toggle]
before_action :authorize
def index
- prepare_ingredients
+ prepare_foods
end
def new
- @ingredient = @project.ingredients.new
- @ingredient.nutrients.new(unit: @ingredient.ref_unit)
+ @food = @project.foods.new
+ @food.nutrients.new(unit: @food.ref_unit)
end
def create
- @ingredient = @project.ingredients.new(ingredient_params)
- if @ingredient.save
- flash[:notice] = 'Created new ingredient'
+ @food = @project.foods.new(food_params)
+ if @food.save
+ flash[:notice] = 'Created new food'
prepare_items
else
- @ingredient.nutrients.new(unit: @ingredient.ref_unit) if @ingredient.nutrients.empty?
+ @food.nutrients.new(unit: @food.ref_unit) if @food.nutrients.empty?
render :new
end
end
@@ -38,8 +38,8 @@ class IngredientsController < ApplicationController
end
def update
- if @ingredient.update(ingredient_params)
- flash[:notice] = 'Updated ingredient'
+ if @food.update(food_params)
+ flash[:notice] = 'Updated food'
prepare_items
render :index
else
@@ -48,13 +48,13 @@ class IngredientsController < ApplicationController
end
def destroy
- if @ingredient.destroy
- flash[:notice] = 'Deleted ingredient'
+ if @food.destroy
+ flash[:notice] = 'Deleted food'
end
end
def toggle
- @ingredient.toggle_hidden!
+ @food.toggle_hidden!
prepare_items
end
@@ -68,7 +68,7 @@ class IngredientsController < ApplicationController
end
def filter
- session[:i_filters] = params.permit(:name, :visibility, formula: [:code, :zero_nil])
+ session[:f_filters] = params.permit(:name, :visibility, formula: [:code, :zero_nil])
prepare_items
render :index
end
@@ -80,7 +80,7 @@ class IngredientsController < ApplicationController
quantities = @project.quantities.diet.map { |q| [q.name, q] }.to_h
units = @project.units.map { |u| [u.shortname, u] }.to_h
sources = @project.sources.map { |s| [s.name, s] }.to_h
- ingredients_params = []
+ foods_params = []
column_units = {}
CSV.foreach(params[:file].path, headers: true).with_index(2) do |row, line|
@@ -92,7 +92,7 @@ class IngredientsController < ApplicationController
warnings << "Line #{line}: unknown source name #{r['Source']}"
end
- i_params = {
+ f_params = {
name: r.delete('Name'),
notes: r.delete('Notes'),
ref_amount: 100.0,
@@ -138,12 +138,12 @@ class IngredientsController < ApplicationController
next if quantities[quantity].blank?
if quantity == 'Reference'
- i_params.update({
+ f_params.update({
ref_amount: amount.to_d,
ref_unit: unit
})
else
- i_params[:nutrients_attributes] << {
+ f_params[:nutrients_attributes] << {
quantity: quantities[quantity],
amount: amount.to_d,
unit: unit
@@ -151,20 +151,20 @@ class IngredientsController < ApplicationController
end
end
- ingredients_params << i_params
+ foods_params << f_params
end
else
warnings << 'No file selected'
end
if warnings.empty?
- ingredients = @project.ingredients.create(ingredients_params)
- flash[:notice] = "Imported #{ingredients.map(&:persisted?).count(true)} out of" \
- " #{ingredients_params.length} ingredients"
- skipped = ingredients.select { |i| !i.persisted? }
+ foods = @project.foods.create(foods_params)
+ flash[:notice] = "Imported #{foods.map(&:persisted?).count(true)} out of" \
+ " #{foods_params.length} foods"
+ skipped = foods.select { |f| !f.persisted? }
if skipped.length > 0
- skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" }
- flash[:warning] = "Ingredients skipped due to errors:
" \
+ skipped_desc = skipped.map { |f| "#{f.name} - #{f.errors.full_messages.join(', ')}" }
+ flash[:warning] = "Foods skipped due to errors:
" \
" #{skipped_desc.join('
').truncate(1024)}"
end
else
@@ -177,11 +177,11 @@ class IngredientsController < ApplicationController
private
def init_session_filters
- session[:i_filters] ||= {formula: {}}
+ session[:f_filters] ||= {formula: {}}
end
- def ingredient_params
- params.require(:ingredient).permit(
+ def food_params
+ params.require(:food).permit(
:name,
:notes,
:ref_amount,
@@ -201,18 +201,18 @@ class IngredientsController < ApplicationController
end
def prepare_items
- params[:view] == 'index' ? prepare_ingredients : prepare_nutrients
+ params[:view] == 'index' ? prepare_foods : prepare_nutrients
end
- def prepare_ingredients
- @ingredients, @formula_q = @project.ingredients
+ def prepare_foods
+ @foods, @formula_q = @project.foods
.includes(:ref_unit, :source)
- .filter(session[:i_filters])
+ .filter(session[:f_filters])
end
def prepare_nutrients
@quantities = @project.nutrient_quantities.includes(:formula)
- @ingredients, @requested_n, @extra_n, @formula_q = @project.ingredients
- .filter(session[:i_filters], @quantities)
+ @foods, @requested_n, @extra_n, @formula_q = @project.foods
+ .filter(session[:f_filters], @quantities)
end
end
diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb
index 89ab654..cc81507 100644
--- a/app/controllers/meals_controller.rb
+++ b/app/controllers/meals_controller.rb
@@ -16,6 +16,6 @@ class MealsController < ApplicationController
private
def prepare_meals
- @meals = @project.meals.includes(:ingredients)
+ @meals = @project.meals.includes(:foods)
end
end
diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/foods_helper.rb
similarity index 79%
rename from app/helpers/ingredients_helper.rb
rename to app/helpers/foods_helper.rb
index a84dca4..5254c53 100644
--- a/app/helpers/ingredients_helper.rb
+++ b/app/helpers/foods_helper.rb
@@ -1,4 +1,4 @@
-module IngredientsHelper
+module FoodsHelper
def quantity_options
nested_set_options(@project.quantities.diet) do |q|
raw("#{' ' * q.level}#{q.name}")
@@ -28,14 +28,14 @@ module IngredientsHelper
def group_options
translations = t('.groups')
- Ingredient.groups.map do |k,v|
+ Food.groups.map do |k,v|
[translations[k.to_sym], k]
end
end
- def action_links(i, view)
- link_to(l(:button_edit), edit_ingredient_path(i, view: view),
+ def action_links(f, view)
+ link_to(l(:button_edit), edit_food_path(f, view: view),
{remote: true, class: "icon icon-edit"}) +
- delete_link(ingredient_path(i), {remote: true, data: {}})
+ delete_link(food_path(f), {remote: true, data: {}})
end
end
diff --git a/app/models/ingredient.rb b/app/models/food.rb
similarity index 91%
rename from app/models/ingredient.rb
rename to app/models/food.rb
index c7d4c09..f06490d 100644
--- a/app/models/ingredient.rb
+++ b/app/models/food.rb
@@ -1,4 +1,4 @@
-class Ingredient < ActiveRecord::Base
+class Food < ActiveRecord::Base
enum group: {
other: 0,
dish: 1,
@@ -16,7 +16,7 @@ class Ingredient < ActiveRecord::Base
belongs_to :ref_unit, class_name: 'Unit', required: true
belongs_to :source, required: false
- has_many :nutrients, inverse_of: :ingredient, dependent: :destroy, validate: true
+ has_many :nutrients, inverse_of: :food, dependent: :destroy, validate: true
validates :nutrients, presence: true
accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs|
attrs['quantity_id'].blank? && attrs['amount'].blank?
diff --git a/app/models/meal.rb b/app/models/meal.rb
index e69de29..9e5bd52 100644
--- a/app/models/meal.rb
+++ b/app/models/meal.rb
@@ -0,0 +1,6 @@
+class Meal < ActiveRecord::Base
+ belongs_to :project, required: true
+
+ has_many :ingredients, as: :composition, dependent: :destroy
+ has_many :foods, through: :ingredients
+end
diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb
index 836ea29..418e4f7 100644
--- a/app/models/nutrient.rb
+++ b/app/models/nutrient.rb
@@ -1,8 +1,8 @@
class Nutrient < ActiveRecord::Base
- belongs_to :ingredient, inverse_of: :nutrients, required: true
+ belongs_to :food, inverse_of: :nutrients, required: true
belongs_to :quantity, required: true
belongs_to :unit, required: true
- validates :quantity, uniqueness: {scope: :ingredient_id}
+ validates :quantity, uniqueness: {scope: :food_id}
validates :amount, numericality: {greater_than_or_equal_to: 0.0}
end
diff --git a/app/views/foods/_contextual.html.erb b/app/views/foods/_contextual.html.erb
new file mode 100644
index 0000000..aeef49b
--- /dev/null
+++ b/app/views/foods/_contextual.html.erb
@@ -0,0 +1,6 @@
+<% if User.current.allowed_to?(:manage_common, @project) %>
+ <%= link_to t(".link_import_foods"), '#', class: 'icon icon-multiple',
+ onclick: '$("#import-foods").show(); $("#filename").focus(); return false;' %>
+ <%= link_to t(".link_new_food"), new_project_food_path(@project, view: view),
+ {remote: true, class: 'icon icon-add'} %>
+<% end %>
diff --git a/app/views/ingredients/_edit_form.html.erb b/app/views/foods/_edit_form.html.erb
similarity index 52%
rename from app/views/ingredients/_edit_form.html.erb
rename to app/views/foods/_edit_form.html.erb
index 945191c..da1d05f 100644
--- a/app/views/ingredients/_edit_form.html.erb
+++ b/app/views/foods/_edit_form.html.erb
@@ -1,9 +1,9 @@
-<%= labelled_form_for @ingredient,
- url: ingredient_path(@ingredient, view: view),
+<%= labelled_form_for @food,
+ url: food_path(@food, view: view),
method: :patch, remote: true,
- html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %>
+ html: {id: 'food-edit-form', name: 'food-edit-form'} do |f| %>
- <%= render partial: 'ingredients/form', locals: {f: f} %>
+ <%= render partial: 'foods/form', locals: {f: f} %>
diff --git a/app/views/ingredients/_filters.html.erb b/app/views/foods/_filters.html.erb similarity index 91% rename from app/views/ingredients/_filters.html.erb rename to app/views/foods/_filters.html.erb index 6d6ce81..dd6348b 100644 --- a/app/views/ingredients/_filters.html.erb +++ b/app/views/foods/_filters.html.erb @@ -9,14 +9,14 @@
- <%= text_field_tag 'name', session[:i_filters][:name], placeholder: 'name' %> + <%= text_field_tag 'name', session[:f_filters][:name], placeholder: 'name' %> | - <%= select_tag 'visibility', visibility_options(session[:i_filters][:visibility]), + <%= select_tag 'visibility', visibility_options(session[:f_filters][:visibility]), prompt: t('.visibility_prompt'), onchange: '$("#filters-form").submit();' %> | - <%= text_field_tag 'formula[code]', session[:i_filters][:formula][:code], + <%= text_field_tag 'formula[code]', session[:f_filters][:formula][:code], placeholder: 'conditional expression including nutrients', size: 40, style: 'box-sizing:border-box; width:100%;' %> | diff --git a/app/views/ingredients/_form.html.erb b/app/views/foods/_form.html.erb similarity index 93% rename from app/views/ingredients/_form.html.erb rename to app/views/foods/_form.html.erb index 679747e..58caaf5 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/foods/_form.html.erb @@ -1,4 +1,4 @@ -<%= error_messages_for @ingredient %> +<%= error_messages_for @food %>