diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb
index 4bc4e88..491369e 100644
--- a/app/controllers/ingredients_controller.rb
+++ b/app/controllers/ingredients_controller.rb
@@ -4,7 +4,6 @@ class IngredientsController < ApplicationController
layout 'body_tracking'
menu_item :body_trackers
helper :body_trackers
- helper_method :current_view
include Concerns::Finders
@@ -16,15 +15,12 @@ class IngredientsController < ApplicationController
before_action :authorize
def index
- self.current_view = :ingredients
- prepare_items
+ prepare_ingredients
end
def new
@ingredient = @project.ingredients.new
- # passing attr for Nutrient after_initialize
- # FIXME: is this necessary when creating through association?
- @ingredient.nutrients.new(ingredient: @ingredient)
+ @ingredient.nutrients.new(unit: @ingredient.ref_unit)
end
def create
@@ -33,7 +29,7 @@ class IngredientsController < ApplicationController
flash[:notice] = 'Created new ingredient'
prepare_items
else
- @ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty?
+ @ingredient.nutrients.new(unit: @ingredient.ref_unit) if @ingredient.nutrients.empty?
render :new
end
end
@@ -63,14 +59,12 @@ class IngredientsController < ApplicationController
end
def nutrients
- self.current_view = :nutrients
- prepare_items
+ prepare_nutrients
end
def toggle_column
@project.nutrient_columns.toggle!(@quantity)
- prepare_items
- render :index
+ prepare_nutrients
end
def filter
@@ -206,6 +200,10 @@ class IngredientsController < ApplicationController
)
end
+ def prepare_items
+ params[:view] == 'index' ? prepare_ingredients : prepare_nutrients
+ end
+
def prepare_ingredients
@ingredients, @formula_q = @project.ingredients
.includes(:ref_unit, :source)
@@ -217,15 +215,4 @@ class IngredientsController < ApplicationController
@ingredients, @requested_n, @extra_n, @formula_q = @project.ingredients
.filter(session[:i_filters], @quantities)
end
-
- def prepare_items
- (current_view == :nutrients) ? prepare_nutrients : prepare_ingredients
- end
-
- def current_view
- @current_view || (params[:view_mode] == "nutrients" ? :nutrients : :ingredients)
- end
- def current_view=(cv)
- @current_view = cv
- end
end
diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb
index 24ad9ef..a84dca4 100644
--- a/app/helpers/ingredients_helper.rb
+++ b/app/helpers/ingredients_helper.rb
@@ -33,8 +33,8 @@ module IngredientsHelper
end
end
- def action_links(i)
- link_to(l(:button_edit), edit_ingredient_path(i, view_mode: current_view),
+ def action_links(i, view)
+ link_to(l(:button_edit), edit_ingredient_path(i, view: view),
{remote: true, class: "icon icon-edit"}) +
delete_link(ingredient_path(i), {remote: true, data: {}})
end
diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb
index ebbd1e6..836ea29 100644
--- a/app/models/nutrient.rb
+++ b/app/models/nutrient.rb
@@ -5,10 +5,4 @@ class Nutrient < ActiveRecord::Base
validates :quantity, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_than_or_equal_to: 0.0}
-
- after_initialize do
- if new_record?
- self.unit ||= self.ingredient.ref_unit
- end
- end
end
diff --git a/app/views/ingredients/_contextual.html.erb b/app/views/ingredients/_contextual.html.erb
index 0c22fcf..e016cc9 100644
--- a/app/views/ingredients/_contextual.html.erb
+++ b/app/views/ingredients/_contextual.html.erb
@@ -1,7 +1,6 @@
<% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".link_import_ingredients"), '#', class: 'icon icon-multiple',
onclick: '$("#import-ingredients").show(); $("#filename").focus(); return false;' %>
- <%= link_to t(".link_new_ingredient"),
- new_project_ingredient_path(@project, view_mode: current_view),
+ <%= link_to t(".link_new_ingredient"), new_project_ingredient_path(@project, view: view),
{remote: true, class: 'icon icon-add'} %>
<% end %>
diff --git a/app/views/ingredients/_edit_form.html.erb b/app/views/ingredients/_edit_form.html.erb
index bba364e..945191c 100644
--- a/app/views/ingredients/_edit_form.html.erb
+++ b/app/views/ingredients/_edit_form.html.erb
@@ -1,5 +1,5 @@
<%= labelled_form_for @ingredient,
- url: ingredient_path(@ingredient, view_mode: current_view),
+ url: ingredient_path(@ingredient, view: view),
method: :patch, remote: true,
html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %>
diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb
index 55f3ecd..679747e 100644
--- a/app/views/ingredients/_form.html.erb
+++ b/app/views/ingredients/_form.html.erb
@@ -4,18 +4,20 @@
<%= f.text_field :name, size: 40, required: true %>
<%= f.text_area :notes, cols: 40, rows: 3, required: false,
style: "width: 100%;" %>
-
- <%= f.number_field :ref_amount, size: 8, required: true, min: 0,
- label: :field_reference %>
- <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
-
- <%= f.select :group, group_options, required: true %>
-
<%= f.select :source_id, source_options,
- {required: false, include_blank: t('.null_source')} %>
+
<%= f.select :group, group_options, required: true %>
+
+ <%= f.number_field :ref_amount, size: 8, required: true, min: 0,
+ label: :field_reference %>
+ <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
+
+
+ <%= f.select :source_id, source_options,
+ {required: false, include_blank: t('.null_source')} %>
+
<%= f.text_field :source_ident, size: 25, required: false %>
diff --git a/app/views/ingredients/_index.html.erb b/app/views/ingredients/_index.html.erb
index 926323c..1ba2b5e 100644
--- a/app/views/ingredients/_index.html.erb
+++ b/app/views/ingredients/_index.html.erb
@@ -1,5 +1,5 @@
<%= render partial: 'ingredients/filters',
- locals: {url: filter_project_ingredients_path(@project)} %>
+ locals: {url: filter_project_ingredients_path(@project, view: :index)} %>
<% if @ingredients.any? { |i| i.persisted? } %>
<%= error_messages_for @formula_q.formula if @formula_q %>
@@ -35,7 +35,7 @@
<%= i.source.name if i.source.present? %>
<%= ", #{i.source_ident}" if i.source_ident.present? %>
- <%= action_links(i) %> |
+ <%= action_links(i, :index) %> |
<% end %>
diff --git a/app/views/ingredients/_new_form.html.erb b/app/views/ingredients/_new_form.html.erb
index 79b5904..83d5b62 100644
--- a/app/views/ingredients/_new_form.html.erb
+++ b/app/views/ingredients/_new_form.html.erb
@@ -1,7 +1,7 @@
<%= t ".heading_new_ingredient" %>
<%= labelled_form_for @ingredient,
- url: project_ingredients_path(@project, view_mode: current_view),
+ url: project_ingredients_path(@project, view: view),
remote: true,
html: {id: 'new-ingredient-form', name: 'new-ingredient-form'} do |f| %>
diff --git a/app/views/ingredients/_nutrients.html.erb b/app/views/ingredients/_nutrients.html.erb
index a779627..6364a7f 100644
--- a/app/views/ingredients/_nutrients.html.erb
+++ b/app/views/ingredients/_nutrients.html.erb
@@ -1,5 +1,5 @@
<%= render partial: 'ingredients/filters',
- locals: {url: filter_project_ingredients_path(@project, view_mode: current_view)} %>
+ locals: {url: filter_project_ingredients_path(@project, view: :nutrients)} %>
<% if @ingredients.any? %>
<%= render partial: 'ingredients/options' %>
@@ -17,8 +17,7 @@
<%= link_to '',
- toggle_column_project_ingredients_path(@project, quantity_id: q.id,
- view_mode: current_view),
+ toggle_column_project_ingredients_path(@project, quantity_id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
<%= q.name %>
@@ -39,7 +38,7 @@
<% @requested_n[index].each do |*, value| %>
| <%= format_value(value) %> |
<% end %>
- <%= action_links(i) %> |
+ <%= action_links(i, :nutrients) %> |
@@ -58,7 +57,9 @@
<%= format_value(value) %>
<% end %>
- <%= action_links(i) %> |
+
+ <%= action_links(i, :nutrients) %>
+ |
<% next unless @quantities.length > 0 %>
diff --git a/app/views/ingredients/_options.html.erb b/app/views/ingredients/_options.html.erb
index 35de053..9b9708e 100644
--- a/app/views/ingredients/_options.html.erb
+++ b/app/views/ingredients/_options.html.erb
@@ -1,7 +1,7 @@