From c548502d8485adc968ca234c3da171f1e68f5b22 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Wed, 30 Nov 2016 19:06:15 +0100 Subject: [PATCH] Move calendar mapping to config directory --- app/models/calendar.rb | 93 ++++-------------------------------- config/holiday_calendars.yml | 78 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 83 deletions(-) create mode 100644 config/holiday_calendars.yml diff --git a/app/models/calendar.rb b/app/models/calendar.rb index c27d0fc64..460567cde 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -75,92 +75,19 @@ returnes preset of ical feeds returns { - 'US' => 'http://www.google.com/calendar/ical/en.usa%23holiday%40group.v.calendar.google.com/public/basic.ics', + 'http://www.google.com/calendar/ical/en.usa%23holiday%40group.v.calendar.google.com/public/basic.ics' => 'US', ... } =end def self.ical_feeds - gfeeds = { - 'Australia' => 'en.australian', - 'Austria' => 'de.austrian', - 'Argentina' => 'en.ar', - 'Bahamas' => 'en.bs', - 'Belarus' => 'en.by', - 'Brazil' => 'en.brazilian', - 'Bulgaria' => 'en.bulgarian', - 'Canada' => 'en.canadian', - 'China' => 'en.china', - 'Chile' => 'en.cl', - 'Costa Rica' => 'en.cr', - 'Colombia' => 'en.co', - 'Croatia' => 'en.croatian', - 'Cuba' => 'en.cu', - 'Cyprus' => 'de.cy', - 'Switzerland' => 'de.ch', - 'Denmark' => 'da.danish', - 'Netherlands' => 'nl.dutch', - 'Egypt' => 'en.eg', - 'Ethiopia' => 'en.et', - 'Ecuador' => 'en.ec', - 'Estonia' => 'en.ee', - 'Finland' => 'en.finnish', - 'France' => 'en.french', - 'Germany' => 'de.german', - 'Greece' => 'en.greek', - 'Ghana' => 'en.gh', - 'Hong Kong' => 'en.hong_kong', - 'Haiti' => 'en.ht', - 'Hungary' => 'en.hungarian', - 'India' => 'en.indian', - 'Indonesia' => 'en.indonesian', - 'Iran' => 'en.ir', - 'Ireland' => 'en.irish', - 'Italy' => 'it.italian', - 'Israel' => 'en.jewish', - 'Japan' => 'en.japanese', - 'Kuwait' => 'en.kw', - 'Latvia' => 'en.latvian', - 'Liechtenstein' => 'en.li', - 'Lithuania' => 'en.lithuanian', - 'Luxembourg' => 'en.lu', - 'Malaysia' => 'en.malaysia', - 'Mexico' => 'en.mexican', - 'Morocco' => 'en.ma', - 'Mauritius' => 'en.mu', - 'Moldova' => 'en.md', - 'New Zealand' => 'en.new_zealand', - 'Norway' => 'en.norwegian', - 'Philippines' => 'en.philippines', - 'Poland' => 'en.polish', - 'Portugal' => 'en.portuguese', - 'Pakistan' => 'en.pk', - 'Russia' => 'en.russian', - 'Senegal' => 'en.sn', - 'Singapore' => 'en.singapore', - 'South Africa' => 'en.sa', - 'South Korean' => 'en.south_korea', - 'Spain' => 'en.spain', - 'Slovakia' => 'en.slovak', - 'Serbia' => 'en.rs', - 'Slovenia' => 'en.slovenian', - 'Sweden' => 'en.swedish', - 'Taiwan' => 'en.taiwan', - 'Thai' => 'en.th', - 'Turkey' => 'en.turkish', - 'UK' => 'en.uk', - 'US' => 'en.usa', - 'Ukraine' => 'en.ukrainian', - 'Uruguay' => 'en.uy', - 'Vietnam' => 'en.vietnamese', - 'Venezuela' => 'en.ve', - } - all_feeds = {} - gfeeds.each { |key, name| - all_feeds["http://www.google.com/calendar/ical/#{name}%23holiday%40group.v.calendar.google.com/public/basic.ics"] = key - } - all_feeds + data = YAML.load_file(Rails.root.join('config/holiday_calendars.yml')) + url = data['url'] + + data['countries'].map do |country, domain| + [(url % { domain: domain }), country] + end.to_h end =begin @@ -201,7 +128,7 @@ returns =end def self.sync - Calendar.all.each(&:sync) + Calendar.find_each(&:sync) true end @@ -313,7 +240,7 @@ returns # if changed calendar is default, set all others default to false def sync_default return if !default - Calendar.all.each { |calendar| + Calendar.find_each { |calendar| next if calendar.id == id next if !calendar.default calendar.default = false @@ -331,7 +258,7 @@ returns # check if sla's are refer to an existing calendar default_calendar = Calendar.find_by(default: true) - Sla.all.each { |sla| + Sla.find_each { |sla| if !sla.calendar_id sla.calendar_id = default_calendar.id sla.save! diff --git a/config/holiday_calendars.yml b/config/holiday_calendars.yml new file mode 100644 index 000000000..c105772f3 --- /dev/null +++ b/config/holiday_calendars.yml @@ -0,0 +1,78 @@ +--- +# % character in the URL has to be escaped (% => %%) +url: "http://www.google.com/calendar/ical/%{domain}%%23holiday%%40group.v.calendar.google.com/public/basic.ics" + +# Mapping from country to domain +countries: + Australia: en.australian + Austria: de.austrian + Argentina: en.ar + Bahamas: en.bs + Belarus: en.by + Brazil: en.brazilian + Bulgaria: en.bulgarian + Canada: en.canadian + China: en.china + Chile: en.cl + Costa Rica: en.cr + Colombia: en.co + Croatia: en.croatian + Cuba: en.cu + Cyprus: de.cy + Switzerland: de.ch + Denmark: da.danish + Netherlands: nl.dutch + Egypt: en.eg + Ethiopia: en.et + Ecuador: en.ec + Estonia: en.ee + Finland: en.finnish + France: en.french + Germany: de.german + Greece: en.greek + Ghana: en.gh + Hong Kong: en.hong_kong + Haiti: en.ht + Hungary: en.hungarian + India: en.indian + Indonesia: en.indonesian + Iran: en.ir + Ireland: en.irish + Italy: it.italian + Israel: en.jewish + Japan: en.japanese + Kuwait: en.kw + Latvia: en.latvian + Liechtenstein: en.li + Lithuania: en.lithuanian + Luxembourg: en.lu + Malaysia: en.malaysia + Mexico: en.mexican + Morocco: en.ma + Mauritius: en.mu + Moldova: en.md + New Zealand: en.new_zealand + Norway: en.norwegian + Philippines: en.philippines + Poland: en.polish + Portugal: en.portuguese + Pakistan: en.pk + Russia: en.russian + Senegal: en.sn + Singapore: en.singapore + South Africa: en.sa + South Korean: en.south_korea + Spain: en.spain + Slovakia: en.slovak + Serbia: en.rs + Slovenia: en.slovenian + Sweden: en.swedish + Taiwan: en.taiwan + Thai: en.th + Turkey: en.turkish + UK: en.uk + US: en.usa + Ukraine: en.ukrainian + Uruguay: en.uy + Vietnam: en.vietnamese + Venezuela: en.ve