From b81ac70a1d99a6177413e8c5c9c92bb76756a607 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Wed, 8 Nov 2023 11:56:28 -0300 Subject: [PATCH] add active in period for the requirement --- app/models/requirement.rb | 2 ++ spec/models/requirement_spec.rb | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/app/models/requirement.rb b/app/models/requirement.rb index d015011..ae0dfe3 100644 --- a/app/models/requirement.rb +++ b/app/models/requirement.rb @@ -31,4 +31,6 @@ class Requirement < ApplicationRecord validates :coverage, presence: true validates :start_date, presence: true validates :end_date, presence: true + + scope :active_in_period, ->(start_date, end_date) { where('start_date <= ? AND end_date >= ?', end_date, start_date) } end diff --git a/spec/models/requirement_spec.rb b/spec/models/requirement_spec.rb index 05cecc9..3a304b7 100644 --- a/spec/models/requirement_spec.rb +++ b/spec/models/requirement_spec.rb @@ -35,4 +35,43 @@ it { should belong_to(:profession) } it { should have_many(:assignments) } end + + describe 'methods' do + context '#active_in_period' do + it 'should return true if dates are fully in the period' do + FactoryBot.create(:requirement, start_date: '2020-01-01', + end_date: '2020-01-31') + + expect(Requirement.active_in_period('2020-01-05', '2020-01-28').length).to eq(1) + end + + it 'should return true if start date is in the period but the end date is out' do + FactoryBot.create(:requirement, start_date: '2020-01-01', + end_date: '2020-01-31') + + expect(Requirement.active_in_period('2020-01-05', '2020-02-28').length).to eq(1) + end + + it 'should return true if start date is out of the period but the end date is in' do + FactoryBot.create(:requirement, start_date: '2020-01-01', + end_date: '2020-01-31') + + expect(Requirement.active_in_period('2019-01-05', '2020-02-28').length).to eq(1) + end + + it 'should return true if both dates cover the start and end date' do + FactoryBot.create(:requirement, start_date: '2020-01-01', + end_date: '2020-01-31') + + expect(Requirement.active_in_period('2019-01-05', '2021-02-28').length).to eq(1) + end + + it 'should return false if dates are fully out of the period' do + FactoryBot.create(:requirement, start_date: '2020-01-01', + end_date: '2020-01-31') + + expect(Requirement.active_in_period('2020-02-01', '2020-02-28').length).to eq(0) + end + end + end end