diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb index a53efc9..d0f2147 100644 --- a/app/controllers/concerns/finders.rb +++ b/app/controllers/concerns/finders.rb @@ -8,6 +8,13 @@ module Concerns::Finders render_404 end + def find_goal_by_project_id + @project = Project.find(params[:project_id]) + @goal = @project.goals.binding + rescue ActiveRecord::RecordNotFound + render_404 + end + def find_meal @meal = Meal.find(params[:id]) @project = @meal.project diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index 95e0194..bad1e22 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -2,40 +2,44 @@ class TargetsController < ApplicationController layout 'body_tracking' menu_item :body_trackers helper :body_trackers - helper_method :current_goal include Concerns::Finders - before_action :find_project_by_project_id, only: [:index, :new, :create] - before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] - before_action :find_goal, only: [:toggle_exposure] - before_action :set_view_params + before_action :find_goal_by_project_id, only: [:index, :new] + #, if: ->{ params[:project_id].present? } + #before_action :find_goal, only: [:index, :new], + # unless: -> { @goal } + before_action :find_project_by_project_id, only: [:create] + before_action :authorize + #before_action :set_view_params def index prepare_targets end def new - target = current_goal.targets.new + target = @goal.targets.new target.arity.times { target.thresholds.new } @targets = [target] end def create - goal_id = params[:goal][:id] - goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.new - goal.attributes = goal_params unless goal.is_binding? + @goal = @project.goals.find(params[:goal_id]) if params[:goal_id].present? + @goal ||= @project.goals.new + @goal.attributes = goal_params unless @goal.is_binding? - @targets = goal.targets.build(targets_params[:targets]) do |target| + @targets = @goal.targets.build(targets_params[:targets]) do |target| target.effective_from = params[:target][:effective_from] end - if goal.target_exposures.empty? - goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6) + if @goal.target_exposures.empty? + @goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6) end # :save only after build, to re-display values in case records are invalid - if goal.save && Target.transaction { @targets.all?(&:save) } + if @goal.save && Target.transaction { @targets.all?(&:save) } flash[: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.each do |target| @@ -50,6 +54,8 @@ class TargetsController < ApplicationController end def update + goal_id = params[:goal][:id] + goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.binding end def destroy @@ -59,14 +65,10 @@ class TargetsController < ApplicationController end def toggle_exposure - current_goal.target_exposures.toggle!(@quantity) + @goal.target_exposures.toggle!(@quantity) prepare_targets end - def current_goal - @goal || @project.goals.binding - end - private def goal_params @@ -90,7 +92,7 @@ class TargetsController < ApplicationController end def prepare_targets - @quantities = current_goal.quantities.includes(:formula) + @quantities = @goal.quantities.includes(:formula) @targets_by_date = Hash.new { |h,k| h[k] = {} } @project.targets.includes(:item, thresholds: [:quantity]).reject(&:new_record?) @@ -106,6 +108,6 @@ class TargetsController < ApplicationController else {view: :by_scope, scope: :all} end - @view_params[goal_id] = @goal.id if @goal + @view_params[:goal_id] = @goal.id if @goal end end diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index 27b5e3b..d3d62c0 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -2,11 +2,11 @@
- <% if current_goal.persisted? %> -

<%= render partial: 'goals/show_form', locals: {goal: current_goal} %>

-

<%= f.date_field :effective_from, disabled: !current_goal.is_binding? %>

+ <% if @goal.persisted? %> +

<%= render partial: 'goals/show_form' %>

+

<%= f.date_field :effective_from, disabled: !@goal.is_binding? %>

<% else %> - <%= render partial: 'goals/form', locals: {goal: current_goal} %> + <%= render partial: 'goals/form' %> <% end %>
diff --git a/app/views/targets/_options.html.erb b/app/views/targets/_options.html.erb index 0188b4a..924453b 100644 --- a/app/views/targets/_options.html.erb +++ b/app/views/targets/_options.html.erb @@ -1,7 +1,7 @@
<%= l(:label_options) %>
- <%= form_tag toggle_exposure_goal_path(current_goal, @view_params), + <%= form_tag toggle_exposure_goal_path(@goal, @view_params), id: 'toggle-exposure-form', name: 'toggle-exposure-form', method: :post, remote: true do %> @@ -9,7 +9,7 @@ <%= select_tag 'quantity_id', - toggle_exposure_options(current_goal.quantities) %> + toggle_exposure_options(@goal.quantities) %> <%= submit_tag l(:button_add) %> diff --git a/app/views/targets/create.js.erb b/app/views/targets/create.js.erb index b825416..92ea423 100644 --- a/app/views/targets/create.js.erb +++ b/app/views/targets/create.js.erb @@ -1,7 +1,7 @@ $('#new-target').empty(); -<% case @view_params[:view] %> - <% when :by_date %> - $('#targets').html('<%= j render partial: 'targets/by_effective_date' %>'); - <% else %> +<%# case @view_params[:view] %> + <%# when :by_date %> +// $('#targets').html('<%#= j render partial: 'targets/by_effective_date' %>'); + <%# else %> $('#targets').html('<%= j render partial: 'targets/index' %>'); -<% end %> +<%# end %>