diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb
index 451d473..2cc9732 100644
--- a/app/controllers/concerns/finders.rb
+++ b/app/controllers/concerns/finders.rb
@@ -24,15 +24,9 @@ module Concerns::Finders
def find_measurement
@measurement = Measurement.find(params[:id])
- @routine = @measurement.routine
- @project = @routine.project
- rescue ActiveRecord::RecordNotFound
- render_404
- end
-
- def find_measurement_routine
- @routine = MeasurementRoutine.find(params[:id])
- @project = @routine.project
+ # DON'T set @routine here: @routine is a context for :readouts view (set
+ # elsewhere), not a # @measurement.routine
+ @project = @measurement.routine.project
rescue ActiveRecord::RecordNotFound
render_404
end
@@ -44,6 +38,10 @@ module Concerns::Finders
render_404
end
+ def find_measurement_routine_by_measurement_routine_id
+ find_measurement_routine(params[:measurement_routine_id])
+ end
+
def find_quantity(id = params[:id])
@quantity = Quantity.find(id)
@project = @quantity.project
diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb
index d2cba5e..1185ef9 100644
--- a/app/controllers/foods_controller.rb
+++ b/app/controllers/foods_controller.rb
@@ -209,14 +209,12 @@ class FoodsController < ApplicationController
end
def prepare_foods
- @foods, @filter_q = @project.foods
- .includes(:ref_unit, :source)
+ @foods, @filter_q = @project.foods.includes(:ref_unit, :source)
.filter(session[:f_filters])
end
def prepare_nutrients
@quantities = @project.nutrient_quantities.includes(:formula)
- @foods, @requested_n, @extra_n, @filter_q = @project.foods
- .filter(session[:f_filters], @quantities)
+ @foods, @filter_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 fb3849e..26b91f9 100644
--- a/app/controllers/meals_controller.rb
+++ b/app/controllers/meals_controller.rb
@@ -82,6 +82,6 @@ class MealsController < ApplicationController
def prepare_meals
@quantities = @project.meal_quantities.includes(:formula)
- @foods, @requested_n, * = @project.meal_foods.compute_quantities(@quantities)
+ @foods = @project.meal_foods.compute_quantities(@quantities)
end
end
diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb
index 5951a1e..3b40b71 100644
--- a/app/controllers/measurements_controller.rb
+++ b/app/controllers/measurements_controller.rb
@@ -9,8 +9,13 @@ class MeasurementsController < ApplicationController
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure]
before_action :find_measurement, only: [:edit, :update, :destroy, :retake]
+ # @routine is set for :readouts view ONLY
before_action :find_measurement_routine, only: [:readouts, :toggle_exposure]
+ before_action :find_measurement_routine_by_measurement_routine_id,
+ only: [:new, :create, :edit, :update, :retake, :filter],
+ if: -> { params[:view] == 'readouts' }
before_action :authorize
+ before_action :set_view_params
def index
prepare_measurements
@@ -18,7 +23,7 @@ class MeasurementsController < ApplicationController
def new
@measurement = @project.measurements.new
- @routine = @measurement.build_routine
+ @measurement.build_routine
@measurement.readouts.new
end
@@ -31,10 +36,10 @@ class MeasurementsController < ApplicationController
update_routine_from_params
@measurement.attributes = measurement_params
@measurement.routine.project = @project
- @routine = @measurement.routine
if @measurement.save
- if @routine.readout_exposures.empty?
- @routine.quantities << @measurement.readouts.map(&:quantity).first(6)
+ routine = @measurement.routine
+ if routine.readout_exposures.empty?
+ routine.quantities << @measurement.readouts.map(&:quantity).first(6)
end
flash[:notice] = 'Created new measurement'
@@ -52,7 +57,7 @@ class MeasurementsController < ApplicationController
update_routine_from_params
if @measurement.update(measurement_params)
flash[:notice] = 'Updated measurement'
- if @measurement.routine.previous_changes.has_key?(:name) && params[:view] == 'readouts'
+ if @measurement.routine.previous_changes.has_key?(:name) && @routine
render js: "window.location.pathname='#{readouts_measurement_routine_path(@routine)}'"
else
prepare_items
@@ -126,19 +131,26 @@ class MeasurementsController < ApplicationController
end
def prepare_items
- params[:view] == 'index' ? prepare_measurements : prepare_readouts
+ @routine ? prepare_readouts : prepare_measurements
end
def prepare_measurements
- @measurements, @filter_q = @project.measurements
- .includes(:routine, :source, :readouts)
+ @measurements, @filter_q = @project.measurements.includes(:routine, :source, :readouts)
.filter(session[:m_filters])
end
def prepare_readouts
@quantities = @routine.quantities.includes(:formula)
- @measurements, @requested_r, @extra_r, @filter_q = @routine.measurements
- .includes(:routine, :source)
+ @measurements, @filter_q = @routine.measurements.includes(:routine, :source)
.filter(session[:m_filters], @quantities)
end
+
+ def set_view_params
+ @view_params =
+ if @routine
+ {view: :readouts, measurement_routine_id: @routine.id}
+ else
+ {view: :index}
+ end
+ end
end
diff --git a/app/helpers/measurements_helper.rb b/app/helpers/measurements_helper.rb
index ef376e2..77d646d 100644
--- a/app/helpers/measurements_helper.rb
+++ b/app/helpers/measurements_helper.rb
@@ -17,10 +17,10 @@ module MeasurementsHelper
end
end
- def action_links(m, view)
- link_to(l(:button_retake), retake_measurement_path(m, view: view),
+ def action_links(m)
+ link_to(l(:button_retake), retake_measurement_path(m, @view_params),
{remote: true, class: "icon icon-reload"}) +
- link_to(l(:button_edit), edit_measurement_path(m, view: view),
+ link_to(l(:button_edit), edit_measurement_path(m, @view_params),
{remote: true, class: "icon icon-edit"}) +
delete_link(measurement_path(m), {remote: true, data: {}})
end
diff --git a/app/views/foods/_filters.html.erb b/app/views/foods/_filters.html.erb
index 38e4f8a..e023144 100644
--- a/app/views/foods/_filters.html.erb
+++ b/app/views/foods/_filters.html.erb
@@ -3,8 +3,8 @@
<%= error_messages_for @filter_q %>
- <%= form_tag url, id: 'filters-form', name: 'filters-form',
- method: :get, remote: true do %>
+ <%= form_tag url, id: 'filters-form', name: 'filters-form', method: :get,
+ remote: true do %>
diff --git a/app/views/foods/_nutrients.html.erb b/app/views/foods/_nutrients.html.erb
index 86fa90b..3f73da2 100644
--- a/app/views/foods/_nutrients.html.erb
+++ b/app/views/foods/_nutrients.html.erb
@@ -28,51 +28,52 @@
- <% @foods.each_with_index do |f, index| %>
- <% row_class = "food#{' hidden' if f.hidden} #{cycle('odd', 'even')}" %>
-
+ <% extra_quantities = @foods.values.first.keys - @quantities %>
+ <% @foods.each do |food, nutrients| %>
+ <% row_class = "food#{' hidden' if food.hidden} #{cycle('odd', 'even')}" %>
+
- <%= f.name %>
+ <%= food.name %>
|
- <% @requested_n[index].each do |*, value| %>
- <%= format_value(value) %> |
+ <% @quantities.each do |q| %>
+ <%= format_value(nutrients[q]) %> |
<% end %>
- <%= action_links(f, :nutrients) %> |
+ <%= action_links(food, :nutrients) %> |
<% if @quantities.length > 0
- rows = (@extra_n[index].length - 1) / @quantities.length + 2
+ rows = (nutrients.length - 1) / @quantities.length + 1
else
rows = 1
end %>
- <%= f.name %>
+ <%= food.name %>
|
- <% @requested_n[index].each do |q, value| %>
+ <% @quantities.each do |q| %>
<%= q.name %>
- <%= format_value(value) %>
+ <%= format_value(nutrients[q]) %>
|
<% end %>
- <%= action_links(f, :nutrients) %>
+ <%= action_links(food, :nutrients) %>
|
<% next unless @quantities.length > 0 %>
- <% @extra_n[index].each_slice(@quantities.length) do |values| %>
+ <% extra_quantities.each_slice(@quantities.length) do |eqs| %>
<% end %>
diff --git a/app/views/measurement_routines/_form.html.erb b/app/views/measurement_routines/_form.html.erb
index a9aac87..ce32e44 100644
--- a/app/views/measurement_routines/_form.html.erb
+++ b/app/views/measurement_routines/_form.html.erb
@@ -1,5 +1,5 @@
- <%= fields_for 'measurement[routine_attributes]', @routine do |ff| %>
+ <%= fields_for 'measurement[routine_attributes]', routine do |ff| %>
<%= ff.hidden_field :id %>
diff --git a/app/views/measurement_routines/_show_form.html.erb b/app/views/measurement_routines/_show_form.html.erb
index c521368..29946f7 100644
--- a/app/views/measurement_routines/_show_form.html.erb
+++ b/app/views/measurement_routines/_show_form.html.erb
@@ -1,10 +1,9 @@
-<%= fields_for 'measurement[routine_attributes]', @routine do |ff| %>
+<%= fields_for 'measurement[routine_attributes]', routine do |ff| %>
<%= ff.select :id,
- options_from_collection_for_select(@project.measurement_routines,
- :id, :name, @routine.id),
+ options_from_collection_for_select(@project.measurement_routines, :id, :name, routine.id),
{required: true}, autocomplete: 'off',
onchange: "var mr_id = $('#measurement_routine_attributes_id').val();
$.ajax({
@@ -22,7 +21,7 @@
class: 'icon icon-edit' %>
<% end %>
-<% unless @routine.description.empty? %>
-
<%= @routine.description %>
+<% unless routine.description.empty? %>
+
<%= routine.description %>
<% end %>
diff --git a/app/views/measurement_routines/edit.js.erb b/app/views/measurement_routines/edit.js.erb
index 45983e2..45dcaa4 100644
--- a/app/views/measurement_routines/edit.js.erb
+++ b/app/views/measurement_routines/edit.js.erb
@@ -1 +1,2 @@
-$('#measurement-routine-form').html('<%= j render partial: 'measurement_routines/form' %>');
+$('#measurement-routine-form').html('<%= j render partial: 'measurement_routines/form',
+ locals: {routine: @routine} %>');
diff --git a/app/views/measurement_routines/show.js.erb b/app/views/measurement_routines/show.js.erb
index 66f83e6..a910cca 100644
--- a/app/views/measurement_routines/show.js.erb
+++ b/app/views/measurement_routines/show.js.erb
@@ -1 +1,2 @@
-$('#measurement-routine-form').html('<%= j render partial: 'measurement_routines/show_form' %>');
+$('#measurement-routine-form').html('<%= j render partial: 'measurement_routines/show_form',
+ locals: {routine: @routine} %>');
diff --git a/app/views/measurements/_contextual.html.erb b/app/views/measurements/_contextual.html.erb
index 821567a..b9451f1 100644
--- a/app/views/measurements/_contextual.html.erb
+++ b/app/views/measurements/_contextual.html.erb
@@ -1,4 +1,5 @@
<% if User.current.allowed_to?(:manage_common, @project) %>
- <%= link_to t(".link_new_measurement"), new_project_measurement_path(@project, view: view),
+ <%= link_to t(".link_new_measurement"),
+ new_project_measurement_path(@project, @view_params),
{remote: true, class: 'icon icon-add'} %>
<% end %>
diff --git a/app/views/measurements/_edit_form.html.erb b/app/views/measurements/_edit_form.html.erb
index 66ae19d..f731f20 100644
--- a/app/views/measurements/_edit_form.html.erb
+++ b/app/views/measurements/_edit_form.html.erb
@@ -1,5 +1,5 @@
<%= labelled_form_for @measurement,
- url: measurement_path(@measurement, view: view),
+ url: measurement_path(@measurement, @view_params),
method: :patch, remote: true,
html: {id: 'edit-measurement-form', name: 'edit-measurement-form'} do |f| %>
diff --git a/app/views/measurements/_filters.html.erb b/app/views/measurements/_filters.html.erb
index 82ad56f..c428bad 100644
--- a/app/views/measurements/_filters.html.erb
+++ b/app/views/measurements/_filters.html.erb
@@ -3,8 +3,8 @@
<%= error_messages_for @filter_q %>
- <%= form_tag url, id: 'filters-form', name: 'filters-form',
- method: :get, remote: true do %>
+ <%= form_tag url, id: 'filters-form', name: 'filters-form', method: :get,
+ remote: true do %>
diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb
index dc505c3..58e32d0 100644
--- a/app/views/measurements/_form.html.erb
+++ b/app/views/measurements/_form.html.erb
@@ -2,10 +2,12 @@
- <% if @routine.persisted? %>
- <%= render partial: 'measurement_routines/show_form', locals: {f: f} %>
+ <% if @measurement.routine.persisted? %>
+ <%= render partial: 'measurement_routines/show_form',
+ locals: {routine: @measurement.routine} %>
<% else %>
- <%= render partial: 'measurement_routines/form', locals: {f: f} %>
+ <%= render partial: 'measurement_routines/form',
+ locals: {routine: @measurement.routine} %>
<% end %>
diff --git a/app/views/measurements/_index.html.erb b/app/views/measurements/_index.html.erb
index d19552f..69ea208 100644
--- a/app/views/measurements/_index.html.erb
+++ b/app/views/measurements/_index.html.erb
@@ -1,5 +1,5 @@
<%= render partial: 'measurements/filters',
- locals: {url: filter_project_measurements_path(@project, view: :index)} %>
+ locals: {url: filter_project_measurements_path(@project, @view_params)} %>
<% if @measurements.any? { |m| m.persisted? } %>
<%= error_messages_for @filter_q.formula if @filter_q %>
@@ -29,7 +29,7 @@
<%= m.notes %> |
<%= m.source.name if m.source.present? %> |
-
<%= action_links(m, :index) %> |
+
<%= action_links(m) %> |
<% end %>
diff --git a/app/views/measurements/_new_form.html.erb b/app/views/measurements/_new_form.html.erb
index d140316..078a560 100644
--- a/app/views/measurements/_new_form.html.erb
+++ b/app/views/measurements/_new_form.html.erb
@@ -1,7 +1,7 @@
<%= t ".heading_new_measurement" %>
<%= labelled_form_for @measurement,
- url: project_measurements_path(@project, view: view),
+ url: project_measurements_path(@project, @view_params),
remote: true,
html: {id: 'new-measurement-form', name: 'new-measurement-form'} do |f| %>
diff --git a/app/views/measurements/_options.html.erb b/app/views/measurements/_options.html.erb
index 529aae7..c933efc 100644
--- a/app/views/measurements/_options.html.erb
+++ b/app/views/measurements/_options.html.erb
@@ -1,7 +1,7 @@