Skip to content

Commit

Permalink
add active in period for the statement of work
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Nov 8, 2023
1 parent 7b36a38 commit 8470df1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/statement_of_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class StatementOfWork < ApplicationRecord
# custom validation for date range
validate :validate_date_range

scope :active_in_period, ->(start_date, end_date) { where('start_date <= ? AND end_date >= ?', end_date, start_date) }

private

def validate_date_range
Expand Down
39 changes: 39 additions & 0 deletions spec/models/statement_of_work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,43 @@
it { should have_many(:requirements) }
it { should have_many(:time_entries) }
end

describe 'methods' do
context '#active_in_period' do
it 'should return true if dates are fully in the period' do
FactoryBot.create(:statement_of_work, :with_maintenance, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(StatementOfWork.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(:statement_of_work, :with_maintenance, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(StatementOfWork.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(:statement_of_work, :with_maintenance, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(StatementOfWork.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(:statement_of_work, :with_maintenance, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(StatementOfWork.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(:statement_of_work, :with_maintenance, start_date: '2020-01-01',
end_date: '2020-01-31')

expect(StatementOfWork.active_in_period('2020-02-01', '2020-02-28').length).to eq(0)
end
end
end
end

0 comments on commit 8470df1

Please sign in to comment.