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 @@
<%= l(:label_options) %>
- <%= form_tag toggle_nutrient_column_project_ingredients_path(@project), - id: 'nutrient-column-add-form', name: 'nutrient-column-add-form', + <%= form_tag toggle_column_project_ingredients_path(@project), + id: 'toggle-column-form', name: 'toggle-column-form', method: :post, remote: true do %> <% end %> diff --git a/app/views/measurements/_options.html.erb b/app/views/measurements/_options.html.erb index 37fbbb8..bd1c9b6 100644 --- a/app/views/measurements/_options.html.erb +++ b/app/views/measurements/_options.html.erb @@ -1,15 +1,15 @@
<%= l(:label_options) %>
- <%= form_tag toggle_quantity_project_measurements_path(@project), - id: 'quantity-toggle-form', name: 'quantity-toggle-form', + <%= form_tag toggle_column_measurement_path(@measurement), + id: 'toggle-column-form', name: 'toggle-column-form', method: :post, remote: true do %>
- <%= select_tag 'id', nutrient_column_options %> + <%= select_tag 'id', toggle_column_options %> <%= submit_tag l(:button_add) %> @@ -27,7 +27,7 @@ <% @quantities.each do |q| %> <%= link_to l(:button_hide), - toggle_nutrient_column_project_ingredients_path(@project, id: q.id), + toggle_column_project_ingredients_path(@project, quantity_id: q.id), {class: "icon icon-close", method: :post, remote: true} %>
<% end %> diff --git a/app/views/quantities/_form.html.erb b/app/views/quantities/_form.html.erb index 1c8ac3b..772643f 100644 --- a/app/views/quantities/_form.html.erb +++ b/app/views/quantities/_form.html.erb @@ -13,5 +13,4 @@

<%= f.text_field :description, style: "width: 100%;" %>

<%= f.text_field :formula, placeholder: t('.formula_placeholder'), style: "width: 100%;" %>

-

<%= f.check_box :primary %>

diff --git a/app/views/quantities/_index.html.erb b/app/views/quantities/_index.html.erb index 67f4f0e..d8ed755 100644 --- a/app/views/quantities/_index.html.erb +++ b/app/views/quantities/_index.html.erb @@ -19,18 +19,9 @@ next if q.new_record? quantity_class = "quantity" quantity_class += " project idnt idnt-#{level+1}" - quantity_class += " primary" if q.primary %> - +
- <%= select_tag 'id', quantity_toggle_options %> + <%= select_tag 'id', toggle_column_options %> <%= submit_tag l(:button_add) %> @@ -27,7 +27,7 @@ <% @quantities.each do |q| %> <%= link_to l(:button_hide), - toggle_quantity_project_measurements_path(@project, id: q.id), + toggle_column_measurement_path(@measurement, quantity_id: q.id), {class: "icon icon-close", method: :post, remote: true} %>
- <%= link_to '', toggle_quantity_path(q), { - remote: true, - method: :post, - class: "icon #{q.primary ? "icon-fav" : "icon-fav-off"}" - } - %> - <%= q.name %> - <%= q.name %> <% [:up, :down, :left, :right].each do |direction| %> <%= diff --git a/config/locales/en.yml b/config/locales/en.yml index bb63375..f4377da 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,9 +13,7 @@ en: field_domain: 'Domain' field_parent_quantity: 'Parent' field_formula: 'Formula' - field_primary: 'Primary' field_shortname: 'Short name' - button_primary: 'Primary' button_retake: 'Retake' activerecord: errors: diff --git a/config/routes.rb b/config/routes.rb index 5eedae4..3973638 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,9 +9,7 @@ resources :projects, shallow: true do member do get 'retake' get 'readouts' - end - collection do - post 'toggle_quantity' + post 'toggle_column' end end resources :ingredients, only: [:index, :create, :destroy] do @@ -20,14 +18,13 @@ resources :projects, shallow: true do get 'nutrients' get 'filter' get 'filter_nutrients' - post 'toggle_nutrient_column' post 'import' + post 'toggle_column' end end resources :sources, only: [:index, :create, :destroy] resources :quantities, only: [:index, :create, :edit, :update, :destroy] do member do - post 'toggle' post 'move/:direction', to: 'quantities#move', as: :move end collection do diff --git a/db/migrate/001_create_schema.rb b/db/migrate/001_create_schema.rb index e706281..096f65c 100644 --- a/db/migrate/001_create_schema.rb +++ b/db/migrate/001_create_schema.rb @@ -13,7 +13,6 @@ class CreateSchema < ActiveRecord::Migration t.string :name t.string :formula t.string :description - t.boolean :primary # fields for awesome_nested_set t.references :parent t.integer :lft, null: false, index: true diff --git a/db/migrate/002_load_defaults.rb b/db/migrate/002_load_defaults.rb index c97cc02..78d7eb3 100644 --- a/db/migrate/002_load_defaults.rb +++ b/db/migrate/002_load_defaults.rb @@ -12,13 +12,13 @@ class LoadDefaults < ActiveRecord::Migration # https://www.fsai.ie/uploadedFiles/Consol_Reg1169_2011.pdf # https://www.fsai.ie/legislation/food_legislation/food_information_fic/nutrition_labelling.html e1 = Quantity.create project: nil, domain: :diet, parent: nil, name: "Energy", - description: "Total energy", primary: true + description: "Total energy" p1 = Quantity.create project: nil, domain: :diet, parent: nil, name: "Proteins", - description: "Total amount of proteins", primary: true + description: "Total amount of proteins" f1 = Quantity.create project: nil, domain: :diet, parent: nil, name: "Fats", - description: "Total lipids, including phospholipids", primary: true + description: "Total lipids, including phospholipids" f2 = Quantity.create project: nil, domain: :diet, parent: f1, name: "Fatty acids", description: "" f3 = Quantity.create project: nil, domain: :diet, parent: f2, name: "Saturated", @@ -45,7 +45,7 @@ class LoadDefaults < ActiveRecord::Migration description: "Docosahexaenoic acid" c1 = Quantity.create project: nil, domain: :diet, parent: nil, name: "Carbohydrates", - description: "Total amount of carbohydrates", primary: true + description: "Total amount of carbohydrates" c2 = Quantity.create project: nil, domain: :diet, parent: c1, name: "Digestible", description: "" c3 = Quantity.create project: nil, domain: :diet, parent: c2, name: "Sugars", @@ -137,7 +137,7 @@ class LoadDefaults < ActiveRecord::Migration # Calculated quantities go at the and to make sure dependencies exist e2 = Quantity.create project: nil, domain: :diet, parent: e1, name: "Calculated", - description: "Total energy calculated from macronutrients", primary: true, + description: "Total energy calculated from macronutrients", formula: "4*Proteins + 9*Fats + 4*Carbohydrates" Source.create project: nil, name: "nutrition label", diff --git a/init.rb b/init.rb index 3a4bcc9..a661d12 100644 --- a/init.rb +++ b/init.rb @@ -24,10 +24,10 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { body_trackers: [:defaults], - measurements: [:new, :create, :edit, :update, :destroy, :retake, :toggle_quantity], - ingredients: [:create, :destroy, :toggle, :import, :toggle_nutrient_column], + measurements: [:new, :create, :edit, :update, :destroy, :retake, :toggle_column], + ingredients: [:create, :destroy, :toggle, :import, :toggle_column], sources: [:create, :destroy], - quantities: [:create, :edit, :update, :destroy, :toggle, :move], + quantities: [:create, :edit, :update, :destroy, :move], units: [:create, :destroy], }, require: :loggedin end diff --git a/lib/body_tracking/application_controller_patch.rb b/lib/body_tracking/application_controller_patch.rb index 853a0e8..f3d9518 100644 --- a/lib/body_tracking/application_controller_patch.rb +++ b/lib/body_tracking/application_controller_patch.rb @@ -3,14 +3,16 @@ module BodyTracking ApplicationController.class_eval do private - # :find_* methods are called before :authorize, - # @project is required for :authorize to succeed - def find_quantity - @quantity = Quantity.find(params[:id]) + 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(params[:quantity_id]) + end end end end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index bac0666..452f37d 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -5,8 +5,13 @@ module BodyTracking has_many :ingredients, -> { order "name" }, dependent: :destroy has_many :sources, dependent: :destroy + has_many :column_views, dependent: :destroy has_many :quantities, -> { order "lft" }, dependent: :destroy has_many :units, dependent: :destroy + + def nutrients_column_view + self.column_views.find_or_create_by(name: 'Nutrients', domain: :diet) + end end end end