-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* validation for frquency and allocations * rubocop fix * spec fix and review comment * validation for carry forward days * fix failing specs * rubocop fixes * fixed allocation_value erro * refactored to use concerns --------- Co-authored-by: “Apoorv <“tiwari.apoorv1316@gmail.com”>
- Loading branch information
1 parent
7184262
commit 6211ff1
Showing
7 changed files
with
263 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
module LeaveTypeValidatable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
validate :valid_allocation_combination | ||
validate :valid_allocation_value | ||
validate :valid_carry_forward | ||
end | ||
|
||
private | ||
|
||
def valid_allocation_combination | ||
if allocation_period == "weeks" && allocation_frequency == "per_week" | ||
errors.add(:base, "Invalid combination: Allocation period in weeks cannot have frequency per week") | ||
end | ||
|
||
if allocation_period == "months" && !(allocation_frequency == "per_quarter" || allocation_frequency == "per_year") | ||
errors.add( | ||
:base, | ||
"Invalid combination: Allocation period in months can only have frequency per quarter or per year") | ||
end | ||
end | ||
|
||
def valid_allocation_value | ||
max_values = { | ||
["days", "per_week"] => 7, | ||
["days", "per_month"] => 31, | ||
["days", "per_quarter"] => 92, | ||
["days", "per_year"] => 366, | ||
["weeks", "per_month"] => 5, | ||
["weeks", "per_quarter"] => 13, | ||
["weeks", "per_year"] => 52, | ||
["months", "per_quarter"] => 3, | ||
["months", "per_year"] => 12 | ||
} | ||
|
||
if allocation_value.present? | ||
key = [allocation_period, allocation_frequency] | ||
if max_values[key] && allocation_value > max_values[key] | ||
errors.add( | ||
:allocation_value, | ||
"cannot exceed #{max_values[key]} #{allocation_period} for #{allocation_frequency} frequency") | ||
end | ||
end | ||
end | ||
|
||
def valid_carry_forward | ||
total_days = convert_allocation_to_days | ||
|
||
if carry_forward_days.present? && total_days.present? && carry_forward_days > total_days | ||
errors.add(:carry_forward_days, "cannot exceed the total allocated days") | ||
end | ||
end | ||
|
||
def convert_allocation_to_days | ||
return nil unless allocation_value | ||
|
||
base_days = case allocation_period | ||
when "days" | ||
allocation_value | ||
when "weeks" | ||
allocation_value * 7 | ||
when "months" | ||
allocation_value * 31 | ||
else | ||
return nil | ||
end | ||
|
||
case allocation_frequency | ||
when "per_week" | ||
base_days * 52 | ||
when "per_month" | ||
base_days * 12 | ||
when "per_quarter" | ||
base_days * 4 | ||
when "per_year" | ||
base_days | ||
else | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe LeaveTypeValidatable, type: :model do | ||
let(:leave) { create(:leave) } | ||
|
||
describe "Custom Validations" do | ||
context "when validating allocation combinations" do | ||
it "is not valid with weekly allocation period and weekly frequency" do | ||
leave_type = build(:leave_type, allocation_period: "weeks", allocation_frequency: "per_week", leave:) | ||
expect(leave_type.valid?).to be false | ||
expect(leave_type.errors[:base]).to include( | ||
"Invalid combination: Allocation period in weeks cannot have frequency per week") | ||
end | ||
|
||
it "is not valid with monthly allocation period and weekly or monthly frequencies" do | ||
["per_week", "per_month"].each do |freq| | ||
leave_type = build(:leave_type, allocation_period: "months", allocation_frequency: freq, leave:) | ||
expect(leave_type.valid?).to be false | ||
expect(leave_type.errors[:base]).to include( | ||
"Invalid combination: Allocation period in months can only have frequency per quarter or per year") | ||
end | ||
end | ||
|
||
it "is valid with monthly allocation period and quarterly or yearly frequencies" do | ||
["per_quarter", "per_year"].each do |freq| | ||
leave_type = build( | ||
:leave_type, allocation_period: "months", allocation_frequency: freq, allocation_value: 2, | ||
leave:) | ||
expect(leave_type.valid?).to be true | ||
end | ||
end | ||
end | ||
|
||
context "when validating allocation values" do | ||
it "is not valid with an allocation value exceeding the limit for the period and frequency" do | ||
combinations = { | ||
["days", "per_week"] => 8, | ||
["weeks", "per_month"] => 6 | ||
} | ||
combinations.each do |(period, freq), value| | ||
leave_type = build( | ||
:leave_type, allocation_period: period, allocation_frequency: freq, | ||
allocation_value: value, leave:) | ||
expect(leave_type).not_to be_valid | ||
expect(leave_type.errors[:allocation_value]).to include( | ||
"cannot exceed #{value - 1} #{period} for #{freq} frequency") | ||
end | ||
end | ||
|
||
it "is valid with an allocation value within the limit for the period and frequency" do | ||
combinations = { | ||
["days", "per_week"] => 7, | ||
["weeks", "per_month"] => 5 | ||
} | ||
combinations.each do |(period, freq), value| | ||
leave_type = build( | ||
:leave_type, allocation_period: period, allocation_frequency: freq, | ||
allocation_value: value, leave:) | ||
expect(leave_type).to be_valid | ||
end | ||
end | ||
end | ||
|
||
context "when validating carry forward limits with frequency considerations" do | ||
it "is valid when carry_forward is less than total days in a year for days per week" do | ||
leave_type = build( | ||
:leave_type, allocation_period: "days", allocation_frequency: "per_week", | ||
allocation_value: 4, carry_forward_days: 5, leave:) | ||
expect(leave_type).to be_valid | ||
end | ||
|
||
it "is not valid when carry_forward exceeds total days for weeks per year" do | ||
leave_type = build( | ||
:leave_type, allocation_period: "weeks", allocation_frequency: "per_year", | ||
allocation_value: 2, carry_forward_days: 15, leave:) | ||
expect(leave_type).not_to be_valid | ||
expect(leave_type.errors[:carry_forward_days]).to include("cannot exceed the total allocated days") | ||
end | ||
|
||
it "is valid when carry_forward does not exceed total days for months per quarter" do | ||
leave_type = build( | ||
:leave_type, allocation_period: "months", allocation_frequency: "per_quarter", | ||
allocation_value: 1, carry_forward_days: 30, leave:) | ||
expect(leave_type).to be_valid | ||
end | ||
|
||
it "is not valid when carry_forward exceeds total days for months per year" do | ||
leave_type = build( | ||
:leave_type, allocation_period: "months", allocation_frequency: "per_year", | ||
allocation_value: 2, carry_forward_days: 63, leave:) | ||
expect(leave_type).not_to be_valid | ||
expect(leave_type.errors[:carry_forward_days]).to include("cannot exceed the total allocated days") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters