1
0

WIP Targets #edit/#update

NestedUniqueness properly restores collection.target
This commit is contained in:
cryptogopher 2021-04-23 19:19:44 +02:00
parent 37e3b98788
commit bea0a8371d
9 changed files with 93 additions and 40 deletions

View File

@ -5,10 +5,9 @@ class TargetsController < ApplicationController
include Concerns::Finders 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_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]
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 :find_goal, only: [:toggle_exposure]
before_action :authorize before_action :authorize
#before_action :set_view_params #before_action :set_view_params
@ -39,21 +38,32 @@ class TargetsController < ApplicationController
end end
else else
@targets = @goal.targets.select(&:changed_for_autosave?) @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 render :new
end end
end end
def edit 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 @effective_from = @targets.first&.effective_from
end end
def update def update
# TODO: DRY same code with #create # TODO: DRY same code with #create
@goal = @project.goals.find(params[:goal_id]) if params[:goal_id].present? @effective_from = params[:goal].delete(:effective_from)
@goal ||= @project.goals.new params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
@goal.attributes = goal_params unless @goal.is_binding?
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 end
def destroy def destroy

View File

@ -1,9 +1,9 @@
module TargetsHelper module TargetsHelper
def action_links(d) def action_links(date)
link_to(l(:button_reapply), reapply_goal_targets_path(@project, d, @view_params), link_to(l(:button_reapply), reapply_goal_targets_path(@goal, date, @view_params),
{remote: true, class: "icon icon-reload"}) + {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"}) + {remote: true, class: "icon icon-edit"}) +
delete_link(target_path(d), {remote: true, data: {}}) delete_link(target_path(date), {remote: true, data: {}})
end end
end end

View File

@ -17,6 +17,7 @@ class Target < ActiveRecord::Base
errors.add(:thresholds, :quantity_mismatch) unless quantities == ancestors errors.add(:thresholds, :quantity_mismatch) unless quantities == ancestors
end end
#validates :scope, inclusion: {in: [:ingredient, :meal, :day], if: -> { quantity&.diet? }} #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?} validates :effective_from, presence: {if: :is_binding?}, absence: {unless: :is_binding?}
after_initialize do after_initialize do

View File

@ -44,10 +44,10 @@ module Validations::NestedUniqueness
def before_validation_nested_uniqueness def before_validation_nested_uniqueness
nested_uniqueness_options.each do |association_name, options| nested_uniqueness_options.each do |association_name, options|
collection = send(association_name) collection = send(association_name)
was_loaded = collection.loaded? records = collection.target.dup unless collection.loaded?
records = collection.target.select(&:changed?)
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 scope = options[:scope]&.map { |attr| [attr, preserved_records.map(&attr).uniq] }.to_h
seen = {} seen = {}
@ -62,7 +62,7 @@ module Validations::NestedUniqueness
end end
end end
end end
unless was_loaded if records
collection.proxy_association.reset collection.proxy_association.reset
records.each { |r| collection.proxy_association.add_to_target(r) } records.each { |r| collection.proxy_association.add_to_target(r) }
end end

View File

@ -1,9 +1,9 @@
<%= labelled_form_for @targets, <%= labelled_form_for @goal, url: goal_targets_path(@goal), method: :patch, remote: true,
url: project_targets_path(@project, @view_params), html: {id: 'edit-target-form', name: 'edit-target-form'} do |goal_f| %>
method: :patch, remote: true,
html: {id: 'edit-target-form', name: 'edit-target-form'} do |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"> <div class="tabular">
<p> <p>

View File

@ -4,10 +4,11 @@
</p> </p>
<% end %> <% 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 %> <%= error_messages_for target_f.object %>
<p class="target"> <p class="target">
<%= target_f.hidden_field :id %>
<%= target_f.hidden_field :_destroy %> <%= target_f.hidden_field :_destroy %>
<em class="info"><%= t ".choose_quantity" %></em> <em class="info"><%= t ".choose_quantity" %></em>
<%= target_f.select :quantity_id, quantity_options, <%= target_f.select :quantity_id, quantity_options,

View File

@ -1,5 +1,5 @@
<% target_f.object.thresholds.new unless last_quantity.leaf? %> <% 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 %> <% threshold_q = threshold_f.object.quantity %>
<% parent_id = threshold_q.nil? ? last_quantity.id : threshold_q.parent_id %> <% parent_id = threshold_q.nil? ? last_quantity.id : threshold_q.parent_id %>
<%= threshold_f.collection_select :quantity_id, <%= threshold_f.collection_select :quantity_id,
@ -16,6 +16,7 @@
} }
}); });
return false;"} %> return false;"} %>
<%# TODO: update above ajax with -1/no change/+1 logic based on copying existing threshold and proper ID/destroy handling %>
<%# {autocomplete: 'off', <%# {autocomplete: 'off',
data: {remote: true, url: subthresholds_project_targets_path(parent_id: parent_id)}} %> data: {remote: true, url: subthresholds_project_targets_path(parent_id: parent_id)}} %>

View File

@ -11,9 +11,10 @@ resources :projects, shallow: true do
member do member do
post 'toggle_exposure', controller: :targets post 'toggle_exposure', controller: :targets
end end
resources :targets, shallow: true, except: [:show, :edit, :update] do resources :targets, except: [:show, :edit, :update] do
collection do collection do
get 'edit/:date', action: :edit, as: :edit get 'edit/:date', action: :edit, as: :edit
patch '', action: :update
post 'reapply/:date', action: :reapply, as: :reapply post 'reapply/:date', action: :reapply, as: :reapply
end end
end end

View File

@ -86,6 +86,7 @@ class TargetsTest < BodyTrackingSystemTestCase
'@project.targets.reload.count' => 1, 'Threshold.count' => 1 do '@project.targets.reload.count' => 1, 'Threshold.count' => 1 do
visit goal_targets_path(@project.goals.binding) visit goal_targets_path(@project.goals.binding)
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do within 'form#new-target-form' do
fill_in t(:field_effective_from), with: date fill_in t(:field_effective_from), with: date
within 'p.target' do within 'p.target' do
@ -142,6 +143,7 @@ class TargetsTest < BodyTrackingSystemTestCase
'Threshold.count' => thresholds.length do 'Threshold.count' => thresholds.length do
visit goal_targets_path(@project.goals.binding) visit goal_targets_path(@project.goals.binding)
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do within 'form#new-target-form' do
within 'p.target' do within 'p.target' do
select quantity.name select quantity.name
@ -167,9 +169,13 @@ class TargetsTest < BodyTrackingSystemTestCase
end end
def test_create_multiple_targets def test_create_multiple_targets
# TODO
end end
def test_create_form_retains_data_on_failure def test_create_properly_handles_data_on_failure
# TODO
# * restoring non-empty targets values
# * removing empty targets
end end
def test_create_duplicate_for_persisted_target_should_fail def test_create_duplicate_for_persisted_target_should_fail
@ -206,8 +212,10 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_no_difference 'Target.count' do assert_no_difference 'Target.count' do
visit goal_targets_path(@project.goals.binding) visit goal_targets_path(@project.goals.binding)
click_link t('targets.contextual.link_new_target') click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do within 'form#new-target-form' do
fill_in t(:field_effective_from), with: date fill_in t(:field_effective_from), with: date
within :xpath, '//p[@class="target"][1]' do within :xpath, '//p[@class="target"][1]' do
select quantity.name select quantity.name
select @project.quantities.target.roots.sample.name select @project.quantities.target.roots.sample.name
@ -215,6 +223,7 @@ class TargetsTest < BodyTrackingSystemTestCase
select @project.units.sample.shortname select @project.units.sample.shortname
end end
click_link t('targets.form.button_new_target') click_link t('targets.form.button_new_target')
within :xpath, '//p[@class="target"][2]' do within :xpath, '//p[@class="target"][2]' do
select quantity.name select quantity.name
select @project.quantities.target.roots.sample.name select @project.quantities.target.roots.sample.name
@ -222,42 +231,67 @@ class TargetsTest < BodyTrackingSystemTestCase
select @project.units.sample.shortname select @project.units.sample.shortname
end end
click_on t(:button_create) click_on t(:button_create)
assert_selector :xpath, '//p[@class="target"][last()]//preceding-sibling::div', assert_selector :xpath, '//p[@class="target"][last()]//preceding-sibling::div',
text: msg text: msg
end end
end end
end end
def test_create_and_simultaneously_remove_persisted_duplicate
end
# TODO: test_create_failure(s)
# * restoring non-empty targets values
# * removing empty targets
def test_edit_binding_target def test_edit_binding_target
# TODO: extend with item + scope
target = @project.goals.binding.targets.sample target = @project.goals.binding.targets.sample
date = Date.current + rand(-10..10).days
quantity = @project.quantities.except_targets.where.not(id: target.quantity.id).sample
thresholds =
@project.quantities.target.where.not(parent: nil).sample.self_and_ancestors.map do |q|
[q, rand(-2000.0..2000.0).to_d(4), @project.units.sample]
end
visit goal_targets_path(@project.goals.binding) visit goal_targets_path(@project.goals.binding)
assert_no_selector 'form#edit-target-form' assert_no_selector 'form#edit-target-form'
within find('td', text: target.effective_from).ancestor('tr') do within find('td', text: target.effective_from).ancestor('tr') do
click_link t(:button_edit) click_link t(:button_edit)
assert_selector :xpath, 'following-sibling::tr//form[@id="edit-target-form"]'
end
within :xpath, 'following-sibling::tr//form' do assert_no_difference 'Target.count' do
within 'form#edit-target-form' do
assert has_field?(t(:field_effective_from), with: target.effective_from) assert has_field?(t(:field_effective_from), with: target.effective_from)
fill_in t(:field_effective_from), with: date
# NOTE: this assumes target.quantity is unique which is not enforced now within find('select option[selected]', exact_text: target.quantity.name)
threshold = t.thresholds.first .ancestor('p.target') do
within find('select option[selected]', exact_text: threshold.quantity.name) target.thresholds.each do |threshold|
.ancestor('p') do assert has_select?(selected: threshold.quantity.name)
assert has_select?(selected: t.condition) assert has_field?(with: threshold.value)
assert has_field?(with: threshold.value) assert has_select?(selected: threshold.unit.shortname)
assert has_select?(selected: threshold.unit.shortname) end
select quantity.name
thresholds.each do |t_quantity, t_value, t_unit|
within select(t_quantity.name).ancestor('select') do
find(:xpath, 'following-sibling::input').set(t_value)
find(:xpath, 'following-sibling::select[1]').select(t_unit.shortname)
end
end
end end
click_on t(:button_save)
end end
end end
assert_selector 'form#edit-target-form', count: 1 target.reload
assert_equal date, target.effective_from
assert_equal quantity, target.quantity
assert_equal thresholds.length, target.thresholds.length
thresholds.each_with_index do |threshold, index|
t_quantity, t_value, t_unit = threshold
assert_equal t_quantity, target.thresholds[index].quantity
assert_equal t_value, target.thresholds[index].value
assert_equal t_unit, target.thresholds[index].unit
end
end end
def test_update def test_update
@ -293,5 +327,10 @@ class TargetsTest < BodyTrackingSystemTestCase
end end
def test_update_swap_targets def test_update_swap_targets
# TODO
end
def test_update_add_and_simultaneously_remove_persisted_duplicate
# TODO
end end
end end