-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d5d4e3
commit 3d8d8cf
Showing
3 changed files
with
92 additions
and
45 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
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 |
---|---|---|
@@ -1,24 +1,43 @@ | ||
# Validators are instantiated once, instance variables persist between validations, might cause bugs | ||
class RentNumberValidator < ActiveModel::Validator | ||
def validate(record) | ||
#Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) | ||
return if record.number.nil? || record.begin.nil? || record.end.nil? | ||
sum = 0 | ||
rents = Rent.all | ||
rents.each do |rent| | ||
sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") | ||
end | ||
if sum+record.number > record.item.number | ||
record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." | ||
record.errors.add :base, "A megadott napokra elérhető eszközök száma:" | ||
day = record.begin.to_date | ||
while(day <= record.end.to_date) | ||
sum = 0 | ||
rents.each do |rent| | ||
sum += rent.number if(rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") | ||
end | ||
record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" | ||
day += 1 | ||
end | ||
end | ||
def validate(record) | ||
item = record.item | ||
active_rents = active_rents(record) | ||
rent_days = rent_days(active_rents) | ||
rents_by_item = rents_by_item(rent_days, active_rents) | ||
|
||
record.days.each do |day| | ||
rent_number_for_day = rents_by_item.dig(day, item) || 0 | ||
if rent_number_for_day + record.number > item.number | ||
available_item_number = item.number = rent_number_for_day | ||
record.errors.add :base, "A kiválasztott napon(#{day}) csak #{available_item_number} darab #{item.name} elérhető." | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# hash[rent_day][item] = sum taken out amount item (per day, per item) | ||
def rents_by_item(rent_days, active_rents) | ||
result = {} | ||
rent_days.each do |rent_date| | ||
result[rent_date] = {} if result[rent_date].nil? | ||
active_rents.each do |active_rent| | ||
result[rent_date][active_rent.item] = 0 if result[rent_date][active_rent.item].nil? | ||
result[rent_date][active_rent.item] += active_rent.number | ||
end | ||
end | ||
result | ||
end | ||
|
||
def rent_days(active_rents) | ||
active_rents.map(&:days).flatten.uniq.sort | ||
end | ||
|
||
def active_rents(record) | ||
Rent.includes(:item) | ||
.where(state: [:approved, :taken]) | ||
.where.not('rents.end <= :begin or :end <= rents.begin', | ||
{ begin: record.begin, end: record.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