Added setting Formula unit in form and when loading defaults
This commit is contained in:
parent
031b857925
commit
2efdc08931
@ -7,36 +7,41 @@ class BodyTrackersController < BodyTrackingPluginController
|
|||||||
|
|
||||||
def defaults
|
def defaults
|
||||||
# Units
|
# Units
|
||||||
available = @project.units.pluck(:shortname)
|
available_units = @project.units.pluck(:shortname, :id).to_h
|
||||||
defaults = Unit.where(project: nil).map do |u|
|
defaults = Unit.where(project: nil).map do |u|
|
||||||
u.attributes.except('id', 'project_id', 'created_at', 'updated_at')
|
u.attributes.except('id', 'project_id', 'created_at', 'updated_at')
|
||||||
end
|
end
|
||||||
defaults.delete_if { |u| available.include?(u['shortname']) }
|
defaults.delete_if { |u| available_units.has_key?(u['shortname']) }
|
||||||
@project.units.create(defaults)
|
new_units = @project.units.create(defaults).map { |u| [u.shortname, u.id] }.to_h
|
||||||
|
available_units.merge(new_units)
|
||||||
|
|
||||||
new_units = defaults.length
|
flash[:notice] = "Loaded #{new_units.length > 0 ? new_units.length : "no" } new" \
|
||||||
flash[:notice] = "Loaded #{new_units > 0 ? new_units : "no" } new" \
|
" #{'unit'.pluralize(new_units.length)}"
|
||||||
" #{'unit'.pluralize(new_units)}"
|
|
||||||
|
|
||||||
# Quantities
|
# Quantities
|
||||||
available = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
|
available_quantities = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
|
||||||
new_quantities = available.length
|
quantities_count = available_quantities.length
|
||||||
defaults = Quantity.where(project: nil)
|
defaults = Quantity.where(project: nil)
|
||||||
Quantity.each_with_level(defaults) do |q, level|
|
Quantity.each_with_level(defaults) do |q, level|
|
||||||
unless available.has_key?([q.name, q.domain])
|
unless available_quantities.has_key?([q.name, q.domain])
|
||||||
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt',
|
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt',
|
||||||
'created_at', 'updated_at')
|
'created_at', 'updated_at')
|
||||||
attrs['parent'] = q.parent ? available[[q.parent.name, q.parent.domain]] : nil
|
if q.parent
|
||||||
attrs['formula_attributes'] = q.formula ? q.formula.attributes
|
attrs['parent_id'] = available_quantities[[q.parent.name, q.parent.domain]].id
|
||||||
.except('id', 'quantity_id', 'created_at', 'updated_at') : {}
|
end
|
||||||
|
if q.formula
|
||||||
|
attrs['formula_attributes'] = q.formula.attributes
|
||||||
|
.except('id', 'quantity_id', 'unit_id', 'created_at', 'updated_at')
|
||||||
|
attrs['formula_attributes']['unit_id'] = available_units[q.formula.unit.shortname]
|
||||||
|
end
|
||||||
obj = @project.quantities.create(attrs)
|
obj = @project.quantities.create(attrs)
|
||||||
available[[q.name, q.domain]] = obj
|
available_quantities[[q.name, q.domain]] = obj if obj.persisted?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
new_quantities = available.length - new_quantities
|
new_quantities_count = available_quantities.length - quantities_count
|
||||||
flash[:notice] += ", #{new_quantities > 0 ? new_quantities : "no" } new" \
|
flash[:notice] += ", #{new_quantities_count > 0 ? new_quantities_count : "no" } new" \
|
||||||
" #{'quantity'.pluralize(new_quantities)}"
|
" #{'quantity'.pluralize(new_quantities_count)}"
|
||||||
|
|
||||||
ncv = @project.nutrients_column_view
|
ncv = @project.nutrients_column_view
|
||||||
if ncv.quantities.count == 0
|
if ncv.quantities.count == 0
|
||||||
@ -45,16 +50,16 @@ class BodyTrackersController < BodyTrackingPluginController
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
available = @project.sources.pluck(:name)
|
available_sources = @project.sources.pluck(:name, :id).to_h
|
||||||
defaults = Source.where(project: nil).map do |s|
|
defaults = Source.where(project: nil).map do |s|
|
||||||
s.attributes.except('id', 'project_id', 'created_at', 'updated_at')
|
s.attributes.except('id', 'project_id', 'created_at', 'updated_at')
|
||||||
end
|
end
|
||||||
defaults.delete_if { |s| available.include?(s['name']) }
|
defaults.delete_if { |s| available_sources.has_key?(s['name']) }
|
||||||
@project.sources.create(defaults)
|
new_sources = @project.sources.create(defaults).map { |s| [s.name, s.id] }.to_h
|
||||||
|
available_sources.merge(new_sources)
|
||||||
|
|
||||||
new_sources = defaults.length
|
flash[:notice] += " and #{new_sources.length > 0 ? new_sources.length : "no" } new" \
|
||||||
flash[:notice] += " and #{new_sources > 0 ? new_sources : "no" } new" \
|
" #{'source'.pluralize(new_sources.length)}"
|
||||||
" #{'source'.pluralize(new_sources)}"
|
|
||||||
|
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
class QuantitiesController < BodyTrackingPluginController
|
class QuantitiesController < BodyTrackingPluginController
|
||||||
|
helper :body_trackers
|
||||||
|
|
||||||
before_action :init_session_filters
|
before_action :init_session_filters
|
||||||
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter, :parents]
|
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter, :parents]
|
||||||
before_action :find_quantity, only: [:edit, :update, :destroy, :move,
|
before_action :find_quantity, only: [:edit, :update, :destroy, :move,
|
||||||
@ -108,7 +110,8 @@ class QuantitiesController < BodyTrackingPluginController
|
|||||||
formula_attributes:
|
formula_attributes:
|
||||||
[
|
[
|
||||||
:code,
|
:code,
|
||||||
:zero_nil
|
:zero_nil,
|
||||||
|
:unit_id
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -10,4 +10,10 @@ module BodyTrackersHelper
|
|||||||
"#{amount} [#{unitname || '-'}]"
|
"#{amount} [#{unitname || '-'}]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unit_options
|
||||||
|
@project.units.map do |u|
|
||||||
|
[u.shortname, u.id]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -20,12 +20,6 @@ module IngredientsHelper
|
|||||||
options_for_select(options, selected)
|
options_for_select(options, selected)
|
||||||
end
|
end
|
||||||
|
|
||||||
def unit_options
|
|
||||||
@project.units.map do |u|
|
|
||||||
[u.shortname, u.id]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def source_options
|
def source_options
|
||||||
@project.sources.map do |s|
|
@project.sources.map do |s|
|
||||||
[s.name, s.id]
|
[s.name, s.id]
|
||||||
|
@ -25,12 +25,6 @@ module MeasurementsHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unit_options
|
|
||||||
@project.units.map do |u|
|
|
||||||
[u.shortname, u.id]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def source_options
|
def source_options
|
||||||
@project.sources.map do |s|
|
@project.sources.map do |s|
|
||||||
[s.name, s.id]
|
[s.name, s.id]
|
||||||
|
@ -4,7 +4,7 @@ class Formula < ActiveRecord::Base
|
|||||||
attr_reader :parts, :quantities
|
attr_reader :parts, :quantities
|
||||||
|
|
||||||
belongs_to :quantity, inverse_of: :formula, required: true
|
belongs_to :quantity, inverse_of: :formula, required: true
|
||||||
belongs_to :unit, required: true
|
belongs_to :unit
|
||||||
|
|
||||||
validates :code, presence: true
|
validates :code, presence: true
|
||||||
validate do
|
validate do
|
||||||
|
@ -12,8 +12,15 @@
|
|||||||
<p><%= f.text_field :name, size: 25, required: true %></p>
|
<p><%= f.text_field :name, size: 25, required: true %></p>
|
||||||
<p><%= f.text_field :description, style: "width: 100%;" %></p>
|
<p><%= f.text_field :description, style: "width: 100%;" %></p>
|
||||||
<%= f.fields_for :formula do |ff| %>
|
<%= f.fields_for :formula do |ff| %>
|
||||||
<p><%= ff.text_field :code, placeholder: t('.formula_placeholder'),
|
<div>
|
||||||
style: "width: 100%;" %></p>
|
<div style="float: right;">
|
||||||
|
<p style="padding-left: 0;"><%= ff.select :unit_id, unit_options, {label: ''} %></p>
|
||||||
|
</div>
|
||||||
|
<div style="overflow: hidden;width: auto;">
|
||||||
|
<p><%= ff.text_field :code, placeholder: t('.formula_placeholder'),
|
||||||
|
style: "width: 100%" %></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<p><%= ff.check_box :zero_nil, {label: ''} %><%= t('.zero_nil') %></p>
|
<p><%= ff.check_box :zero_nil, {label: ''} %><%= t('.zero_nil') %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -118,7 +118,6 @@ en:
|
|||||||
domain_prompt: 'all'
|
domain_prompt: 'all'
|
||||||
index:
|
index:
|
||||||
heading: 'Quantities'
|
heading: 'Quantities'
|
||||||
heading_new_quantity: 'New quantity'
|
|
||||||
link_new_quantity: 'New quantity'
|
link_new_quantity: 'New quantity'
|
||||||
form:
|
form:
|
||||||
domains:
|
domains:
|
||||||
@ -129,6 +128,8 @@ en:
|
|||||||
formula_placeholder: 'provide if value of quantity has to be computed in terms of
|
formula_placeholder: 'provide if value of quantity has to be computed in terms of
|
||||||
other quantities'
|
other quantities'
|
||||||
zero_nil: 'substitute missing formula values with 0?'
|
zero_nil: 'substitute missing formula values with 0?'
|
||||||
|
new_form:
|
||||||
|
heading_new_quantity: 'New quantity'
|
||||||
units:
|
units:
|
||||||
index:
|
index:
|
||||||
heading: 'Units'
|
heading: 'Units'
|
||||||
|
Reference in New Issue
Block a user