summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/targets_controller.rb2
-rw-r--r--app/models/target.rb10
-rw-r--r--db/seeds.rb3
-rw-r--r--db/seeds/Target.rb21
4 files changed, 29 insertions, 7 deletions
diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb
index 60d3523..a11ddd7 100644
--- a/app/controllers/targets_controller.rb
+++ b/app/controllers/targets_controller.rb
@@ -1,5 +1,5 @@
class TargetsController < ApplicationController
- before_action :set_target, only: %i[show edit update]
+ before_action :set_target, only: %i[show edit update destroy]
def index
@targets = Target.order(:name)
diff --git a/app/models/target.rb b/app/models/target.rb
index 3063b42..9211e74 100644
--- a/app/models/target.rb
+++ b/app/models/target.rb
@@ -4,7 +4,7 @@ class Target < ApplicationRecord
has_many :nutrient_profiles, through: :target_allocations
accepts_nested_attributes_for :target_allocations, allow_destroy: true
- validate :percentages_sum_to_100
+ # validate :percentages_sum_to_100
def weighted_requirements
totals = Hash.new(0.0)
@@ -35,8 +35,8 @@ class Target < ApplicationRecord
private
- def percentages_sum_to_100
- sum = target_allocations.reject(&:marked_for_destruction?).sum { |a| a.percentage.to_f }
- errors.add(:base, "La somme des pourcentages doit être égale à 100%") unless (sum - 100.0).abs <= 0.01
- end
+ # def percentages_sum_to_100
+ # sum = target_allocations.sum { |a| a.percentage.to_f }
+ # errors.add(:base, "La somme des pourcentages doit être égale à 100%") unless (sum - 100.0).abs <= 0.1
+ # end
end
diff --git a/db/seeds.rb b/db/seeds.rb
index 245247e..d5f046d 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -9,7 +9,8 @@
# end
MODELS = [ NutrientMeasurement,
- NutrientProfile
+ NutrientProfile,
+ Target
].freeze
def seed_file_for(model_class)
diff --git a/db/seeds/Target.rb b/db/seeds/Target.rb
new file mode 100644
index 0000000..790fef7
--- /dev/null
+++ b/db/seeds/Target.rb
@@ -0,0 +1,21 @@
+Target.transaction do
+ wanted = {
+ "salades" => 40,
+ "tomates général" => 40,
+ "développement floral" => 20
+ }
+
+ # Ensure all referenced profiles exist
+ profiles = NutrientProfile.where(name: wanted.keys).index_by(&:name)
+ missing = wanted.keys - profiles.keys
+ raise "Missing NutrientProfile(s): #{missing.join(', ')}" if missing.any?
+
+ target = Target.find_or_create_by!(name: "Objectif par défaut")
+
+ target.target_allocations.destroy_all
+ wanted.each do |name, pct|
+ target.target_allocations.build(nutrient_profile: profiles[name], percentage: pct)
+ end
+
+ target.save!
+end
Copyright 2019--2025 Marius PETER