diff --git a/app/models/bench.rb b/app/models/bench.rb index 590c54d..bf7315e 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? @@ -40,16 +36,8 @@ def distributions_areas_lower_than_bench_area 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 f0c3a00..5bca226 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -23,9 +23,12 @@ 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" + 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..013910a 100644 --- a/test/models/request_distribution_test.rb +++ b/test/models/request_distribution_test.rb @@ -91,27 +91,29 @@ 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