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 include Concerns::Finders
before_action :find_project_by_project_id, only: [:index, :new, :create] 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 before_action :authorize
def index def index
@ -29,10 +29,16 @@ class GoalsController < ApplicationController
end end
end end
def show def edit
end 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 end
private private

View File

@ -5,13 +5,10 @@ class TargetsController < ApplicationController
include Concerns::Finders include Concerns::Finders
before_action :find_binding_goal_by_project_id, only: [:new, :edit] before_action :find_binding_goal_by_project_id, only: [:edit]
before_action :find_project_by_project_id, only: [:create] before_action :find_project_by_project_id, only: [:subthresholds]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] before_action :find_quantity_by_quantity_id, only: [:toggle_exposure]
#, if: ->{ params[:project_id].present? } before_action :find_goal_by_goal_id, only: [:index, :new, :create]
#before_action :find_goal, only: [:index, :new],
# unless: -> { @goal }
before_action :find_goal_by_goal_id, only: [:index, :subthresholds]
before_action :find_goal, only: [:toggle_exposure] before_action :find_goal, only: [:toggle_exposure]
before_action :authorize before_action :authorize
#before_action :set_view_params #before_action :set_view_params
@ -27,19 +24,14 @@ class TargetsController < ApplicationController
end end
def create def create
@goal = @project.goals.find_by(id: params[:goal][:id]) || @project.goals.new @effective_from = params[:goal].delete(:effective_from)
@goal.attributes = goal_params params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
@targets = @goal.targets.build(targets_params[:targets_attributes]) do |target|
target.effective_from = params[:target][:effective_from]
end
# :save only after build, to re-display values in case records are invalid if @goal.update(targets_params)
if @goal.save
flash.now[:notice] = 'Created new target(s)' 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 prepare_targets
else else
@targets = @goal.targets.select(&:changed_for_autosave?)
@targets.each { |t| t.thresholds.new unless t.thresholds.present? } @targets.each { |t| t.thresholds.new unless t.thresholds.present? }
render :new render :new
end end
@ -69,7 +61,7 @@ class TargetsController < ApplicationController
end end
def subthresholds def subthresholds
@target = @goal.targets.new @target = @project.goals.binding.targets.new
quantity = @project.quantities.target.find_by(id: params[:quantity_id]) quantity = @project.quantities.target.find_by(id: params[:quantity_id])
if quantity.nil? if quantity.nil?
@last_quantity = @project.quantities.target.find(params[:parent_id]) @last_quantity = @project.quantities.target.find(params[:parent_id])

View File

@ -1,8 +1,8 @@
module TargetsHelper module TargetsHelper
def action_links(d) 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"}) + {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"}) + {remote: true, class: "icon icon-edit"}) +
delete_link(target_path(d), {remote: true, data: {}}) delete_link(target_path(d), {remote: true, data: {}})
end end

View File

@ -1,5 +1,5 @@
class Threshold < QuantityValue class Threshold < QuantityValue
DOMAIN = :target DOMAIN = "target"
# Need to specify polymorphic association so :registry_type gets written (see # Need to specify polymorphic association so :registry_type gets written (see
# QuantityValue for explanation why it's needed) # QuantityValue for explanation why it's needed)

View File

@ -15,7 +15,7 @@
<h3><%= t ".heading_common" %></h3> <h3><%= t ".heading_common" %></h3>
<ul> <ul>
<li> <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) %> <%= link_to t(".link_goals"), project_goals_path(@project) %>
</li> </li>

View File

@ -1,5 +1,4 @@
<% if User.current.allowed_to?(:manage_body_trackers, @project) %> <% if User.current.allowed_to?(:manage_body_trackers, @project) %>
<%= link_to t(".link_new_target"), <%= link_to t(".link_new_target"), new_goal_target_path(@goal, @view_params),
new_project_target_path(@project, @view_params),
{remote: true, class: 'icon icon-add'} %> {remote: true, class: 'icon icon-add'} %>
<% end %> <% end %>

View File

@ -1,7 +1,7 @@
<h2><%= t ".heading_new_target" %></h2> <h2><%= t ".heading_new_target" %></h2>
<%# url: project_targets_path(@project, @view_params), %> <%# 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| %> html: {id: 'new-target-form', name: 'new-target-form'} do |goal_f| %>
<%= error_messages_for *@targets %> <%= error_messages_for *@targets %>

View File

@ -8,7 +8,7 @@
{include_blank: parent_id.nil? ? false : '.', required: true, no_label: true}, {include_blank: parent_id.nil? ? false : '.', required: true, no_label: true},
{autocomplete: 'off', {autocomplete: 'off',
onchange: "$.ajax({ 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(), data: 'quantity_id=' + $(this).val(),
dataType: 'html', dataType: 'html',
success: function(data) { success: function(data) {

View File

@ -7,23 +7,19 @@ resources :projects, shallow: true do
post 'defaults' post 'defaults'
end end
end end
resources :goals do resources :goals, except: [:show] do
member do member do
#get 'targets', controller: :targets, action: :index, as: :targets
post 'toggle_exposure', controller: :targets post 'toggle_exposure', controller: :targets
end end
resources :targets, only: [:index] do resources :targets, shallow: true, except: [:show, :edit, :update] do
collection 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 end
end end
resources :targets, except: [:show, :edit] do get 'subthresholds/(:parent_id)', controller: :targets, action: :subthresholds,
collection do as: :subthresholds
get 'edit/:date', action: :edit, as: :edit
post 'reapply/:date', action: :reapply, as: :reapply
end
end
resources :ingredients, only: [] do resources :ingredients, only: [] do
member do member do
post 'adjust/:adjustment', controller: :meals, action: :adjust, as: :adjust 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 project_module :body_tracking do
permission :view_body_trackers, { permission :view_body_trackers, {
body_trackers: [:index], body_trackers: [:index],
goals: [:index, :show], goals: [:index],
targets: [:index], targets: [:index],
meals: [:index], meals: [:index],
measurement_routines: [:show], measurement_routines: [:show],
@ -27,7 +27,7 @@ Redmine::Plugin.register :body_tracking do
}, read: true }, read: true
permission :manage_body_trackers, { permission :manage_body_trackers, {
body_trackers: [:defaults], body_trackers: [:defaults],
goals: [:new, :create, :edit], goals: [:new, :create, :edit, :update],
targets: [:new, :create, :edit, :update, :destroy, :reapply, :toggle_exposure, targets: [:new, :create, :edit, :update, :destroy, :reapply, :toggle_exposure,
:subthresholds], :subthresholds],
meals: [:new, :create, :edit, :update, :destroy, :edit_notes, :update_notes, meals: [:new, :create, :edit, :update, :destroy, :edit_notes, :update_notes,

View File

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