1
0

Added setting Formula unit in form and when loading defaults

This commit is contained in:
cryptogopher 2020-03-25 21:40:24 +01:00
parent 031b857925
commit 2efdc08931
8 changed files with 49 additions and 39 deletions

View File

@ -7,36 +7,41 @@ class BodyTrackersController < BodyTrackingPluginController
def defaults
# Units
available = @project.units.pluck(:shortname)
available_units = @project.units.pluck(:shortname, :id).to_h
defaults = Unit.where(project: nil).map do |u|
u.attributes.except('id', 'project_id', 'created_at', 'updated_at')
end
defaults.delete_if { |u| available.include?(u['shortname']) }
@project.units.create(defaults)
defaults.delete_if { |u| available_units.has_key?(u['shortname']) }
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 > 0 ? new_units : "no" } new" \
" #{'unit'.pluralize(new_units)}"
flash[:notice] = "Loaded #{new_units.length > 0 ? new_units.length : "no" } new" \
" #{'unit'.pluralize(new_units.length)}"
# Quantities
available = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
new_quantities = available.length
available_quantities = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
quantities_count = available_quantities.length
defaults = Quantity.where(project: nil)
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',
'created_at', 'updated_at')
attrs['parent'] = q.parent ? available[[q.parent.name, q.parent.domain]] : nil
attrs['formula_attributes'] = q.formula ? q.formula.attributes
.except('id', 'quantity_id', 'created_at', 'updated_at') : {}
if q.parent
attrs['parent_id'] = available_quantities[[q.parent.name, q.parent.domain]].id
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)
available[[q.name, q.domain]] = obj
available_quantities[[q.name, q.domain]] = obj if obj.persisted?
end
end
new_quantities = available.length - new_quantities
flash[:notice] += ", #{new_quantities > 0 ? new_quantities : "no" } new" \
" #{'quantity'.pluralize(new_quantities)}"
new_quantities_count = available_quantities.length - quantities_count
flash[:notice] += ", #{new_quantities_count > 0 ? new_quantities_count : "no" } new" \
" #{'quantity'.pluralize(new_quantities_count)}"
ncv = @project.nutrients_column_view
if ncv.quantities.count == 0
@ -45,16 +50,16 @@ class BodyTrackersController < BodyTrackingPluginController
end
# Sources
available = @project.sources.pluck(:name)
available_sources = @project.sources.pluck(:name, :id).to_h
defaults = Source.where(project: nil).map do |s|
s.attributes.except('id', 'project_id', 'created_at', 'updated_at')
end
defaults.delete_if { |s| available.include?(s['name']) }
@project.sources.create(defaults)
defaults.delete_if { |s| available_sources.has_key?(s['name']) }
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 > 0 ? new_sources : "no" } new" \
" #{'source'.pluralize(new_sources)}"
flash[:notice] += " and #{new_sources.length > 0 ? new_sources.length : "no" } new" \
" #{'source'.pluralize(new_sources.length)}"
redirect_to :back
end

View File

@ -1,4 +1,6 @@
class QuantitiesController < BodyTrackingPluginController
helper :body_trackers
before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :new, :create, :filter, :parents]
before_action :find_quantity, only: [:edit, :update, :destroy, :move,
@ -108,7 +110,8 @@ class QuantitiesController < BodyTrackingPluginController
formula_attributes:
[
:code,
:zero_nil
:zero_nil,
:unit_id
]
)
end

View File

@ -10,4 +10,10 @@ module BodyTrackersHelper
"#{amount} [#{unitname || '-'}]"
end
end
def unit_options
@project.units.map do |u|
[u.shortname, u.id]
end
end
end

View File

@ -20,12 +20,6 @@ module IngredientsHelper
options_for_select(options, selected)
end
def unit_options
@project.units.map do |u|
[u.shortname, u.id]
end
end
def source_options
@project.sources.map do |s|
[s.name, s.id]

View File

@ -25,12 +25,6 @@ module MeasurementsHelper
end
end
def unit_options
@project.units.map do |u|
[u.shortname, u.id]
end
end
def source_options
@project.sources.map do |s|
[s.name, s.id]

View File

@ -4,7 +4,7 @@ class Formula < ActiveRecord::Base
attr_reader :parts, :quantities
belongs_to :quantity, inverse_of: :formula, required: true
belongs_to :unit, required: true
belongs_to :unit
validates :code, presence: true
validate do

View File

@ -12,8 +12,15 @@
<p><%= f.text_field :name, size: 25, required: true %></p>
<p><%= f.text_field :description, style: "width: 100%;" %></p>
<%= f.fields_for :formula do |ff| %>
<p><%= ff.text_field :code, placeholder: t('.formula_placeholder'),
style: "width: 100%;" %></p>
<div>
<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>
<% end %>
</div>

View File

@ -118,7 +118,6 @@ en:
domain_prompt: 'all'
index:
heading: 'Quantities'
heading_new_quantity: 'New quantity'
link_new_quantity: 'New quantity'
form:
domains:
@ -129,6 +128,8 @@ en:
formula_placeholder: 'provide if value of quantity has to be computed in terms of
other quantities'
zero_nil: 'substitute missing formula values with 0?'
new_form:
heading_new_quantity: 'New quantity'
units:
index:
heading: 'Units'