summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/dashboard_controller.rb43
-rw-r--r--app/controllers/targets_controller.rb41
2 files changed, 20 insertions, 64 deletions
diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb
index a315eda..fc8cbd8 100644
--- a/app/controllers/dashboard_controller.rb
+++ b/app/controllers/dashboard_controller.rb
@@ -1,50 +1,11 @@
class DashboardController < ApplicationController
def index
- # Raft allocation by crop type
- # @raft_data = raft_data_series
-
- @nutrient_profiles = NutrientProfile.order(:name)
-
- # Nutrient target table
- # @latest_measurement = NutrientMeasurement.order(measured_on: :desc, created_at: :desc).first
- # @target = TargetNutrientCalculator.call
+ @latest_measurement = NutrientMeasurement.order(measured_on: :desc).first
+ @latest_target = Target.order(created_at: :desc).first
# Measurement history table
@measurements = NutrientMeasurement.order(measured_on: :desc).limit(10)
@npk_measurement_data = NutrientMeasurement.data_series_for(:nno3, :p, :k)
@ammonium_measurement_data = NutrientMeasurement.data_series_for(:nnh4)
-
- @weighted = Target.first.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
- end
- end
-
- private
-
- def raft_data_series
- data_series = []
-
- counts = Raft.left_outer_joins(:crop)
- .group("crops.name", "crops.crop_type")
- .count
-
- counts.each do |(crop_name, crop_type), count|
- name = (crop_name || "unassigned").titleize
- type = (crop_type || "unassigned").titleize
- data = { type => count }
- data_series << { name:, data: }
- end
-
- unassigned, assigned = data_series.partition { |s| s[:name].casecmp("unassigned").zero? }
- assigned + unassigned
end
end
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,
Copyright 2019--2026 Marius PETER