diff --git a/app/controllers/body_trackers_controller.rb b/app/controllers/body_trackers_controller.rb index 6f0ea4d..61e9ff6 100644 --- a/app/controllers/body_trackers_controller.rb +++ b/app/controllers/body_trackers_controller.rb @@ -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 diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index d4d3de5..491fe53 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -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 diff --git a/app/helpers/body_trackers_helper.rb b/app/helpers/body_trackers_helper.rb index 8b41cf8..6a6b047 100644 --- a/app/helpers/body_trackers_helper.rb +++ b/app/helpers/body_trackers_helper.rb @@ -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 diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index 4508a8b..8107d62 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -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] diff --git a/app/helpers/measurements_helper.rb b/app/helpers/measurements_helper.rb index 5dc740d..bbe6530 100644 --- a/app/helpers/measurements_helper.rb +++ b/app/helpers/measurements_helper.rb @@ -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] diff --git a/app/models/formula.rb b/app/models/formula.rb index 00c04f0..ce8122e 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -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 diff --git a/app/views/quantities/_form.html.erb b/app/views/quantities/_form.html.erb index c46498e..eb48e5e 100644 --- a/app/views/quantities/_form.html.erb +++ b/app/views/quantities/_form.html.erb @@ -12,8 +12,15 @@
<%= f.text_field :name, size: 25, required: true %>
<%= f.text_field :description, style: "width: 100%;" %>
<%= f.fields_for :formula do |ff| %> -<%= ff.text_field :code, placeholder: t('.formula_placeholder'), - style: "width: 100%;" %>
+<%= ff.select :unit_id, unit_options, {label: ''} %>
+<%= ff.check_box :zero_nil, {label: ''} %><%= t('.zero_nil') %>
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b9cb516..2f0bb66 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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'