1
0

Test pass: test_edit_binding_target

This commit is contained in:
cryptogopher 2021-04-24 15:36:22 +02:00
parent bea0a8371d
commit 5ff7d8395a
5 changed files with 55 additions and 44 deletions

View File

@ -5,8 +5,7 @@ class TargetsController < ApplicationController
include Concerns::Finders
before_action :find_project_by_project_id, only: [:subthresholds]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure, :subthresholds]
before_action :find_goal_by_goal_id, only: [:index, :new, :create, :edit, :update]
before_action :find_goal, only: [:toggle_exposure]
before_action :authorize
@ -54,7 +53,6 @@ class TargetsController < ApplicationController
@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
@ -78,15 +76,7 @@ class TargetsController < ApplicationController
end
def subthresholds
@target = @project.goals.binding.targets.new
quantity = @project.quantities.target.find_by(id: params[:quantity_id])
if quantity.nil?
@last_quantity = @project.quantities.target.find(params[:parent_id])
@target.thresholds.clear
else
@last_quantity = quantity
@target.thresholds.first.quantity = quantity
end
@quantities = @quantity.children
end
private

View File

@ -31,35 +31,68 @@
<%= javascript_tag do %>
function showQuantityPath(event) {
$(event.target).prevAll('em').text($('option:selected', event.target)
.attr('data-path'));
$(event.target).prevAll('em')
.text($('option:selected', event.target).attr('data-path'));
}
$(document).ajaxComplete(function() {
$('p.target select:first-child[id$=__quantity_id]').trigger(jQuery.Event('change'));
$('p.target').find('select:first').change();
})
function newTarget(event) {
var form = $(event.target).closest('form');
var row = form.find('p.target:visible:last');
var new_row = row.clone().insertAfter(row);
new_row.find('em').text('<%= t ".choose_quantity" %>');
new_row.find('input, select:first').val('');
new_row.find('label:first').hide();
var target = form.find('p.target:visible:last');
var new_target = target.clone().insertAfter(target);
new_target.find('em').text('<%= t ".choose_quantity" %>');
new_target.find('label:first').hide();
new_target.find('input, select:first').val('');
new_target.find('input, select.threshold:first').change();
form.find('p.target:visible a.icon-del').show();
}
function deleteTarget(event) {
var form = $(event.target).closest('form');
var row = $(event.target).closest('p.target');
if (row.find('input[id$=__id]').val()) {
row.hide();
row.find('input[id$=__destroy]').val('1');
var target = $(event.target).closest('p.target');
if (target.find('input[id$=__id]').val()) {
target.hide();
target.find('input[id$=__destroy]').val('1');
} else {
row.remove();
target.remove();
}
form.find('p.target:visible:first label:first').show();
if (form.find('p.target:visible').length <= 1) {
form.find('p.target:visible a.icon-del').hide();
}
}
function subthresholds(event) {
$(event.target).nextUntil('a').hide();
$(event.target).nextAll('input[id$=__destroy]').val('true');
if ($(event.target).val() != "") {
var threshold_value = $(event.target).nextUntil('select.threshold, a');
if (threshold_value.length == 0) {
template = $(event.target).closest('p.target').find('select.threshold:first')
.nextUntil('select.threshold, a').not('input[type=hidden]');
threshold_value = template.clone().insertAfter($(event.target)).before(" ");
}
threshold_value.show();
threshold_value.filter('input[id$=__destroy]').val('false');
$.ajax({
url: '<%= subthresholds_path %>',
data: {'quantity_id' : $(event.target).val()},
dataType: 'html',
success: function(data) {
if (data) {
var threshold_quantity = $(event.target).nextAll('select.threshold:first');
if (threshold_quantity.length == 0) {
threshold_quantity = $(event.target).clone()
.insertAfter(threshold_value.last()).before(" ");
}
threshold_quantity.show().empty().append(data);
}
}
});
}
}
<% end %>

View File

@ -6,21 +6,11 @@
@project.quantities.target.children_of(parent_id),
:id, :name,
{include_blank: parent_id.nil? ? false : '.', required: true, no_label: true},
{autocomplete: 'off',
onchange: "$.ajax({
url: '#{project_subthresholds_path(@project, parent_id: parent_id)}',
data: 'quantity_id=' + $(this).val(),
dataType: 'html',
success: function(data) {
$(event.target).nextAll().addBack().last().after(data).end().remove();
}
});
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)}} %>
{autocomplete: 'off', onchange: 'subthresholds(event); return false;',
class: 'threshold'} %>
<% unless threshold_q.nil? %>
<%= threshold_f.hidden_field :id %>
<%= threshold_f.hidden_field :_destroy %>
<%= threshold_f.number_field :value, {size: 8, step: :any, no_label: true} %>
<%= threshold_f.collection_select :unit_id, @project.units, :id, :shortname,

View File

@ -1,4 +1,2 @@
<%= fields_for 'goal[targets_attributes][]', @target do |target_f| %>
<%= render partial: 'targets/thresholds_form',
locals: {target_f: target_f, last_quantity: @last_quantity} %>
<% end %>
<%= options_for_select [['.', nil]] unless @quantities.empty? -%>
<%= options_from_collection_for_select(@quantities, :id, :name) -%>

View File

@ -19,8 +19,6 @@ resources :projects, shallow: true do
end
end
end
get 'subthresholds/(:parent_id)', controller: :targets, action: :subthresholds,
as: :subthresholds
resources :ingredients, only: [] do
member do
post 'adjust/:adjustment', controller: :meals, action: :adjust, as: :adjust
@ -76,3 +74,5 @@ resources :projects, shallow: true do
end
resources :units, only: [:index, :create, :destroy]
end
get 'subthresholds', controller: :targets, action: :subthresholds, as: :subthresholds