diff --git a/app/controllers/goals_controller.rb b/app/controllers/goals_controller.rb index b6fd049..2a24140 100644 --- a/app/controllers/goals_controller.rb +++ b/app/controllers/goals_controller.rb @@ -6,7 +6,7 @@ class GoalsController < ApplicationController include Concerns::Finders before_action :find_project_by_project_id, only: [:index, :new, :create] - before_action :find_goal, only: [:show, :edit] + before_action :find_goal, only: [:edit, :update] before_action :authorize def index @@ -29,10 +29,16 @@ class GoalsController < ApplicationController end end - def show + def edit end - def edit + def update + if @goal.update(params.require(:goal).permit(:name, :description)) + flash.now[:notice] = 'Updated goal' + @goals = @project.goals + else + render :new + end end private diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index a02f4b3..fb97aa3 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -5,13 +5,10 @@ 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] + 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] - #, if: ->{ params[:project_id].present? } - #before_action :find_goal, only: [:index, :new], - # unless: -> { @goal } - before_action :find_goal_by_goal_id, only: [:index, :subthresholds] + before_action :find_goal_by_goal_id, only: [:index, :new, :create] before_action :find_goal, only: [:toggle_exposure] before_action :authorize #before_action :set_view_params @@ -27,19 +24,14 @@ class TargetsController < ApplicationController end def create - @goal = @project.goals.find_by(id: params[:goal][:id]) || @project.goals.new - @goal.attributes = goal_params - @targets = @goal.targets.build(targets_params[:targets_attributes]) do |target| - target.effective_from = params[:target][:effective_from] - end + @effective_from = params[:goal].delete(:effective_from) + params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from } - # :save only after build, to re-display values in case records are invalid - if @goal.save + if @goal.update(targets_params) flash.now[:notice] = 'Created new target(s)' - # create view should only refresh targets belonging to @goal - # e.g. by rendering to div#goal-id-targets prepare_targets else + @targets = @goal.targets.select(&:changed_for_autosave?) @targets.each { |t| t.thresholds.new unless t.thresholds.present? } render :new end @@ -69,7 +61,7 @@ class TargetsController < ApplicationController end def subthresholds - @target = @goal.targets.new + @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]) diff --git a/app/helpers/targets_helper.rb b/app/helpers/targets_helper.rb index 93f46ee..9d10c89 100644 --- a/app/helpers/targets_helper.rb +++ b/app/helpers/targets_helper.rb @@ -1,8 +1,8 @@ module TargetsHelper def action_links(d) - link_to(l(:button_reapply), reapply_project_targets_path(@project, d, @view_params), + link_to(l(:button_reapply), reapply_goal_targets_path(@project, d, @view_params), {remote: true, class: "icon icon-reload"}) + - link_to(l(:button_edit), edit_project_targets_path(@project, d, @view_params), + link_to(l(:button_edit), edit_goal_targets_path(@project, d, @view_params), {remote: true, class: "icon icon-edit"}) + delete_link(target_path(d), {remote: true, data: {}}) end diff --git a/app/models/threshold.rb b/app/models/threshold.rb index 54984c6..7bbbcf1 100644 --- a/app/models/threshold.rb +++ b/app/models/threshold.rb @@ -1,5 +1,5 @@ class Threshold < QuantityValue - DOMAIN = :target + DOMAIN = "target" # Need to specify polymorphic association so :registry_type gets written (see # QuantityValue for explanation why it's needed) diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 8b61c5c..b296dda 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -15,7 +15,7 @@

<%= t ".heading_common" %>