diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 85cf9b9..cc980b2 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -6,7 +6,7 @@ class IngredientsController < ApplicationController before_action :init_session_filters before_action :find_project_by_project_id, only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients] - before_action :find_quantity, only: [:toggle_nutrient_column] + before_action :find_quantity_by_quantity_id, only: [:toggle_column] before_action :find_ingredient, only: [:destroy, :toggle] before_action :authorize @@ -25,8 +25,8 @@ class IngredientsController < ApplicationController prepare_nutrients end - def toggle_nutrient_column - @quantity.toggle_primary! + def toggle_column + @project.nutrients_column_view.toggle_column!(@quantity) prepare_nutrients end @@ -207,7 +207,7 @@ class IngredientsController < ApplicationController end def prepare_nutrients - @quantities = @project.quantities.diet.where(primary: true) + @quantities = @project.nutrients_column_view.quantities ingredients, requested_n, extra_n, @formula_q = @project.ingredients .filter(@project, session[:i_filters], @quantities) diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 56c4e00..ab3a76e 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -3,8 +3,9 @@ class MeasurementsController < ApplicationController before_action :init_session_filters before_action :find_project_by_project_id, only: [:index, :new, :create] - before_action :find_quantity, only: [:toggle_quantity] - before_action :find_measurement, only: [:edit, :update, :destroy, :retake, :readouts] + before_action :find_quantity_by_quantity_id, only: [:toggle_column] + before_action :find_measurement, + only: [:edit, :update, :destroy, :retake, :readouts, :toggle_column] before_action :authorize def index @@ -61,8 +62,8 @@ class MeasurementsController < ApplicationController prepare_readouts end - def toggle_quantity - @quantity.toggle_primary! + def toggle_column + @measurement.column_view.toggle_column!(@quantity) prepare_readouts end @@ -103,7 +104,7 @@ class MeasurementsController < ApplicationController end def prepare_readouts - @quantities = @project.quantities.measurement.where(primary: true) + @quantities = @measurement.column_view.quantities @measurements, @requested_r, @extra_r, @formula_q = @project.measurements .includes(:source) .filter(session[:m_filters], @quantities) diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 3a11fb4..ea0bed8 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -3,7 +3,7 @@ class QuantitiesController < ApplicationController before_action :init_session_filters before_action :find_project_by_project_id, only: [:index, :parents, :create, :filter] - before_action :find_quantity, only: [:edit, :update, :destroy, :toggle, :move] + before_action :find_quantity, only: [:edit, :update, :destroy, :move] before_action :authorize def index @@ -55,11 +55,6 @@ class QuantitiesController < ApplicationController render :index end - def toggle - @quantity.toggle_primary! - prepare_quantities - end - def move direction = params[:direction].to_sym case direction @@ -90,8 +85,7 @@ class QuantitiesController < ApplicationController :parent_id, :name, :description, - :formula, - :primary + :formula ) end diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index b9b0933..53eaf9b 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -5,10 +5,11 @@ module IngredientsHelper end end - def nutrient_column_options + def toggle_column_options disabled = [] + enabled_columns = @project.nutrients_column_view.quantities options = nested_set_options(@project.quantities.diet) do |q| - disabled << q.id if q.primary + disabled << q.id if enabled_columns.include?(q) raw("#{' ' * q.level}#{q.name}") end options_for_select(options, disabled: disabled) diff --git a/app/helpers/measurements_helper.rb b/app/helpers/measurements_helper.rb index a3bfda6..10536df 100644 --- a/app/helpers/measurements_helper.rb +++ b/app/helpers/measurements_helper.rb @@ -12,10 +12,11 @@ module MeasurementsHelper amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]" end - def quantity_toggle_options + def toggle_column_options disabled = [] + enabled_columns = @measurement.column_view.quantities options = nested_set_options(@project.quantities.measurement) do |q| - disabled << q.id if q.primary + disabled << q.id if enabled_columns.include?(q) raw("#{' ' * q.level}#{q.name}") end options_for_select(options, disabled: disabled) diff --git a/app/models/column_view.rb b/app/models/column_view.rb index e45a8ad..2c0715b 100644 --- a/app/models/column_view.rb +++ b/app/models/column_view.rb @@ -1,2 +1,17 @@ class ColumnView < ActiveRecord::Base + enum domain: Quantity.domains + + belongs_to :project, required: true + has_and_belongs_to_many :quantities + + validates :name, presence: true, uniqueness: {scope: :domain} + validates :domain, inclusion: {in: domains.keys} + + # TODO: enforce column_view - quantity 'domain' identity + def toggle_column!(q) + column = self.quantities.find(q.id) + self.quantites.destroy(column) + rescue ActiveRecord::RecordNotFound + self.quantities.create!(quantity: q) + end end diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 2501968..d8ccaf4 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -28,6 +28,33 @@ class Measurement < ActiveRecord::Base end end + # Copy/rename ColumnView on Measurement rename + after_save do + old_column_view = self.project.column_views + .find_by(name: self.name_was, domain: :measurement) + return unless old_column_view + + if self.project.measurements.exists?(name: self.name_was) + return unless old_column_view.quantities.exist? + self.column_view.quantities.create(old_column_view.quantities) + self.column_view.save! + else + old_column_view.name = self.name + old_column_view.save! + end + end, if: :name_changed? + + # Destroy ColumnView after last Measurement destruction + after_destroy do + unless self.project.measurements.exists?(name: self.name) + self.column_view.destroy! + end + end + + def column_view + self.project.column_views.find_or_create_by(name: self.name, domain: :measurement) + end + def toggle_hidden! self.toggle!(:hidden) end diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 17ceb53..49f6d00 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -15,6 +15,7 @@ class Quantity < ActiveRecord::Base acts_as_nested_set dependent: :destroy, scope: :project belongs_to :project, required: false + has_and_belongs_to_many :column_views validates :name, presence: true, uniqueness: {scope: :project_id} validates :domain, inclusion: {in: domains.keys} @@ -26,7 +27,6 @@ class Quantity < ActiveRecord::Base after_initialize do if new_record? self.domain ||= :diet - self.primary = false if self.primary.nil? end end @@ -46,10 +46,6 @@ class Quantity < ActiveRecord::Base end end - def toggle_primary! - self.toggle!(:primary) - end - def formula_quantities Formula.new(self.project, self.formula).get_quantities end diff --git a/app/views/ingredients/_options.html.erb b/app/views/ingredients/_options.html.erb index 65a0e62..1bb441d 100644 --- a/app/views/ingredients/_options.html.erb +++ b/app/views/ingredients/_options.html.erb @@ -1,15 +1,15 @@