diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb index 2937225..ea0eae5 100644 --- a/app/controllers/concerns/finders.rb +++ b/app/controllers/concerns/finders.rb @@ -10,7 +10,8 @@ module Concerns::Finders def find_measurement @measurement = Measurement.find(params[:id]) - @project = @measurement.routine.project + @routine = @measurement.routine + @project = @routine.project rescue ActiveRecord::RecordNotFound render_404 end @@ -22,15 +23,22 @@ module Concerns::Finders render_404 end - def find_quantity(id = :id) - @quantity = Quantity.find(params[id]) + def find_measurement_routine(id = params[:id]) + @routine = MeasurementRoutine.find(id) + @project = @routine.project + rescue ActiveRecord::RecordNotFound + render_404 + end + + def find_quantity(id = params[:id]) + @quantity = Quantity.find(id) @project = @quantity.project rescue ActiveRecord::RecordNotFound render_404 end def find_quantity_by_quantity_id - find_quantity(:quantity_id) + find_quantity(params[:quantity_id]) end def find_unit diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index b62fdf5..4bc4e88 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -23,6 +23,7 @@ class IngredientsController < ApplicationController 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) end diff --git a/app/controllers/measurement_routines_controller.rb b/app/controllers/measurement_routines_controller.rb new file mode 100644 index 0000000..bd34ffc --- /dev/null +++ b/app/controllers/measurement_routines_controller.rb @@ -0,0 +1,12 @@ +class MeasurementRoutinesController < ApplicationController + include Concerns::Finders + + before_action :find_measurement_routine, only: [:show, :edit] + before_action :authorize + + def show + end + + def edit + end +end diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index ed20b2c..4f2b779 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -18,16 +18,25 @@ class MeasurementsController < ApplicationController def new @measurement = @project.measurements.new - @measurement.build_routine + @routine = @measurement.build_routine @measurement.readouts.new end def create - @measurement = @project.measurements.new(measurement_params) + # Nested attributes cannot create outer object (Measurement) and at the same time edit + # existing nested object (MeasurementRoutine) if it's not associated with outer object + # https://stackoverflow.com/questions/6346134/ + # That's why routine needs to be found and associated before measurement initialization + @measurement = @project.measurements.new do |m| + routine_id = params[:measurement][:routine_attributes][:id] + m.routine = @project.measurement_routines.find_by(id: routine_id) if routine_id + end + @measurement.attributes = measurement_params @measurement.routine.project = @project + @routine = @measurement.routine if @measurement.save - if @measurement.routine.columns.empty? - @measurement.routine.quantities << @measurement.readouts.map(&:quantity).first(6) + if @routine.columns.empty? + @routine.quantities << @measurement.readouts.map(&:quantity).first(6) end flash[:notice] = 'Created new measurement' @@ -93,6 +102,7 @@ class MeasurementsController < ApplicationController :source_id, routine_attributes: [ + :id, :name, :description ], @@ -118,7 +128,7 @@ class MeasurementsController < ApplicationController end def prepare_readouts - @quantities = @measurement.routine.quantities.includes(:formula) + @quantities = @routine.quantities.includes(:formula) @measurements, @requested_r, @extra_r, @formula_q = @routine.measurements .includes(:routine, :source) .filter(session[:m_filters], @quantities) diff --git a/app/helpers/measurement_routines_helper.rb b/app/helpers/measurement_routines_helper.rb new file mode 100644 index 0000000..16fa09d --- /dev/null +++ b/app/helpers/measurement_routines_helper.rb @@ -0,0 +1,2 @@ +module MeasurementRoutinesHelper +end diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 7ea473e..780a1d1 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -5,6 +5,7 @@ class Measurement < ActiveRecord::Base attrs['name'].blank? } after_destroy { self.routine.destroy if self.routine.measurements.empty? } + has_one :project, through: :routine belongs_to :source, required: false diff --git a/app/views/measurement_routines/_form.html.erb b/app/views/measurement_routines/_form.html.erb new file mode 100644 index 0000000..35ede9e --- /dev/null +++ b/app/views/measurement_routines/_form.html.erb @@ -0,0 +1,7 @@ +
<%= ff.text_field :name, required: true, style: "width: 95%;" %>
+<%= ff.text_area :description, cols: 40, rows: 3, style: "width: 95%;" %>
+ <% end %> +<%= @routine.description %>
+<% end %> diff --git a/app/views/measurement_routines/_show_form.html.erb b/app/views/measurement_routines/_show_form.html.erb new file mode 100644 index 0000000..3a91dd0 --- /dev/null +++ b/app/views/measurement_routines/_show_form.html.erb @@ -0,0 +1,27 @@ ++ <%= ff.select :id, + options_from_collection_for_select(@project.measurement_routines, + :id, :name, @routine.id), + {label: :field_measurement_routine, required: true}, + onchange: "var mr_id = $('#measurement_routine_attributes_id option:selected').val(); + $.ajax({ + url: '#{measurement_routine_path(id: :mr_id)}'.replace('mr_id', mr_id), + dataType: 'script' + }); + return false;" %> + <%= link_to l(:button_edit), '#', + onclick: "var mr_id = $('#measurement_routine_attributes_id option:selected').val(); + $.ajax({ + url: '#{edit_measurement_routine_path(id: :mr_id)}'.replace('mr_id', mr_id), + dataType: 'script' + }); + return false;", + class: 'icon icon-edit' %> +
+<% end %> +