2014-08-06 14:47:47 +02:00
|
|
|
# This is mostly to group events around a region
|
2017-05-27 09:34:24 +02:00
|
|
|
class Region < ApplicationRecord
|
2017-05-20 16:02:07 +02:00
|
|
|
belongs_to :region
|
2017-09-17 11:14:49 +02:00
|
|
|
has_many :regions, dependent: :nullify
|
2017-05-20 16:02:07 +02:00
|
|
|
|
2017-09-17 11:14:49 +02:00
|
|
|
has_many :orgas, dependent: :destroy
|
2014-01-02 00:21:49 +01:00
|
|
|
|
2014-01-04 11:34:50 +01:00
|
|
|
default_scope { order :name }
|
2019-12-21 21:00:16 +01:00
|
|
|
scope :top, -> { where(region: nil).includes(:regions).reorder :code }
|
|
|
|
scope :local, -> { where(url: [nil, '']) }
|
2018-03-18 16:05:26 +01:00
|
|
|
scope :region, (lambda do |region|
|
|
|
|
return if region.nil? || region == 'all' || region.to_i.zero?
|
2018-09-15 15:05:21 +02:00
|
|
|
|
2018-03-18 16:05:26 +01:00
|
|
|
temp = Region.find region
|
|
|
|
where region: [temp, temp.regions].flatten
|
|
|
|
end)
|
2014-06-09 12:18:40 +02:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
2019-04-27 14:44:29 +02:00
|
|
|
|
2019-12-25 16:46:50 +01:00
|
|
|
def flag
|
2020-01-11 18:28:07 +01:00
|
|
|
region&.flag || code&.downcase
|
2019-12-25 16:46:50 +01:00
|
|
|
end
|
|
|
|
|
2019-04-27 14:44:29 +02:00
|
|
|
# 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?
|
2019-04-27 17:10:45 +02:00
|
|
|
# Get arbitrarily the first zone for this country
|
2019-04-27 14:44:29 +02:00
|
|
|
country.zone_identifiers[0]
|
|
|
|
else
|
|
|
|
Time.now.zone
|
|
|
|
end
|
|
|
|
rescue TZInfo::InvalidCountryCode
|
|
|
|
# Mostly useful for non country calendars
|
|
|
|
Time.now.zone
|
|
|
|
end
|
2013-12-03 21:56:20 +01:00
|
|
|
end
|