Commit f6769c68 authored by Alexandre Friquet's avatar Alexandre Friquet

Group passed events by season: refs #4.

parent 884a15be
......@@ -25,6 +25,7 @@ class EventsController < ApplicationController
def index
@passed_events = Event.reverse_chronology.visible.passed
@seasoned_passed_events = Season.all_for(@passed_events)
@current_events = Event.reverse_chronology.visible.current
@events_to_come = Event.chronology.visible.to_come
@unscheduled_events = Event.visible.unscheduled
......
......@@ -4,16 +4,7 @@ module SeasonedEventsConcern
included do
def seasoned_events
result = {}
self.events.with_dates.visible.reverse_chronology.each do |event|
year = event.begin_at.year
if event.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event
else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event
end
end
result.sort.reverse
Season.all_for(self.events.with_dates.visible.reverse_chronology)
end
end
end
......@@ -32,6 +32,19 @@ class Season
Settings.instance.reload.season_birthdate || self.default_birthdate
end
def self.all_for(items)
result = {}
items.each do |item|
year = item.begin_at.year
if item.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << item
else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << item
end
end
result.sort.reverse
end
def initialize(date)
year = (date < Season.birthdate.change(year: date.year) ? date.year-1 : date.year)
@begining = Season.birthdate.change(year: year)
......
......@@ -67,7 +67,15 @@
<%= toggle_link "Passés (#{@passed_events.count})", '#time-elapsed' %>
</h2>
<div id="time-elapsed" style="display:none;">
<%= render :partial => 'event', :collection => @passed_events %>
<%- @seasoned_passed_events.each do |season, events| %>
<div>
<% season_label = season.downcase.gsub(/\s/, '_') %>
<%= toggle_link "#{ season } (#{ events.length })", "##{season_label}" %>
<div id="<%= season_label %>" style="display:none;">
<%= render :partial => 'event', :collection => events %>
</div>
</div>
<%- end %>
</div>
</li>
<% end %>
......
......@@ -53,6 +53,23 @@ RSpec.describe Season, type: :model do
expect(result).to eq first_of_september
end
end
it ".all_for returns items arranged by season" do
# Given
allow(Settings).to receive_message_chain(:instance, :reload, :season_birthdate).and_return(Date.new(1970,9,1))
Item = Struct.new(:begin_at)
item1 = Item.new(Date.new(2018,8,31))
item2 = Item.new(Date.new(2018,9,1))
item3 = Item.new(Date.new(2018,9,2))
expected = [
["Saison 2018-2019", [item2, item3]],
["Saison 2017-2018", [item1]]
]
# When
result = Season.all_for([ item1, item2, item3 ])
# Then
expect(result).to eq expected
end
end
context "regarding initialization" do
......
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