Browse Source

Attempt to manage ical time zones using proper time zone TZID.

Refs #76 and refs #136
remotes/origin/master
echarp 3 years ago
parent
commit
999f947aa4
  1. 13
      app/models/region.rb
  2. 11
      app/views/events/index.ics.haml
  3. 11
      app/views/events/show.ics.haml
  4. 13
      test/fixtures/regions.yml
  5. 2
      test/models/event_test.rb
  6. 15
      test/models/region_test.rb

13
app/models/region.rb

@ -18,4 +18,17 @@ class Region < ApplicationRecord
def to_s
name
end
# Attempt to get a corresponding timezone, used for ical
def tzid
country = TZInfo::Country.get region.try(:code) || code
if country.present? && country.zone_identifiers.length.positive?
country.zone_identifiers[0]
else
Time.now.zone
end
rescue TZInfo::InvalidCountryCode
# Mostly useful for non country calendars
Time.now.zone
end
end

11
app/views/events/index.ics.haml

@ -2,14 +2,17 @@
# Create a calendar with an event (standard method)
cal = Icalendar::Calendar.new
@events.each do |event|
tzid = event.region.tzid
cal.event do |e|
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time, tzid: tzid
e.uid = "#{event.id}@#{request.domain}"
e.dtstart = Icalendar::Values::DateTime.new event.start_time
e.dtend = Icalendar::Values::DateTime.new event.end_time
e.dtstart = Icalendar::Values::DateTime.new event.start_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new event.end_time, tzid: tzid
e.summary = event.title
e.description = event.description.tr '\'', '’'
e.description = to_markdown event.description.tr '\'', '’'
e.location = event.full_address.tr '\'', '’'
e.organizer = "mailto:#{event.contact}"
e.x_alt_desc = Icalendar::Values::Text.new event.description, FMTTYPE: 'text/html'
end
end

11
app/views/events/show.ics.haml

@ -1,12 +1,17 @@
:ruby
# Create a calendar with an event (standard method)
cal = Icalendar::Calendar.new
tzid = @event.region.tzid
cal.event do |e|
e.dtstart = Icalendar::Values::DateTime.new @event.start_time
e.dtend = Icalendar::Values::DateTime.new @event.end_time
e.dtstamp = Icalendar::Values::DateTime.new @event.decision_time, tzid: tzid
e.uid = "#{event.id}@#{request.domain}"
e.dtstart = Icalendar::Values::DateTime.new @event.start_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new @event.end_time, tzid: tzid
e.summary = @event.title
e.description = strip_tags(@event.description).tr '\'', '’'
e.description = to_markdown @event.description.tr '\'', '’'
e.location = @event.full_address
e.organizer = "mailto:#{@event.contact}"
e.x_alt_desc = Icalendar::Values::Text.new @event.description, FMTTYPE: 'text/html'
end
cal.publish

13
test/fixtures/regions.yml

@ -1,7 +1,20 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
region_france:
name: France
code: FR
region_quebec:
name: Quebec
code: QC
region_one:
name: Alsace
region: region_france
region_two:
name: Aquitaine
region: region_france
region_other:
name: Other

2
test/models/event_test.rb

@ -118,6 +118,6 @@ class EventTest < ActiveSupport::TestCase
@event.city = 'world'
@event.region.name = 'here'
@event.region.code = 'xyz'
assert_equal 'hello, world', @event.full_address
assert_equal 'hello, world, here, France', @event.full_address
end
end

15
test/models/region_test.rb

@ -2,7 +2,16 @@ require 'test_helper'
# Mostly a data holder model, to maintain coutry > region data
class RegionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test 'proper timezone' do
assert regions(:region_one).tzid.present?
assert regions(:region_one).tzid == 'Europe/Paris'
end
test 'check québec timezone is local timezone' do
assert regions(:region_quebec).tzid == Time.now.zone
end
test 'check other timezone is local timezone' do
assert regions(:region_other).tzid == Time.now.zone
end
end

Loading…
Cancel
Save