diff options
Diffstat (limited to 'app/controllers/targets_controller.rb')
| -rw-r--r-- | app/controllers/targets_controller.rb | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index a11ddd7..679cb75 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -6,16 +6,24 @@ class TargetsController < ApplicationController end def new - @target = Target.new(name: "Cible #{Date.today + 1.month}") - seed_allocations + @target = Target.new + @nutrient_profiles = NutrientProfile.order(:name) + # Build one allocation per profile so each appears as a row + @nutrient_profiles.each do |np| + @target.target_allocations.build(nutrient_profile: np, percentage: 12.5) + end end def create @target = Target.new(target_params) if @target.save - redirect_to @target, notice: "Cible enregistrée." + redirect_to @target, notice: "Cible créée." else - seed_allocations if @target.target_allocations.blank? + # Rebuild rows for any profiles missing (e.g., after validation errors) + existing_ids = @target.target_allocations.map(&:nutrient_profile_id) + (NutrientProfile.where.not(id: existing_ids)).order(:name).each do |np| + @target.target_allocations.build(nutrient_profile: np, percentage: 0) + end render :new, status: :unprocessable_entity end end @@ -31,18 +39,12 @@ class TargetsController < ApplicationController end end - def show - @weighted = @target.weighted_requirements # => { "nno3"=>..., "p"=>..., ... } - - last = NutrientMeasurement.order(measured_on: :desc, created_at: :desc).first - @latest_measurements = {} - - if last - # Use the same keys as NutrientProfile to keep naming consistent. - keys = (NutrientProfile::NUTRIENT_KEYS rescue []).map(&:to_s) - keys.each do |k| - @latest_measurements[k] = last.send(k) if last.respond_to?(k) - end + def destroy + @target = Target.find(params[:id]) + @target.destroy + respond_to do |format| + format.turbo_stream { render turbo_stream: turbo_stream.remove(dom_id(@target)) } + format.html { redirect_to targets_path, notice: "Cible supprimé." } end end @@ -52,13 +54,6 @@ class TargetsController < ApplicationController @target = Target.find(params[:id]) end - def seed_allocations - existing_ids = @target.target_allocations.map(&:nutrient_profile_id).compact - (NutrientProfile.order(:name).pluck(:id) - existing_ids).each do |np_id| - @target.target_allocations.build(nutrient_profile_id: np_id, percentage: 12.5) - end - end - def target_params params.require(:target).permit( :name, |