From 1d025614f0b53165f73466c260f4168b5a279d56 Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Tue, 10 Dec 2024 12:27:30 +0100 Subject: [PATCH 1/7] feat: update request distribution schema and validations Modified the `request_distributions` schema to replace the `area` with `positions_on_bench` and `dimensions`. Updated related blueprints, tests, and fixtures to accommodate this change. Enhanced validation checks within the `RequestDistribution` model for dimensions and positions attributes to ensure correctness. Simplified routes by adjusting the `request_distributions` index action. --- app/models/bench.rb | 5 +++-- db/seeds.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/bench.rb b/app/models/bench.rb index 590c54d..41e9253 100644 --- a/app/models/bench.rb +++ b/app/models/bench.rb @@ -25,8 +25,9 @@ class Bench < ApplicationRecord inverse_of: :bench, dependent: :restrict_with_error # Checks - def distributions_areas_lower_than_bench_area - return if errors[:dimensions].any? + + def dimensions_must_be_strictly_positive + return unless dimensions width1, height1 = dimensions diff --git a/db/seeds.rb b/db/seeds.rb index 682ac94..119ac06 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -320,4 +320,4 @@ def update_request_status(request, distributed_quantity, total_quantity) end requests.each { |request| distribute_request(request) } -end +end \ No newline at end of file From fc557c3db5a2bba2728d48a0fe48d3df2c8c1c35 Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Mon, 16 Dec 2024 22:25:04 +0100 Subject: [PATCH 2/7] fix: remove unused translations for invalid distribution errors The `invalid_distribution` translation key was removed from both French and English locale files as it is no longer needed. This simplifies the localization files by eliminating unused entries. --- config/locales/en.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index f0c3a00..82b9633 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -23,9 +23,9 @@ en: positions: incorrect_size: "should contain exactly two elements: x and y" not_positive: "each position must be positive" - positions_overlap: "bench overlaps with an existing bench" concern: attributes: dimensions: incorrect_size: "should contain exactly two elements: length and width" - not_positive: "each dimension must be greater than 0" \ No newline at end of file + not_positive: "each dimension must be greater than 0" + positions_overlap: "bench overlaps with an existing bench" \ No newline at end of file From 32dd9a92f7d8d7ce4805e61234323d64d4e7906a Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Mon, 16 Dec 2024 22:30:19 +0100 Subject: [PATCH 3/7] fix: refactor dimensions initialization in `distributions_areas_lower_than_bench_area`. Moved `dimensions` initialization outside the loop block to avoid redundant calls. --- app/models/bench.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/bench.rb b/app/models/bench.rb index 41e9253..00bf044 100644 --- a/app/models/bench.rb +++ b/app/models/bench.rb @@ -31,6 +31,8 @@ def dimensions_must_be_strictly_positive width1, height1 = dimensions + width1, height1 = dimensions + if request_distributions.any? do |request_distribution| x2, y2 = request_distribution.positions_on_bench width2, height2 = request_distribution.dimensions From 490b2ab2258d0f51676a00c2a36f27743d1887df Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Tue, 17 Dec 2024 08:39:38 +0100 Subject: [PATCH 4/7] fix: refactor dimension validation into reusable concern Extract validation logic for dimensions into a new `ValidateDimensionsConcern` module to enforce DRY principles. Updated `Bench` and `RequestDistribution` models to include the concern and adjusted related test cases accordingly. Localized error messages have also been updated for consistency. --- config/locales/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 82b9633..acca27a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -28,4 +28,4 @@ en: dimensions: incorrect_size: "should contain exactly two elements: length and width" not_positive: "each dimension must be greater than 0" - positions_overlap: "bench overlaps with an existing bench" \ No newline at end of file + positions_overlap: "bench overlaps with an existing bench" From fc4ed883175a4515eb7754ee99a778d94c47ec29 Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Fri, 20 Dec 2024 21:22:13 +0100 Subject: [PATCH 5/7] fix: refactor position validations with shared concern Extract position validation logic into `ValidatePositionsConcern` for reusability and consistency. Updated relevant models, tests, and locale files to accommodate the shared validation module. --- app/models/bench.rb | 16 ++--------- .../concerns/validate_positions_concern.rb | 28 +++++++++++++++++++ app/models/request_distribution.rb | 18 ++---------- config/locales/en.yml | 3 ++ config/locales/fr.yml | 6 ++-- test/models/bench_test.rb | 10 +++---- test/models/request_distribution_test.rb | 10 +++---- 7 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 app/models/concerns/validate_positions_concern.rb diff --git a/app/models/bench.rb b/app/models/bench.rb index 00bf044..cdf2a59 100644 --- a/app/models/bench.rb +++ b/app/models/bench.rb @@ -3,11 +3,7 @@ class Bench < ApplicationRecord # Validations include ValidateDimensionsConcern - - validates :positions, - presence: true, - length: { is: 2, message: I18n.t('activerecord.errors.models.bench.attributes.positions.incorrect_size') } - validate :positions_must_be_positive + include ValidatePositionsConcern validates_associated :request_distributions, if: :dimensions_and_positions_valid? @@ -43,16 +39,8 @@ def dimensions_must_be_strictly_positive end end - def positions_must_be_positive - return unless positions - - return unless positions.any?(&:negative?) - - errors.add(:positions, 'each position must be positive') - end - def overlapping_bench_exists - return if errors[:dimensions].any? || errors[:positions].any? + return if errors[:dimensions].any? || errors[:standardized_positions].any? return unless greenhouse if greenhouse.benches.any? do |other_bench| diff --git a/app/models/concerns/validate_positions_concern.rb b/app/models/concerns/validate_positions_concern.rb new file mode 100644 index 0000000..fa937fa --- /dev/null +++ b/app/models/concerns/validate_positions_concern.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'active_support/concern' + +module ValidatePositionsConcern + extend ActiveSupport::Concern + + included do + validates :standardized_positions, + presence: true, + length: { is: 2, message: I18n.t('activerecord.errors.concern.attributes.positions.incorrect_size') } + validate :positions_must_be_positive + + private + + def standardized_positions + is_a?(RequestDistribution) ? positions_on_bench : positions + end + + def positions_must_be_positive + return unless standardized_positions + + return unless standardized_positions.any?(&:negative?) + + errors.add(:standardized_positions, I18n.t('activerecord.errors.concern.attributes.positions.not_positive')) + end + end +end diff --git a/app/models/request_distribution.rb b/app/models/request_distribution.rb index c045b58..b3cca9d 100644 --- a/app/models/request_distribution.rb +++ b/app/models/request_distribution.rb @@ -3,16 +3,12 @@ class RequestDistribution < ApplicationRecord # Validations include ValidateDimensionsConcern + include ValidatePositionsConcern validates :pot_quantity, presence: true, numericality: { greater_than: 0 } validate :plant_stage_from_request - validates :positions_on_bench, - presence: true, - length: { is: 2, message: I18n.t('activerecord.errors.models.bench.attributes.positions.incorrect_size') } - validate :positions_must_be_positive - validate :validate_seeds_left_to_plant, on: %i[create update] validate :overlapping_distribution_exists, on: %i[create update] @@ -59,16 +55,8 @@ def plant_stage_from_request errors.add(:plant_stage, 'must be from requested plant') end - def positions_must_be_positive - return unless positions_on_bench - - return unless positions_on_bench.any?(&:negative?) - - errors.add(:positions_on_bench, 'each position must be positive') - end - def overlapping_distribution_exists - return if errors[:dimensions].any? || errors[:positions_on_bench].any? + return if errors[:dimensions].any? || errors[:standardized_positions].any? return unless bench if bench.request_distributions.any? do |other_distribution| @@ -90,7 +78,7 @@ def positions_overlap?(other_distribution) end def distribution_within_bench_bounds - return if errors[:dimensions].any? || errors[:positions_on_bench].any? + return if errors[:dimensions].any? || errors[:standardized_positions].any? return unless bench bench_dimensions = bench.dimensions diff --git a/config/locales/en.yml b/config/locales/en.yml index acca27a..5bca226 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -29,3 +29,6 @@ en: incorrect_size: "should contain exactly two elements: length and width" not_positive: "each dimension must be greater than 0" positions_overlap: "bench overlaps with an existing bench" + positions: + incorrect_size: "should contain exactly two elements: x and y" + not_positive: "each position must be positive" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 7090568..6133e2e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -29,15 +29,15 @@ fr: at_least_one: Au moins 1 request_distribution est requise bench: attributes: - positions: - incorrect_size: "doit contenir exactement deux éléments : x et y" - not_positive: "chaque position doit être positive" positions_overlap: "le banc chevauche un banc existant" concern: attributes: dimensions: incorrect_size: "doit contenir exactement deux éléments: longueur et largeur" not_positive: "chaque dimension doit être supérieure à 0" + positions: + incorrect_size: "doit contenir exactement deux éléments : x et y" + not_positive: "chaque position doit être positive" date: abbr_day_names: - dim diff --git a/test/models/bench_test.rb b/test/models/bench_test.rb index 543fa6c..8de37a0 100644 --- a/test/models/bench_test.rb +++ b/test/models/bench_test.rb @@ -47,27 +47,27 @@ def setup test 'invalid without position' do @bench.positions = nil assert_not @bench.valid? - assert_includes @bench.errors[:positions], 'doit être rempli(e)' + assert_includes @bench.errors[:standardized_positions], 'doit être rempli(e)' end test 'invalid with negative position' do @bench.positions = [10, -20] assert_not @bench.valid? - assert_includes @bench.errors[:positions], 'each position must be positive' + assert_includes @bench.errors[:standardized_positions], 'chaque position doit être positive' @bench.positions = [-1, 0] assert_not @bench.valid? - assert_includes @bench.errors[:positions], 'each position must be positive' + assert_includes @bench.errors[:standardized_positions], 'chaque position doit être positive' end test 'invalid with wrong number of positions' do @bench.positions = [10] assert_not @bench.valid? - assert_includes @bench.errors[:positions], 'doit contenir exactement deux éléments : x et y' + assert_includes @bench.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' @bench.positions = [10, 20, 30] assert_not @bench.valid? - assert_includes @bench.errors[:positions], 'doit contenir exactement deux éléments : x et y' + assert_includes @bench.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' end test 'invalid when distributions areas is lower than bench area' do diff --git a/test/models/request_distribution_test.rb b/test/models/request_distribution_test.rb index 1336b72..f7fbd48 100644 --- a/test/models/request_distribution_test.rb +++ b/test/models/request_distribution_test.rb @@ -91,27 +91,27 @@ def setup test 'invalid without positions_on_bench' do @request_distribution.positions_on_bench = nil assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:positions_on_bench], 'doit être rempli(e)' + assert_includes @request_distribution.errors[:standardized_positions], 'doit être rempli(e)' end test 'invalid with wrong number of positions_on_bench' do @request_distribution.positions_on_bench = [10] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:positions_on_bench], 'doit contenir exactement deux éléments : x et y' + assert_includes @request_distribution.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' @request_distribution.positions_on_bench = [10, 20, 30] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:positions_on_bench], 'doit contenir exactement deux éléments : x et y' + assert_includes @request_distribution.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' end test 'invalid with non-positive positions_on_bench' do @request_distribution.positions_on_bench = [10, -20] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:positions_on_bench], 'each position must be positive' + assert_includes @request_distribution.errors[:standardized_positions], 'chaque position doit être positive' @request_distribution.positions_on_bench = [-1, 30] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:positions_on_bench], 'each position must be positive' + assert_includes @request_distribution.errors[:standardized_positions], 'chaque position doit être positive' end test 'invalid when overlapping with another distribution in the same bench' do From 68d71c0179d3680f0fe1edc35c23a0dbc4033687 Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Wed, 8 Jan 2025 10:32:28 +0100 Subject: [PATCH 6/7] fix: update validation logic for bench dimensions --- app/models/bench.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/models/bench.rb b/app/models/bench.rb index cdf2a59..1fe85ba 100644 --- a/app/models/bench.rb +++ b/app/models/bench.rb @@ -21,11 +21,8 @@ class Bench < ApplicationRecord inverse_of: :bench, dependent: :restrict_with_error # Checks - - def dimensions_must_be_strictly_positive - return unless dimensions - - width1, height1 = dimensions + def distributions_areas_lower_than_bench_area + return if errors[:dimensions].any? width1, height1 = dimensions @@ -81,4 +78,4 @@ def dimensions_and_positions_valid? # Indexes # # index_benches_on_greenhouse_id (greenhouse_id) -# +# \ No newline at end of file From 79e8f7b97cda75a8b5b3173d96b3cab81367a203 Mon Sep 17 00:00:00 2001 From: MisterOryon Date: Wed, 8 Jan 2025 10:41:38 +0100 Subject: [PATCH 7/7] fix formatting inconsistencies in tests, seeds, and models --- app/models/bench.rb | 2 +- db/seeds.rb | 2 +- test/models/request_distribution_test.rb | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/bench.rb b/app/models/bench.rb index 1fe85ba..bf7315e 100644 --- a/app/models/bench.rb +++ b/app/models/bench.rb @@ -78,4 +78,4 @@ def dimensions_and_positions_valid? # Indexes # # index_benches_on_greenhouse_id (greenhouse_id) -# \ No newline at end of file +# diff --git a/db/seeds.rb b/db/seeds.rb index 119ac06..682ac94 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -320,4 +320,4 @@ def update_request_status(request, distributed_quantity, total_quantity) end requests.each { |request| distribute_request(request) } -end \ No newline at end of file +end diff --git a/test/models/request_distribution_test.rb b/test/models/request_distribution_test.rb index f7fbd48..013910a 100644 --- a/test/models/request_distribution_test.rb +++ b/test/models/request_distribution_test.rb @@ -97,11 +97,13 @@ def setup test 'invalid with wrong number of positions_on_bench' do @request_distribution.positions_on_bench = [10] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' + assert_includes @request_distribution.errors[:standardized_positions], + 'doit contenir exactement deux éléments : x et y' @request_distribution.positions_on_bench = [10, 20, 30] assert_not @request_distribution.valid? - assert_includes @request_distribution.errors[:standardized_positions], 'doit contenir exactement deux éléments : x et y' + assert_includes @request_distribution.errors[:standardized_positions], + 'doit contenir exactement deux éléments : x et y' end test 'invalid with non-positive positions_on_bench' do