It is now possible to repeat events using a new méthod: nth day of the month

This commit is contained in:
echarp 2016-09-17 17:19:41 +02:00
parent ab195d28ae
commit 3069dced13
6 changed files with 35 additions and 16 deletions

View File

@ -1,10 +1,12 @@
require 'schedule'
# This is the central ADL class, where are managed all events
class Event < ActiveRecord::Base
extend SimpleCalendar
include Schedule
strip_attributes
has_paper_trail ignore: [:last_updated, :lock_version, :secret,
:submitter, :decision_time,
:latitude, :longitude]
has_paper_trail ignore: [:last_updated, :lock_version, :secret, :submitter,
:decision_time, :latitude, :longitude]
belongs_to :region
# This is the scheduled first event
@ -14,7 +16,7 @@ class Event < ActiveRecord::Base
validates :title, presence: true
validate :end_after_start
RULES = %w(daily weekly monthly).freeze
RULES = %w(daily weekly monthly monthly_day).freeze
validates :rule, allow_nil: true, inclusion: RULES
validates :description, presence: true
validates :city, presence: true
@ -94,12 +96,6 @@ class Event < ActiveRecord::Base
[address, city].compact.join ', '
end
def schedule
IceCube::Schedule.new(start_time, end_time: end_time) do |s|
s.add_recurrence_rule IceCube::Rule.send(rule).count(repeat + 1)
end
end
def hashtags
tags.split.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
end

View File

@ -7,4 +7,4 @@
%strong.city{ title: event.address }= event.city
= event.title
- if event.repeat > 0
%em.fa.fa-repeat(title="#{event.repeat} - #{t event.rule, scope: 'activerecord.attributes.event.rule_values'}")
%em.fa.fa-repeat{ title: event.schedule }

View File

@ -118,9 +118,10 @@
- if @event.repeat > 0
%h3
%em.fa.fa-repeat
= t @event.rule, scope: 'activerecord.attributes.event.rule_values'
= @event.schedule
%ul
%li= link_to_unless_current @event.event || @event, @event.event || @event
- (@event.event || @event).events.each do |e|
%li= link_to_unless_current e, e
- if @event.moderated?
%ul
%li= link_to_unless_current @event.event || @event, @event.event || @event
- (@event.event || @event).events.each do |e|
%li= link_to_unless_current e, e

View File

@ -69,6 +69,7 @@ en:
daily: Daily
weekly: Weekly
monthly: Monthly
monthly_day: Nth day of the month
yearly: Yearly
description: Description
place_name: Place name

View File

@ -69,6 +69,7 @@ fr:
daily: Journalière
weekly: Hebdomadaire
monthly: Mensuelle
monthly_day: Nème jour du mois
yearly: Annuelle
description: Description
place_name: Nom du lieu

20
lib/schedule.rb Normal file
View File

@ -0,0 +1,20 @@
# The code to manage adl scheduling. Repeat events, is managed here
module Schedule
def schedule
IceCube::Schedule.new start_time, end_time: end_time do |s|
s.add_recurrence_rule prepare_schedule.count repeat + 1
end
end
private
def prepare_schedule
rule ||= 'daily'
r = IceCube::Rule.send rule.split('_').first
if rule == 'monthly_day'
r.day_of_week start_time.wday => [start_time.day / 7 + 1]
else
r
end
end
end