summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/dashboard_controller.rb27
-rw-r--r--app/controllers/nutrient_measurements_controller.rb26
-rw-r--r--app/controllers/targets_controller.rb68
3 files changed, 109 insertions, 12 deletions
diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb
index 6f323a9..a315eda 100644
--- a/app/controllers/dashboard_controller.rb
+++ b/app/controllers/dashboard_controller.rb
@@ -10,10 +10,22 @@ class DashboardController < ApplicationController
# @target = TargetNutrientCalculator.call
# Measurement history table
- # @measurements = NutrientMeasurement.order(measured_on: :desc).limit(10)
+ @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)
- # @npk_measurement_data = measurement_data_series(:nno3, :p, :k)
- # @ammonium_measurement_data = measurement_data_series(: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
@@ -35,13 +47,4 @@ class DashboardController < ApplicationController
unassigned, assigned = data_series.partition { |s| s[:name].casecmp("unassigned").zero? }
assigned + unassigned
end
-
- def measurement_data_series(*nutrients)
- nutrients.map do |formula|
- { name: Nutrient.find_by!(formula:).name,
- data: NutrientMeasurement
- .order(:measured_on)
- .pluck(:measured_on, formula) }
- end
- end
end
diff --git a/app/controllers/nutrient_measurements_controller.rb b/app/controllers/nutrient_measurements_controller.rb
new file mode 100644
index 0000000..71ca9e4
--- /dev/null
+++ b/app/controllers/nutrient_measurements_controller.rb
@@ -0,0 +1,26 @@
+class NutrientMeasurementsController < ApplicationController
+ def index
+ @nutrient_measurements = NutrientMeasurement.order(measured_on: :desc)
+ @npk_measurement_data = NutrientMeasurement.data_series_for(:nno3, :p, :k)
+ end
+
+ def new
+ @nutrient_measurement = NutrientMeasurement.new(measured_on: Date.today)
+ end
+
+ def create
+ @measurement = NutrientMeasurement.new(nutrient_measurement_params)
+ if @measurement.save
+ redirect_to @measurement, notice: "Relevé enregistré."
+ else
+ render :new, status: :unprocessable_entity
+ end
+ end
+
+ private
+
+ def nutrient_measurement_params
+ permitted = [ :measured_on ] + NutrientMeasurement::NUTRIENT_FIELDS
+ params.require(:nutrient_measurement).permit(*permitted)
+ end
+end
diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb
new file mode 100644
index 0000000..60d3523
--- /dev/null
+++ b/app/controllers/targets_controller.rb
@@ -0,0 +1,68 @@
+class TargetsController < ApplicationController
+ before_action :set_target, only: %i[show edit update]
+
+ def index
+ @targets = Target.order(:name)
+ end
+
+ def new
+ @target = Target.new(name: "Cible #{Date.today + 1.month}")
+ seed_allocations
+ end
+
+ def create
+ @target = Target.new(target_params)
+ if @target.save
+ redirect_to @target, notice: "Cible enregistrée."
+ else
+ seed_allocations if @target.target_allocations.blank?
+ render :new, status: :unprocessable_entity
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ if @target.update(target_params)
+ redirect_to @target, notice: "Cible mise à jour."
+ else
+ render :edit, status: :unprocessable_entity
+ 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
+ end
+ end
+
+ private
+
+ def set_target
+ @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,
+ target_allocations_attributes: [ :id, :nutrient_profile_id, :percentage, :_destroy ]
+ )
+ end
+end
Copyright 2019--2025 Marius PETER