From 406eabaccc285e168eb091952335894ea027fc3a Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 6 Mar 2021 13:00:50 +0100 Subject: [PATCH] Fix target subtreshold listing Route targets under goal --- app/controllers/concerns/finders.rb | 4 ++++ app/controllers/targets_controller.rb | 13 +++++++------ app/views/goals/_index.html.erb | 4 ++-- app/views/targets/_form.html.erb | 2 +- app/views/targets/_new_form.html.erb | 2 +- app/views/targets/_thresholds_form.html.erb | 4 ++-- app/views/targets/index.html.erb | 3 +++ config/locales/en.yml | 2 +- config/routes.rb | 8 ++++++-- 9 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb index 0285bf2..64e1a8e 100644 --- a/app/controllers/concerns/finders.rb +++ b/app/controllers/concerns/finders.rb @@ -8,6 +8,10 @@ module Concerns::Finders render_404 end + def find_goal_by_goal_id + find_goal(params[:goal_id]) + end + def find_binding_goal_by_project_id @project = Project.find(params[:project_id]) @goal = @project.goals.binding diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index ef41258..a02f4b3 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -6,12 +6,13 @@ class TargetsController < ApplicationController include Concerns::Finders before_action :find_binding_goal_by_project_id, only: [:new, :edit] - before_action :find_project_by_project_id, only: [:create, :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? } #before_action :find_goal, only: [:index, :new], # unless: -> { @goal } - before_action :find_goal, only: [:index, :toggle_exposure] + before_action :find_goal_by_goal_id, only: [:index, :subthresholds] + before_action :find_goal, only: [:toggle_exposure] before_action :authorize #before_action :set_view_params @@ -21,7 +22,6 @@ class TargetsController < ApplicationController def new target = @goal.targets.new - target.thresholds.new(quantity: Quantity.target.roots.last) @targets = [target] @effective_from = target.effective_from end @@ -69,13 +69,14 @@ class TargetsController < ApplicationController end def subthresholds - @target = @project.goals.binding.targets.new - quantity = @project.quantities.target.find_by(id: params['quantity_id']) + @target = @goal.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.new(quantity: quantity) + @target.thresholds.first.quantity = quantity end end diff --git a/app/views/goals/_index.html.erb b/app/views/goals/_index.html.erb index dd86405..ad6afd8 100644 --- a/app/views/goals/_index.html.erb +++ b/app/views/goals/_index.html.erb @@ -2,7 +2,7 @@ - + @@ -12,7 +12,7 @@
<%= l(:field_name) %><%= l(:field_name) %> <%= l(:field_description) %> <%= l(:field_action) %>
- <%= checked_image g.is_binding %><%= link_to g.name, targets_goal_path(g) %> + <%= checked_image g.is_binding %><%= link_to g.name, goal_targets_path(g) %>
<%# TODO: display # of active targets/targeted quantities %> diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index 8983381..1660d64 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -1,6 +1,6 @@ <% if goal_f.object.is_binding? %>

- <%= date_field :effective_from, value: @effective_from %> + <%= goal_f.date_field :effective_from, value: @effective_from, required: true %>

<% end %> diff --git a/app/views/targets/_new_form.html.erb b/app/views/targets/_new_form.html.erb index b42a525..a923b1d 100644 --- a/app/views/targets/_new_form.html.erb +++ b/app/views/targets/_new_form.html.erb @@ -1,7 +1,7 @@

<%= t ".heading_new_target" %>

<%# url: project_targets_path(@project, @view_params), %> -<%= labelled_form_for [@project, @goal], remote: true, +<%= labelled_form_for @goal, remote: true, html: {id: 'new-target-form', name: 'new-target-form'} do |goal_f| %> <%= error_messages_for *@targets %> diff --git a/app/views/targets/_thresholds_form.html.erb b/app/views/targets/_thresholds_form.html.erb index 94c3e25..0bf7f7b 100644 --- a/app/views/targets/_thresholds_form.html.erb +++ b/app/views/targets/_thresholds_form.html.erb @@ -5,10 +5,10 @@ <%= threshold_f.collection_select :quantity_id, @project.quantities.target.children_of(parent_id), :id, :name, - {prompt: parent_id.nil? ? false : '.', required: true, no_label: true}, + {include_blank: parent_id.nil? ? false : '.', required: true, no_label: true}, {autocomplete: 'off', onchange: "$.ajax({ - url: '#{subthresholds_project_targets_path(parent_id: parent_id)}', + url: '#{subthresholds_goal_targets_path(@goal, parent_id: parent_id)}', data: 'quantity_id=' + $(this).val(), dataType: 'html', success: function(data) { diff --git a/app/views/targets/index.html.erb b/app/views/targets/index.html.erb index 4fa2921..28a05ed 100644 --- a/app/views/targets/index.html.erb +++ b/app/views/targets/index.html.erb @@ -6,6 +6,9 @@
<%= title [t("goals.index.heading"), project_goals_path(@project)], @goal.name %> +<% if @goal.description? %> +

<%= @goal.description %>

+<% end %>
<%= render partial: 'targets/index' %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 2ce1cbb..7ac0950 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -104,7 +104,7 @@ en: link_new_goal: 'New goal' targets: contextual: - link_new_target: 'New target' + link_new_target: 'New target(s)' form: choose_quantity: 'Choose quantity' button_new_target: 'Add target' diff --git a/config/routes.rb b/config/routes.rb index 14a4af5..d3213d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,14 +9,18 @@ resources :projects, shallow: true do end resources :goals do member do - get 'targets', controller: :targets, action: :index, as: :targets + #get 'targets', controller: :targets, action: :index, as: :targets post 'toggle_exposure', controller: :targets end + resources :targets, only: [:index] do + collection do + get 'subthresholds/(:parent_id)', action: :subthresholds, as: :subthresholds + end + end end resources :targets, except: [:show, :edit] do collection do get 'edit/:date', action: :edit, as: :edit - get 'subthresholds/(:parent_id)', action: :subthresholds, as: :subthresholds post 'reapply/:date', action: :reapply, as: :reapply end end