1
0

Fix targets#create

This commit is contained in:
cryptogopher 2021-03-07 15:07:45 +01:00
parent 406eabaccc
commit 5b73e9e7fd
11 changed files with 33 additions and 40 deletions

View File

@ -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

View File

@ -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])

View File

@ -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

View File

@ -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)

View File

@ -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>

View File

@ -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 %>

View File

@ -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 %>

View File

@ -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) {

View File

@ -7,23 +7,19 @@ resources :projects, shallow: true do
post 'defaults'
end
end
resources :goals do
resources :goals, except: [:show] do
member do
#get 'targets', controller: :targets, action: :index, as: :targets
post 'toggle_exposure', controller: :targets
end
resources :targets, only: [:index] do
resources :targets, shallow: true, except: [:show, :edit, :update] do
collection do
get 'subthresholds/(:parent_id)', action: :subthresholds, as: :subthresholds
get 'edit/:date', action: :edit, as: :edit
post 'reapply/:date', action: :reapply, as: :reapply
end
end
end
resources :targets, except: [:show, :edit] do
collection do
get 'edit/:date', action: :edit, as: :edit
post 'reapply/:date', action: :reapply, as: :reapply
end
end
get 'subthresholds/(:parent_id)', controller: :targets, action: :subthresholds,
as: :subthresholds
resources :ingredients, only: [] do
member do
post 'adjust/:adjustment', controller: :meals, action: :adjust, as: :adjust

View File

@ -15,7 +15,7 @@ Redmine::Plugin.register :body_tracking do
project_module :body_tracking do
permission :view_body_trackers, {
body_trackers: [:index],
goals: [:index, :show],
goals: [:index],
targets: [:index],
meals: [:index],
measurement_routines: [:show],
@ -27,7 +27,7 @@ Redmine::Plugin.register :body_tracking do
}, read: true
permission :manage_body_trackers, {
body_trackers: [:defaults],
goals: [:new, :create, :edit],
goals: [:new, :create, :edit, :update],
targets: [:new, :create, :edit, :update, :destroy, :reapply, :toggle_exposure,
:subthresholds],
meals: [:new, :create, :edit, :update, :destroy, :edit_notes, :update_notes,

View File

@ -63,7 +63,7 @@ class TargetsTest < BodyTrackingSystemTestCase
def test_create_binding_target
assert_difference 'Goal.count' => 0, 'Target.count' => 1,
'@project1.targets.reload.count' => 1, 'Threshold.count' => 1 do
visit project_targets_path(@project1)
visit goal_targets_path(@project1.goals.binding)
click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do
within 'p.target' do