Commit b67daacb authored by Alexandre Friquet's avatar Alexandre Friquet

Fixes bug with season's selector: refs #97.

parent 0105f357
......@@ -43,14 +43,16 @@ class Season
end
def self.all(klass, date_field=:begin_at)
min_year = klass.send(:minimum, date_field).try(:year)
max_year = klass.send(:maximum, date_field).try(:year)
return [] unless min_year && max_year
min_date = klass.send(:minimum, date_field)
max_date = klass.send(:maximum, date_field)
return [] unless min_date && max_date
[].tap do |years|
(min_year..max_year).each do |year|
season = self.new(self.birthdate.change(year: year))
years << season.short_label
[].tap do |seasons|
date = min_date
loop do
seasons << self.new(date).short_label
date += 1.year
break if date > max_date
end
end
end
......
......@@ -112,12 +112,25 @@ RSpec.describe Season, type: :model do
expect(result).to eq expected
end
it "returns only one if all data within same season" do
# Given
create :event, begin_at: Date.new(2017,9,1)
create :event, begin_at: Date.new(2017,12,18)
create :event, begin_at: Date.new(2018,5,19)
create :event, begin_at: Date.new(2018,8,31)
expected = ["2017-2018"]
# When
result = Season.all(Event)
# Then
expect(result).to eq expected
end
it "based on :begin_at if no field provided" do
# Given
create :event, begin_at: Date.new(2016,9,1)
create :event, begin_at: Date.new(2017,4,1)
create :event, begin_at: Date.new(2017,9,1)
create :event, begin_at: Date.new(2018,9,1)
expected = ["2016-2017","2017-2018","2018-2019"]
create :event, begin_at: Date.new(2018,8,31)
expected = ["2016-2017","2017-2018"]
# When
result = Season.all(Event)
# Then
......@@ -126,10 +139,10 @@ RSpec.describe Season, type: :model do
it "based on the provided field" do
# Given
create :subscription, created_at: Date.new(2016,9,1)
create :subscription, created_at: Date.new(2017,4,1)
create :subscription, created_at: Date.new(2017,9,1)
create :subscription, created_at: Date.new(2018,9,1)
expected = ["2016-2017","2017-2018","2018-2019"]
create :subscription, created_at: Date.new(2018,8,31)
expected = ["2016-2017","2017-2018"]
# When
result = Season.all(Subscription, :created_at)
# Then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment