Commit 27d551d2 authored by Alexandre Friquet's avatar Alexandre Friquet

Use new season birthdate for event types and activities: refs #89.

parent b2dab4e4
...@@ -47,7 +47,7 @@ class Activity < ApplicationRecord ...@@ -47,7 +47,7 @@ class Activity < ApplicationRecord
result = {} result = {}
self.events.visible.chronology.each do |event| self.events.visible.chronology.each do |event|
year = event.begin_at.year year = event.begin_at.year
if event.begin_at >= Date.new(year,8,15) if event.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event (result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event
else else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event (result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event
......
...@@ -57,7 +57,7 @@ class EventType < ApplicationRecord ...@@ -57,7 +57,7 @@ class EventType < ApplicationRecord
result = {} result = {}
self.events.with_dates.visible.reverse_chronology.each do |event| self.events.with_dates.visible.reverse_chronology.each do |event|
year = event.begin_at.year year = event.begin_at.year
if event.begin_at >= Date.new(year,8,15) if event.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event (result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event
else else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event (result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event
......
...@@ -28,9 +28,13 @@ class Season ...@@ -28,9 +28,13 @@ class Season
Date.new(1970,FIRST_MONTH, FIRST_DAY) Date.new(1970,FIRST_MONTH, FIRST_DAY)
end end
def self.birthdate
Settings.instance.reload.season_birthdate || self.default_birthdate
end
def initialize(date) def initialize(date)
year = (date < Date.new(date.year, FIRST_MONTH, FIRST_DAY) ? date.year-1 : date.year) year = (date < Season.birthdate.change(year: date.year) ? date.year-1 : date.year)
@begining = Date.new(year, FIRST_MONTH, FIRST_DAY) @begining = Season.birthdate.change(year: year)
return self return self
end end
......
...@@ -23,19 +23,73 @@ ...@@ -23,19 +23,73 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Season, type: :model do RSpec.describe Season, type: :model do
context "regarding initialization" do
context "sets the begining ot the season to" do context "class methods" do
scenario "15/08/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19)) it ".default_birthdate set to 15/08/1970" do
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15) # Given
# When
result = Season.default_birthdate
# Then
expect(result).to eq Date.new(1970,8,15)
end
context ".birthdate" do
it "return 15/08/1970 when season birthdate not provided" do
# Given
# When
result = Season.birthdate
# Then
expect(result).to eq Date.new(1970,8,15)
end end
scenario "15/08/2017 when date 15/08/2017 is given" do
season = Season.new(Date.new(2017,8,15)) it "returns season birthdate when provided" do
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15) # Given
first_of_september = Date.new(1970,9,1)
@settings.update_attribute(:season_birthdate, first_of_september)
# When
result = Season.birthdate
# Then
expect(result).to eq first_of_september
end end
scenario "15/08/2016 when date 14/08/2017 is given" do end
season = Season.new(Date.new(2017,8,14)) end
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,8,15)
context "regarding initialization" do
context "when season birthdate not provided" do
context "sets the begining ot the season to" do
scenario "15/08/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
end
scenario "15/08/2017 when date 15/08/2017 is given" do
season = Season.new(Date.new(2017,8,15))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
end
scenario "15/08/2016 when date 14/08/2017 is given" do
season = Season.new(Date.new(2017,8,14))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,8,15)
end
end
end
context "when season birthdate set to 01/09/1970" do
before { @settings.update_attribute(:season_birthdate, Date.new(1970,9,1)) }
context "sets the begining ot the season to" do
scenario "01/09/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,9,1)
end
scenario "01/09/2017 when date 01/09/2017 is given" do
season = Season.new(Date.new(2017,9,1))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,9,1)
end
scenario "01/09/2016 when date 31/08/2017 is given" do
season = Season.new(Date.new(2017,8,31))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,9,1)
end
end end
end end
end end
......
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