WIP Targets #edit/#update
NestedUniqueness properly restores collection.target
This commit is contained in:
@@ -5,10 +5,9 @@ class TargetsController < ApplicationController
|
||||
|
||||
include Concerns::Finders
|
||||
|
||||
before_action :find_binding_goal_by_project_id, only: [:edit]
|
||||
before_action :find_project_by_project_id, only: [:subthresholds]
|
||||
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure]
|
||||
before_action :find_goal_by_goal_id, only: [:index, :new, :create]
|
||||
before_action :find_goal_by_goal_id, only: [:index, :new, :create, :edit, :update]
|
||||
before_action :find_goal, only: [:toggle_exposure]
|
||||
before_action :authorize
|
||||
#before_action :set_view_params
|
||||
@@ -39,21 +38,32 @@ class TargetsController < ApplicationController
|
||||
end
|
||||
else
|
||||
@targets = @goal.targets.select(&:changed_for_autosave?)
|
||||
@targets.each { |t| t.thresholds.new unless t.thresholds.present? }
|
||||
.each { |t| t.thresholds.new unless t.thresholds.present? }
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
@targets = @goal.targets.where(effective_from: params[:date]).to_a
|
||||
@targets = @goal.targets.joins(:quantity).where(effective_from: params[:date])
|
||||
.order('quantities.lft' => :asc).to_a
|
||||
@effective_from = @targets.first&.effective_from
|
||||
end
|
||||
|
||||
def update
|
||||
# TODO: DRY same code with #create
|
||||
@goal = @project.goals.find(params[:goal_id]) if params[:goal_id].present?
|
||||
@goal ||= @project.goals.new
|
||||
@goal.attributes = goal_params unless @goal.is_binding?
|
||||
@effective_from = params[:goal].delete(:effective_from)
|
||||
params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
|
||||
|
||||
byebug
|
||||
if @goal.update(targets_params)
|
||||
flash.now[:notice] = t('.success')
|
||||
prepare_targets
|
||||
else
|
||||
@targets = @goal.targets.where(id: targets_params[:targets_attributes].pluck(:id))
|
||||
@targets += @goal.targets.select(&:changed_for_autosave?)
|
||||
.each { |t| t.thresholds.new unless t.thresholds.present? }
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
module TargetsHelper
|
||||
def action_links(d)
|
||||
link_to(l(:button_reapply), reapply_goal_targets_path(@project, d, @view_params),
|
||||
def action_links(date)
|
||||
link_to(l(:button_reapply), reapply_goal_targets_path(@goal, date, @view_params),
|
||||
{remote: true, class: "icon icon-reload"}) +
|
||||
link_to(l(:button_edit), edit_goal_targets_path(@project, d, @view_params),
|
||||
link_to(l(:button_edit), edit_goal_targets_path(@goal, date, @view_params),
|
||||
{remote: true, class: "icon icon-edit"}) +
|
||||
delete_link(target_path(d), {remote: true, data: {}})
|
||||
delete_link(target_path(date), {remote: true, data: {}})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,6 +17,7 @@ class Target < ActiveRecord::Base
|
||||
errors.add(:thresholds, :quantity_mismatch) unless quantities == ancestors
|
||||
end
|
||||
#validates :scope, inclusion: {in: [:ingredient, :meal, :day], if: -> { quantity&.diet? }}
|
||||
# FIXME: does not seem to work
|
||||
validates :effective_from, presence: {if: :is_binding?}, absence: {unless: :is_binding?}
|
||||
|
||||
after_initialize do
|
||||
|
||||
@@ -44,10 +44,10 @@ module Validations::NestedUniqueness
|
||||
def before_validation_nested_uniqueness
|
||||
nested_uniqueness_options.each do |association_name, options|
|
||||
collection = send(association_name)
|
||||
was_loaded = collection.loaded?
|
||||
records = collection.target.select(&:changed?)
|
||||
records = collection.target.dup unless collection.loaded?
|
||||
|
||||
preserved_records = records.reject(&:marked_for_destruction?)
|
||||
preserved_records = records.select(&:changed?).reject(&:marked_for_destruction?)
|
||||
# TODO: do scoping on options[:attributes] and remove options[:scope]
|
||||
scope = options[:scope]&.map { |attr| [attr, preserved_records.map(&attr).uniq] }.to_h
|
||||
|
||||
seen = {}
|
||||
@@ -62,7 +62,7 @@ module Validations::NestedUniqueness
|
||||
end
|
||||
end
|
||||
end
|
||||
unless was_loaded
|
||||
if records
|
||||
collection.proxy_association.reset
|
||||
records.each { |r| collection.proxy_association.add_to_target(r) }
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<%= labelled_form_for @targets,
|
||||
url: project_targets_path(@project, @view_params),
|
||||
method: :patch, remote: true,
|
||||
html: {id: 'edit-target-form', name: 'edit-target-form'} do |f| %>
|
||||
<%= labelled_form_for @goal, url: goal_targets_path(@goal), method: :patch, remote: true,
|
||||
html: {id: 'edit-target-form', name: 'edit-target-form'} do |goal_f| %>
|
||||
|
||||
<%= render partial: 'targets/form', locals: {f: f} %>
|
||||
<div class="box tabular">
|
||||
<%= render partial: 'targets/form', locals: {goal_f: goal_f} %>
|
||||
</div>
|
||||
|
||||
<div class="tabular">
|
||||
<p>
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= goal_f.fields_for :targets, @targets, child_index: '' do |target_f| %>
|
||||
<%= goal_f.fields_for :targets, @targets, index: '', child_index: '' do |target_f| %>
|
||||
<%= error_messages_for target_f.object %>
|
||||
|
||||
<p class="target">
|
||||
<%= target_f.hidden_field :id %>
|
||||
<%= target_f.hidden_field :_destroy %>
|
||||
<em class="info"><%= t ".choose_quantity" %></em>
|
||||
<%= target_f.select :quantity_id, quantity_options,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<% target_f.object.thresholds.new unless last_quantity.leaf? %>
|
||||
<%= target_f.fields_for :thresholds, child_index: '' do |threshold_f| %>
|
||||
<%= target_f.fields_for :thresholds, index: '', child_index: '' do |threshold_f| %>
|
||||
<% threshold_q = threshold_f.object.quantity %>
|
||||
<% parent_id = threshold_q.nil? ? last_quantity.id : threshold_q.parent_id %>
|
||||
<%= threshold_f.collection_select :quantity_id,
|
||||
@@ -16,6 +16,7 @@
|
||||
}
|
||||
});
|
||||
return false;"} %>
|
||||
<%# TODO: update above ajax with -1/no change/+1 logic based on copying existing threshold and proper ID/destroy handling %>
|
||||
<%# {autocomplete: 'off',
|
||||
data: {remote: true, url: subthresholds_project_targets_path(parent_id: parent_id)}} %>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user