Skip to content

Commit

Permalink
Merge branch 'bugfixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
leomurta committed Jun 5, 2019
2 parents 997544e + deba3dd commit ae8746e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
32 changes: 25 additions & 7 deletions app/models/scholarship_duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,36 @@ class ScholarshipDuration < ApplicationRecord
# #validates if a scholarship duration start date isn't before it's end date
validates_date :start_date, :on_or_before => :end_date, :on_or_before_message => I18n.t("activerecord.errors.models.scholarship_duration.attributes.start_date_after_end_date")
#
# #validates if a scholarship duration isn't invalid according to the selected scholarship
validates_date :start_date, :on_or_after => :scholarship_start_date, :on_or_after_message => I18n.t("activerecord.errors.models.scholarship_duration.start_date_before_scholarship_start_date")
validates_date :start_date, :on_or_before => :scholarship_end_date, :on_or_before_message => I18n.t("activerecord.errors.models.scholarship_duration.start_date_after_scholarship_end_date")
validates_date :end_date, :on_or_after => :scholarship_start_date, :on_or_after_message => I18n.t("activerecord.errors.models.scholarship_duration.end_date_before_scholarship_start_date")
validates_date :end_date, :on_or_before => :scholarship_end_date, :on_or_before_message => I18n.t("activerecord.errors.models.scholarship_duration.end_date_after_scholarship_end_date")
#
# #validates if a cancel date of an scholarship duration is valid
validates_date :cancel_date, :on_or_before => :end_date, :allow_nil => true
validates_date :cancel_date, :on_or_before => :end_date, :allow_nil => true, :on_or_before_message => I18n.t("activerecord.errors.models.scholarship_duration.cancel_date_after_end_date")
validates_date :cancel_date, :on_or_after => :start_date, :allow_nil => true

validate :check_date_boundaries_of_scholarship

before_save :update_end_and_cancel_dates

def day_of(date)
date.strftime("%Y%m%d").to_i
end

def check_date_boundaries_of_scholarship
if (not start_date.nil?) && (not scholarship.start_date.nil?)
if day_of(start_date) < day_of(scholarship.start_date)
errors.add(:start_date, I18n.t("activerecord.errors.models.scholarship_duration.start_date_before_scholarship_start_date"))
end
end

if not scholarship.end_date.nil?
if not cancel_date.nil?
if day_of(cancel_date) > day_of(scholarship.end_date)
errors.add(:cancel_date, I18n.t("activerecord.errors.models.scholarship_duration.cancel_date_after_scholarship_end_date"))
end
elsif (not end_date.nil?) && (day_of(end_date) > day_of(scholarship.end_date))
errors.add(:end_date, I18n.t("activerecord.errors.models.scholarship_duration.end_date_after_scholarship_end_date"))
end
end
end

def init
self.start_date = Date.today.beginning_of_month + 1.month if self.start_date.nil?
self.update_end_date
Expand Down
3 changes: 2 additions & 1 deletion config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ pt-BR:
scholarship_duration:
the_levels_of_scholarship_and_enrollment_are_different: "não pode ser alocado a esta Matrícula porque eles são de níveis diferentes"
end_date_after_scholarship_end_date: "posterior a Data de Fim de Bolsa"
cancel_date_after_scholarship_end_date: "posterior a Data de Fim de Bolsa"
end_date_before_scholarship_start_date: "anterior a Data de Início de Bolsa"
cancel_date_after_end_date: "posterior a Data Limite de Concessão"
enrollment_and_scholarship_uniqueness: "já possui uma Bolsa alocada nesse período"
scholarship_start_date_after_end_or_cancel_date: "posterior a Data de Início de outra alocação de bolsa"
start_date_after_scholarship_end_date: "da alocação de bolsa deve ser posterior a data de limite de concessão de uma alocação que não tem data de encerramento"
Expand All @@ -545,7 +547,6 @@ pt-BR:
start_date_after_end_date: "posterior a Data de Limite de Concessão"
cancel_date:
on_or_after: "anterior a Data de Início"
on_or_before: "posterior a Data Limite de Concessão"
scholarship_suspension:
suspension_start_date_after_end_date: "posterior a Data de Fim"
suspension_start_date_before_scholarship_start_date: "anterior a Data de Início da Alocação"
Expand Down
14 changes: 14 additions & 0 deletions spec/models/scholarship_duration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@
end
end
end
describe "end_date" do
context "is valid and greater than scholarship.end_date" do
it "there must be a cancel_date less than or equal scholarship.end_date" do
enrollment = FactoryGirl.create(:enrollment)
scholarship = FactoryGirl.create(:scholarship, :start_date => start_date, :end_date => end_date)
scholarship_duration.enrollment = enrollment
scholarship_duration.scholarship = scholarship
scholarship_duration.start_date = scholarship.start_date
scholarship_duration.end_date = scholarship.end_date + 1.day
scholarship_duration.cancel_date = scholarship.end_date
scholarship_duration.should have(0).errors_on :end_date
end
end
end
describe "if_scholarship_is_not_with_another_student" do
context "should return the expected error when" do
it "has an old scholarship that ends after the new scholarship starts" do
Expand Down
35 changes: 34 additions & 1 deletion spec/models/scholarship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,40 @@
end
end
end
describe "start_date" do
context "is after scholarship_duration.start_date" do
scholarship_duration = ScholarshipDuration.new
it "should return an error message" do
scholarship.start_date = Date.today
scholarship_duration.scholarship = scholarship
scholarship_duration.start_date = scholarship.start_date - 1.day
scholarship.validate
expect(scholarship_duration).to have_error(:start_date_before_scholarship_start_date).on :start_date
end
end
end
describe "end_date" do
context "is before scholarship_duration.end_date and scholarship.cancel_date is nil" do
scholarship_duration = ScholarshipDuration.new
it "should return an error message" do
scholarship.end_date = Date.today
scholarship_duration.scholarship = scholarship
scholarship_duration.end_date = scholarship.end_date + 1.day
scholarship_duration.cancel_date = nil
scholarship.validate
expect(scholarship_duration).to have_error(:end_date_after_scholarship_end_date).on :end_date
end
end
context "is before scholarship_duration.cancel_date" do
scholarship_duration = ScholarshipDuration.new
it "should return an error message" do
scholarship.end_date = Date.today
scholarship_duration.scholarship = scholarship
scholarship_duration.cancel_date = scholarship.end_date + 1.day
scholarship.validate
expect(scholarship_duration).to have_error(:cancel_date_after_scholarship_end_date).on :cancel_date
end
end
context "should be valid when" do
it "end_date is greater than start_date" do
scholarship.end_date = Date.today
Expand Down Expand Up @@ -100,4 +133,4 @@
end
end
end
end
end

0 comments on commit ae8746e

Please sign in to comment.