WIP: Targets configurable with Quantities
This commit is contained in:
@@ -28,19 +28,19 @@ class BodyTrackersController < ApplicationController
|
||||
quantities_count = available_quantities.length
|
||||
defaults = Quantity.defaults
|
||||
Quantity.each_with_path(defaults) do |q, path|
|
||||
unless available_quantities.has_key?(path)
|
||||
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt',
|
||||
'created_at', 'updated_at')
|
||||
if q.parent
|
||||
attrs['parent'] = available_quantities[path.rpartition('::').first]
|
||||
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
|
||||
available_quantities[path] = @project.quantities.build(attrs)
|
||||
next if available_quantities.has_key?(path)
|
||||
|
||||
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt', 'depth',
|
||||
'created_at', 'updated_at')
|
||||
if q.parent
|
||||
attrs['parent'] = available_quantities[path.rpartition('::').first]
|
||||
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
|
||||
available_quantities[path] = @project.quantities.build(attrs)
|
||||
end
|
||||
Quantity.transaction do
|
||||
failed_objects += available_quantities.values.reject { |o| o.persisted? || o.save }
|
||||
|
||||
@@ -6,6 +6,7 @@ class TargetsController < ApplicationController
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :find_binding_goal_by_project_id, only: [:index, :new, :edit]
|
||||
before_action :find_project, only: [:subthresholds]
|
||||
before_action :find_project_by_project_id, only: [:create]
|
||||
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure]
|
||||
#, if: ->{ params[:project_id].present? }
|
||||
@@ -21,8 +22,9 @@ class TargetsController < ApplicationController
|
||||
|
||||
def new
|
||||
target = @goal.targets.new
|
||||
target.arity.times { target.thresholds.new }
|
||||
target.thresholds.new(quantity: Quantity.target.roots.last)
|
||||
@targets = [target]
|
||||
@effective_from = target.effective_from
|
||||
end
|
||||
|
||||
def create
|
||||
@@ -75,6 +77,15 @@ class TargetsController < ApplicationController
|
||||
prepare_targets
|
||||
end
|
||||
|
||||
def subthresholds
|
||||
quantity_id = params[:goal][:targets_attributes]
|
||||
.last[:thresholds_attributes][:quantity_id]
|
||||
return if quantity_id.blank?
|
||||
|
||||
quantity = @project.quantities.find(quantity_id)
|
||||
@threshold = Threshold.new(quantity: quantity)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def goal_params
|
||||
|
||||
Reference in New Issue
Block a user