Skip to content

Commit

Permalink
Fixes AP period check
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice TEXIER committed Mar 22, 2016
1 parent 1450764 commit a5fda31
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -108,7 +108,7 @@ GEM
activesupport (>= 3.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
cocoon (1.2.8)
cocoon (1.2.9)
code_analyzer (0.4.5)
sexp_processor
code_string (0.0.0)
Expand Down Expand Up @@ -291,7 +291,7 @@ GEM
ref (2.0.0)
remotipart (1.2.1)
require_all (1.3.3)
responders (2.1.1)
responders (2.1.2)
railties (>= 4.2.0, < 5.1)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
Expand Down
1 change: 0 additions & 1 deletion app/controllers/backend/workers_controller.rb
Expand Up @@ -18,7 +18,6 @@

module Backend
class WorkersController < Backend::ProductsController

list do |t|
t.action :edit
t.action :destroy, if: :destroyable?
Expand Down
15 changes: 14 additions & 1 deletion app/models/activity_production.rb
Expand Up @@ -69,7 +69,7 @@ class ActivityProduction < Ekylibre::Record::Base
composed_of :size, class_name: 'Measure', mapping: [%w(size_value to_d), %w(size_unit_name unit)]

# [VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
validates_date :started_on, :stopped_on, allow_blank: true, before: Date.today + 30.years
validates_date :started_on, :stopped_on, allow_blank: true, on_or_after: Date.civil(1, 1, 1)
validates_numericality_of :rank_number, allow_nil: true, only_integer: true
validates_numericality_of :size_value, allow_nil: true
validates_inclusion_of :irrigated, :nitrate_fixing, in: [true, false]
Expand Down Expand Up @@ -189,6 +189,19 @@ class ActivityProduction < Ekylibre::Record::Base
end

validate do
min = Date.civil(1900, 1, 1)
max = Date.today >> (50 * 12)
if self.started_on
errors.add(:started_on, :anterior, to: max) if self.started_on > max
errors.add(:started_on, :posterior, to: min) if self.started_on < min
end
if self.stopped_on
errors.add(:stopped_on, :anterior, to: max) if self.stopped_on > max
errors.add(:stopped_on, :posterior, to: min) if self.stopped_on < min
end
if self.started_on && self.stopped_on
errors.add(:stopped_on, :posterior, to: self.started_on) if self.stopped_on < self.started_on
end
if plant_farming?
errors.add(:support_shape, :empty) if self.support_shape && self.support_shape.empty?
end
Expand Down
12 changes: 11 additions & 1 deletion test/models/activity_production_test.rb
Expand Up @@ -49,5 +49,15 @@
require 'test_helper'

class ActivityProductionTest < ActiveSupport::TestCase
# Add tests here...
test 'create' do
activity = Activity.find_by(production_cycle: :annual, family: 'plant_farming')
p = activity.productions.new(started_on: '2015-07-01', stopped_on: '2016-01-15', campaign: Campaign.of(2016), cultivable_zone: CultivableZone.first)
assert p.save, p.errors.inspect
p = activity.productions.new(started_on: '2015-07-01', stopped_on: '72016-01-15', campaign: Campaign.of(2016), cultivable_zone: CultivableZone.first)
assert !p.save, p.errors.inspect
p = activity.productions.new(started_on: '15-07-01', stopped_on: '2016-01-15', campaign: Campaign.of(2016), cultivable_zone: CultivableZone.first)
assert !p.save, p.errors.inspect
p = activity.productions.new(started_on: '2017-07-01', stopped_on: '2016-01-15', campaign: Campaign.of(2016), cultivable_zone: CultivableZone.first)
assert !p.save, p.errors.inspect
end
end

0 comments on commit a5fda31

Please sign in to comment.