diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index 4aa16c4..b418dc0 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -1,13 +1,12 @@ class TargetsController < ApplicationController - layout 'body_tracking' + layout 'body_tracking', except: :subthresholds menu_item :body_trackers helper :body_trackers include Concerns::Finders before_action :find_binding_goal_by_project_id, only: [:index, :new, :edit] - before_action :find_project, only: [:subthresholds] - before_action :find_project_by_project_id, only: [:create] + before_action :find_project_by_project_id, only: [:create, :subthresholds] before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] #, if: ->{ params[:project_id].present? } #before_action :find_goal, only: [:index, :new], @@ -78,12 +77,14 @@ class TargetsController < ApplicationController end def subthresholds - quantity_id = params[:goal][:targets_attributes] - .last[:thresholds_attributes][:quantity_id] - return if quantity_id.blank? - - quantity = @project.quantities.find(quantity_id) - @threshold = Threshold.new(quantity: quantity) + @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]) + else + @last_quantity = quantity + @target.thresholds.new(quantity: quantity) + end end private diff --git a/app/views/goals/_show_form.html.erb b/app/views/goals/_show_form.html.erb index f353963..cbe16ae 100644 --- a/app/views/goals/_show_form.html.erb +++ b/app/views/goals/_show_form.html.erb @@ -1,3 +1,4 @@ +<%# FIXME: nie trzeba ustawiac goal_id, data-remote samo wysle %> <%= goal_f.collection_select :id, @project.goals, :id, :name, {include_blank: false, required: true, label: :field_goal}, autocomplete: 'off', diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index d6130cc..81cf015 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -22,16 +22,9 @@ {include_blank: true, required: true, label: :field_target}, onchange: "showQuantityPath(event);" %> - <%= target_f.fields_for :thresholds do |threshold_f| %> - <%= render partial: 'thresholds/form', locals: {threshold_f: threshold_f} %> - <% end %> - <% last_quantity = target_f.object.thresholds.last.quantity %> - <% unless last_quantity.leaf? %> - <%= target_f.fields_for 'thresholds_attributes', Threshold.new do |threshold_f| %> - <%= render partial: 'thresholds/form', - locals: {threshold_f: threshold_f, parent: last_quantity} %> - <% end %> - <% end %> + <%= render partial: 'targets/thresholds_form', + locals: {target_f: target_f, + last_quantity: target_f.object.thresholds.last.quantity} %> <%= link_to t(".button_delete_target"), '#', class: 'icon icon-del', style: (@targets.many? ? "" : "display:none"), diff --git a/app/views/targets/_thresholds_form.html.erb b/app/views/targets/_thresholds_form.html.erb new file mode 100644 index 0000000..94c3e25 --- /dev/null +++ b/app/views/targets/_thresholds_form.html.erb @@ -0,0 +1,28 @@ +<% target_f.object.thresholds.new unless last_quantity.leaf? %> +<%= target_f.fields_for :thresholds, child_index: '' do |threshold_f| %> + <% threshold_q = threshold_f.object.quantity %> + <% parent_id = threshold_q.nil? ? last_quantity.id : threshold_q.parent_id %> + <%= 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}, + {autocomplete: 'off', + onchange: "$.ajax({ + url: '#{subthresholds_project_targets_path(parent_id: parent_id)}', + data: 'quantity_id=' + $(this).val(), + dataType: 'html', + success: function(data) { + $(event.target).nextAll().addBack().last().after(data).end().remove(); + } + }); + return false;"} %> + <%# {autocomplete: 'off', + data: {remote: true, url: subthresholds_project_targets_path(parent_id: parent_id)}} %> + + <% unless threshold_q.nil? %> + <%= threshold_f.hidden_field :_destroy %> + <%= threshold_f.number_field :value, {size: 8, step: :any, no_label: true} %> + <%= threshold_f.collection_select :unit_id, @project.units, :id, :shortname, + {no_label: true} %> + <% end %> +<% end %> diff --git a/app/views/targets/subthresholds.html.erb b/app/views/targets/subthresholds.html.erb new file mode 100644 index 0000000..2f06119 --- /dev/null +++ b/app/views/targets/subthresholds.html.erb @@ -0,0 +1,4 @@ +<%= fields_for 'goal[targets_attributes][]', @target do |target_f| %> + <%= render partial: 'targets/thresholds_form', + locals: {target_f: target_f, last_quantity: @last_quantity} %> +<% end %> diff --git a/app/views/targets/subthresholds.js.erb b/app/views/targets/subthresholds.js.erb deleted file mode 100644 index 2700061..0000000 --- a/app/views/targets/subthresholds.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if @threshold %> - $('#targets').html('<%= j render partial: 'targets/index' %>'); -<% else %> - $(event.target).empty(); -<% end %> diff --git a/app/views/thresholds/_form.html.erb b/app/views/thresholds/_form.html.erb deleted file mode 100644 index 976889b..0000000 --- a/app/views/thresholds/_form.html.erb +++ /dev/null @@ -1,15 +0,0 @@ -<% threshold_q = threshold_f.object.quantity %> -<% parent_id = defined?(parent) ? parent.id : threshold_q.parent_id %> -<%= 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}, - {autocomplete: 'off', data: {remote: true, - url: subthresholds_quantity_path(@project)}} %> - -<% unless threshold_q.nil? %> - <%= threshold_f.hidden_field :_destroy %> - <%= threshold_f.number_field :value, {size: 8, step: :any, no_label: true} %> - <%= threshold_f.collection_select :unit_id, @project.units, :id, :shortname, - {no_label: true} %> -<% end %> diff --git a/config/routes.rb b/config/routes.rb index 2277b03..28b9be6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,7 @@ resources :projects, shallow: true do resources :targets, except: [:show, :edit, :update] do collection do get 'edit/:date', action: :edit, as: :edit + get 'subthresholds/(:parent_id)', action: :subthresholds, as: :subthresholds patch :update post 'reapply/:date', action: :reapply, as: :reapply end @@ -64,7 +65,6 @@ resources :projects, shallow: true do resources :quantities, except: [:show] do member do get 'new_child' - get 'subthresholds', controller: :targets post 'create_child' post 'move/:direction', action: :move, as: :move end