Replaced Quantity 'primary' attr with ColumnView model
This commit is contained in:
parent
ccf26d1830
commit
0c0ca1d286
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,15 +1,15 @@
|
||||
<fieldset id="options" class="collapsible">
|
||||
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
|
||||
<div>
|
||||
<%= 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 %>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td style="width:100%"></td>
|
||||
<td>
|
||||
<%= select_tag 'id', nutrient_column_options %>
|
||||
<%= select_tag 'id', toggle_column_options %>
|
||||
</td>
|
||||
<td>
|
||||
<%= submit_tag l(:button_add) %>
|
||||
@ -27,7 +27,7 @@
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="action" style="width:<%= 100/total_width %>%">
|
||||
<%= 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} %>
|
||||
</td>
|
||||
<% end %>
|
||||
|
@ -1,15 +1,15 @@
|
||||
<fieldset id="options" class="collapsible">
|
||||
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
|
||||
<div>
|
||||
<%= 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 %>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td style="width:100%"></td>
|
||||
<td>
|
||||
<%= select_tag 'id', quantity_toggle_options %>
|
||||
<%= select_tag 'id', toggle_column_options %>
|
||||
</td>
|
||||
<td>
|
||||
<%= submit_tag l(:button_add) %>
|
||||
@ -27,7 +27,7 @@
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="action" style="width:<%= 100/total_width %>%">
|
||||
<%= 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} %>
|
||||
</td>
|
||||
<% end %>
|
||||
|
@ -13,5 +13,4 @@
|
||||
<p><%= f.text_field :description, style: "width: 100%;" %></p>
|
||||
<p><%= f.text_field :formula, placeholder: t('.formula_placeholder'),
|
||||
style: "width: 100%;" %></p>
|
||||
<p><%= f.check_box :primary %></p>
|
||||
</div>
|
||||
|
@ -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
|
||||
%>
|
||||
<tr id="quantity-<%= q.id %>" class="<%= quantity_class %>">
|
||||
<td class="name">
|
||||
<%= link_to '', toggle_quantity_path(q), {
|
||||
remote: true,
|
||||
method: :post,
|
||||
class: "icon #{q.primary ? "icon-fav" : "icon-fav-off"}"
|
||||
}
|
||||
%>
|
||||
<%= q.name %>
|
||||
</td>
|
||||
<td class="name"><%= q.name %></td>
|
||||
<td class="order">
|
||||
<% [:up, :down, :left, :right].each do |direction| %>
|
||||
<%=
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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",
|
||||
|
6
init.rb
6
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user