Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions app/models/bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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|
Expand Down
28 changes: 28 additions & 0 deletions app/models/concerns/validate_positions_concern.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 3 additions & 15 deletions app/models/request_distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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"
6 changes: 3 additions & 3 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/models/bench_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions test/models/request_distribution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading