From bbc146796508cbb3c8b5203cdfacd0c704d9ff77 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 25 Apr 2021 15:25:41 +0200 Subject: [PATCH] Parametrize Target routes with :date --- app/controllers/targets_controller.rb | 4 ++-- app/helpers/targets_helper.rb | 6 +++--- app/views/targets/_index.html.erb | 2 +- app/views/targets/_options.html.erb | 2 +- config/routes.rb | 19 +++++++++---------- test/system/targets_test.rb | 3 +++ 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index dae50e5..0b8d2d7 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -5,9 +5,9 @@ class TargetsController < ApplicationController include Concerns::Finders + before_action :find_goal_by_goal_id, + only: [:index, :new, :create, :edit, :update, :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 #before_action :set_view_params diff --git a/app/helpers/targets_helper.rb b/app/helpers/targets_helper.rb index f3455c1..4b3bcc3 100644 --- a/app/helpers/targets_helper.rb +++ b/app/helpers/targets_helper.rb @@ -1,9 +1,9 @@ module TargetsHelper def action_links(date) - link_to(l(:button_reapply), reapply_goal_targets_path(@goal, date, @view_params), + link_to(l(:button_reapply), reapply_goal_target_path(@goal, date, @view_params), {remote: true, class: "icon icon-reload"}) + - link_to(l(:button_edit), edit_goal_targets_path(@goal, date, @view_params), + link_to(l(:button_edit), edit_goal_target_path(@goal, date, @view_params), {remote: true, class: "icon icon-edit"}) + - delete_link(target_path(date), {remote: true, data: {}}) + delete_link(goal_target_path(@goal, date), {remote: true, data: {}}) end end diff --git a/app/views/targets/_index.html.erb b/app/views/targets/_index.html.erb index c5ab8a2..0a25b56 100644 --- a/app/views/targets/_index.html.erb +++ b/app/views/targets/_index.html.erb @@ -37,7 +37,7 @@ <% else %> <% button_classes += ' icon-bullet-closed' %> <% end %> - <%= link_to '', toggle_exposure_goal_path(@goal, quantity_id: q.id), + <%= link_to '', goal_toggle_exposure_path(@goal, quantity_id: q.id), {class: button_classes, method: :post, remote: true} %> <%= q.name %> diff --git a/app/views/targets/_options.html.erb b/app/views/targets/_options.html.erb index 924453b..e59b5e5 100644 --- a/app/views/targets/_options.html.erb +++ b/app/views/targets/_options.html.erb @@ -1,7 +1,7 @@
<%= l(:label_options) %>
- <%= form_tag toggle_exposure_goal_path(@goal, @view_params), + <%= form_tag goal_toggle_exposure_path(@goal, @view_params), id: 'toggle-exposure-form', name: 'toggle-exposure-form', method: :post, remote: true do %> diff --git a/config/routes.rb b/config/routes.rb index 1e22b8a..ccc6c32 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,16 +8,6 @@ resources :projects, shallow: true do end end resources :goals, except: [:show] do - member do - post 'toggle_exposure', controller: :targets - end - resources :targets, except: [:show, :edit, :update] do - collection do - get 'edit/:date', action: :edit, as: :edit - patch '', action: :update - post 'reapply/:date', action: :reapply, as: :reapply - end - end end resources :ingredients, only: [] do member do @@ -75,4 +65,13 @@ resources :projects, shallow: true do resources :units, only: [:index, :create, :destroy] end +resources :goals, only: [] do + resources :targets, param: :date, except: [:update] do + member do + post 'reapply' + end + end + resource :targets, only: [:update] + post 'toggle_exposure', controller: :targets +end get 'subthresholds', controller: :targets, action: :subthresholds, as: :subthresholds diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index fe2f4b4..819a812 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -186,14 +186,17 @@ class TargetsTest < BodyTrackingSystemTestCase assert_no_difference 'Target.count' do visit goal_targets_path(source.goal) click_link t('targets.contextual.link_new_target') + within 'form#new-target-form' do fill_in t(:field_effective_from), with: source.effective_from + within 'p.target' do select source.quantity.name select @project.quantities.target.roots.sample.name fill_in with: rand(-2000.0..2000.0).to_d(4) select @project.units.sample.shortname end + click_on t(:button_create) assert_selector :xpath, '//p[@class="target"]//preceding-sibling::div', text: msg end