1
0

Replaced #current_goal with @goal

This commit is contained in:
cryptogopher 2020-08-24 22:09:04 +02:00
parent 7e8508f9d4
commit c2055c4eb4
5 changed files with 40 additions and 31 deletions

View File

@ -8,6 +8,13 @@ module Concerns::Finders
render_404 render_404
end 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 def find_meal
@meal = Meal.find(params[:id]) @meal = Meal.find(params[:id])
@project = @meal.project @project = @meal.project

View File

@ -2,40 +2,44 @@ class TargetsController < ApplicationController
layout 'body_tracking' layout 'body_tracking'
menu_item :body_trackers menu_item :body_trackers
helper :body_trackers helper :body_trackers
helper_method :current_goal
include Concerns::Finders include Concerns::Finders
before_action :find_project_by_project_id, only: [:index, :new, :create] before_action :find_goal_by_project_id, only: [:index, :new]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] #, if: ->{ params[:project_id].present? }
before_action :find_goal, only: [:toggle_exposure] #before_action :find_goal, only: [:index, :new],
before_action :set_view_params # unless: -> { @goal }
before_action :find_project_by_project_id, only: [:create]
before_action :authorize
#before_action :set_view_params
def index def index
prepare_targets prepare_targets
end end
def new def new
target = current_goal.targets.new target = @goal.targets.new
target.arity.times { target.thresholds.new } target.arity.times { target.thresholds.new }
@targets = [target] @targets = [target]
end end
def create def create
goal_id = params[:goal][:id] @goal = @project.goals.find(params[:goal_id]) if params[:goal_id].present?
goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.new @goal ||= @project.goals.new
goal.attributes = goal_params unless goal.is_binding? @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] target.effective_from = params[:target][:effective_from]
end end
if goal.target_exposures.empty? if @goal.target_exposures.empty?
goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6) @goal.quantities << @targets.map { |t| t.thresholds.first.quantity }.first(6)
end end
# :save only after build, to re-display values in case records are invalid # :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)' 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 prepare_targets
else else
@targets.each do |target| @targets.each do |target|
@ -50,6 +54,8 @@ class TargetsController < ApplicationController
end end
def update def update
goal_id = params[:goal][:id]
goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.binding
end end
def destroy def destroy
@ -59,14 +65,10 @@ class TargetsController < ApplicationController
end end
def toggle_exposure def toggle_exposure
current_goal.target_exposures.toggle!(@quantity) @goal.target_exposures.toggle!(@quantity)
prepare_targets prepare_targets
end end
def current_goal
@goal || @project.goals.binding
end
private private
def goal_params def goal_params
@ -90,7 +92,7 @@ class TargetsController < ApplicationController
end end
def prepare_targets def prepare_targets
@quantities = current_goal.quantities.includes(:formula) @quantities = @goal.quantities.includes(:formula)
@targets_by_date = Hash.new { |h,k| h[k] = {} } @targets_by_date = Hash.new { |h,k| h[k] = {} }
@project.targets.includes(:item, thresholds: [:quantity]).reject(&:new_record?) @project.targets.includes(:item, thresholds: [:quantity]).reject(&:new_record?)
@ -106,6 +108,6 @@ class TargetsController < ApplicationController
else else
{view: :by_scope, scope: :all} {view: :by_scope, scope: :all}
end end
@view_params[goal_id] = @goal.id if @goal @view_params[:goal_id] = @goal.id if @goal
end end
end end

View File

@ -2,11 +2,11 @@
<div class="box"> <div class="box">
<div id='goal-form' class="tabular"> <div id='goal-form' class="tabular">
<% if current_goal.persisted? %> <% if @goal.persisted? %>
<p><%= render partial: 'goals/show_form', locals: {goal: current_goal} %></p> <p><%= render partial: 'goals/show_form' %></p>
<p><%= f.date_field :effective_from, disabled: !current_goal.is_binding? %></p> <p><%= f.date_field :effective_from, disabled: !@goal.is_binding? %></p>
<% else %> <% else %>
<%= render partial: 'goals/form', locals: {goal: current_goal} %> <%= render partial: 'goals/form' %>
<% end %> <% end %>
</div> </div>

View File

@ -1,7 +1,7 @@
<fieldset id="options" class="collapsible"> <fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend> <legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div> <div>
<%= 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', id: 'toggle-exposure-form', name: 'toggle-exposure-form',
method: :post, remote: true do %> method: :post, remote: true do %>
@ -9,7 +9,7 @@
<tr> <tr>
<td style="width:100%"></td> <td style="width:100%"></td>
<td><%= select_tag 'quantity_id', <td><%= select_tag 'quantity_id',
toggle_exposure_options(current_goal.quantities) %></td> toggle_exposure_options(@goal.quantities) %></td>
<td><%= submit_tag l(:button_add) %></td> <td><%= submit_tag l(:button_add) %></td>
</tr> </tr>
</table> </table>

View File

@ -1,7 +1,7 @@
$('#new-target').empty(); $('#new-target').empty();
<% case @view_params[:view] %> <%# case @view_params[:view] %>
<% when :by_date %> <%# when :by_date %>
$('#targets').html('<%= j render partial: 'targets/by_effective_date' %>'); // $('#targets').html('<%#= j render partial: 'targets/by_effective_date' %>');
<% else %> <%# else %>
$('#targets').html('<%= j render partial: 'targets/index' %>'); $('#targets').html('<%= j render partial: 'targets/index' %>');
<% end %> <%# end %>