Fix targets#create
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<h3><%= t ".heading_common" %></h3>
|
||||
<ul>
|
||||
<li>
|
||||
<%= link_to t(".link_targets"), project_targets_path(@project) %>
|
||||
<%= link_to t(".link_targets"), goal_targets_path(@project.goals.binding) %>
|
||||
/
|
||||
<%= link_to t(".link_goals"), project_goals_path(@project) %>
|
||||
</li>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<% if User.current.allowed_to?(:manage_body_trackers, @project) %>
|
||||
<%= link_to t(".link_new_target"),
|
||||
new_project_target_path(@project, @view_params),
|
||||
<%= link_to t(".link_new_target"), new_goal_target_path(@goal, @view_params),
|
||||
{remote: true, class: 'icon icon-add'} %>
|
||||
<% end %>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<h2><%= t ".heading_new_target" %></h2>
|
||||
|
||||
<%# url: project_targets_path(@project, @view_params), %>
|
||||
<%= labelled_form_for @goal, remote: true,
|
||||
<%= labelled_form_for @goal, url: goal_targets_path(@goal), method: :post, remote: true,
|
||||
html: {id: 'new-target-form', name: 'new-target-form'} do |goal_f| %>
|
||||
|
||||
<%= error_messages_for *@targets %>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
{include_blank: parent_id.nil? ? false : '.', required: true, no_label: true},
|
||||
{autocomplete: 'off',
|
||||
onchange: "$.ajax({
|
||||
url: '#{subthresholds_goal_targets_path(@goal, parent_id: parent_id)}',
|
||||
url: '#{project_subthresholds_path(@project, parent_id: parent_id)}',
|
||||
data: 'quantity_id=' + $(this).val(),
|
||||
dataType: 'html',
|
||||
success: function(data) {
|
||||
|
||||
Reference in New Issue
Block a user