Commit 884a15be authored by Alexandre Friquet's avatar Alexandre Friquet

Refactor seasoned events to concern: refs #4.

parent 27d551d2
......@@ -21,6 +21,8 @@
# along with OLIGAA. If not, see <http://www.gnu.org/licenses/>.
#
class Activity < ApplicationRecord
include SeasonedEventsConcern
has_and_belongs_to_many :activity_categories, :join_table => :activities_cats
has_and_belongs_to_many :events
has_and_belongs_to_many :members
......@@ -42,18 +44,4 @@ class Activity < ApplicationRecord
def self.uncategorized
all.reject { |act| !act.activity_categories.blank? }
end
def seasoned_events
result = {}
self.events.visible.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
end
end
module SeasonedEventsConcern
extend ActiveSupport::Concern
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
end
end
end
......@@ -21,6 +21,8 @@
# along with OLIGAA. If not, see <http://www.gnu.org/licenses/>.
#
class EventType < ApplicationRecord
include SeasonedEventsConcern
has_many :events
validates_presence_of :title
......@@ -52,18 +54,4 @@ class EventType < ApplicationRecord
def ga_cal
GoogleAgenda::Calendar.find_by_id_or_create(self.ga_cal_id)
end
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
end
end
......@@ -69,13 +69,6 @@ describe Activity do
end
describe "behaviors" do
it "#seasoned_events returns associated events by season" do
activity = create :activity
event1 = create :event, begin_at: "01/08/2013", activities: [activity]
event2 = create :event, begin_at: "01/09/2013", activities: [activity]
event3 = create :event, begin_at: "02/09/2013", activities: [activity], goto_trash: true
expected = [["Saison 2013-2014", [event2]], ["Saison 2012-2013", [event1]]]
expect(activity.seasoned_events).to eq expected
end
it_behaves_like "seasoned_events"
end
end
......@@ -35,16 +35,8 @@ describe EventType do
end
describe "behaviors" do
it "#seasoned_events returns seasons with corresponding events" do
et = create :event_type
ev1 = create :event, begin_at: nil, end_at: nil
ev2 = create :event, begin_at: "02/07/2013 09:00", end_at: "02/07/2013 11:00"
ev3 = create :event, begin_at: "02/07/2012 09:00", end_at: "02/07/2012 11:00"
ev4 = create :event, begin_at: "02/01/2012 09:00", end_at: "02/01/2012 11:00"
et.events << [ev1, ev2, ev3, ev4]
expected = [["Saison 2012-2013", [ev2]], ["Saison 2011-2012", [ev3, ev4]]]
expect(et.seasoned_events).to eq expected
end
it_behaves_like "seasoned_events"
describe '.ga_cal' do
......
require 'rails_helper'
shared_examples_for "seasoned_events" do
let(:factory) { described_class.name.underscore.to_sym }
it "#seasoned_events returns seasons with corresponding events" do
model = create factory
ev1 = create :event, begin_at: nil, end_at: nil
ev2 = create :event, begin_at: "02/07/2013 09:00", end_at: "02/07/2013 11:00"
ev3 = create :event, begin_at: "02/07/2012 09:00", end_at: "02/07/2012 11:00"
ev4 = create :event, begin_at: "02/01/2012 09:00", end_at: "02/01/2012 11:00"
model.events << [ev1, ev2, ev3, ev4]
expected = [["Saison 2012-2013", [ev2]], ["Saison 2011-2012", [ev3, ev4]]]
expect(model.seasoned_events).to eq expected
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