calendar/app/models/calendar_setting.rb

83 lines
2.3 KiB
Ruby

class CalendarSetting
include Mongoid::Document
include Mongoid::Timestamps
field :enable, type: Boolean, default: false
field :modes, type: Hash, default: {}, localize: true
field :days, type: Array, default: [], localize: true
field :sunday_first, type: Boolean, localize: true
field :date_type, type: Integer, localize: true, default: 0
field :time_type, type: Integer, localize: true, default: 0
field :titleFormat, localize: true
All_days = (0...7).to_a
ModesInfo = {'day'=>'timeGridDay', 'week'=>'timeGridWeek', 'month'=>'dayGridMonth', 'agenda'=>'agenda'}
All_modes = ModesInfo.keys
DateTypes = {
"zh_tw"=>["[LocaleString]: 2022年7月12日 (週三)", "[en-US format]: weekday, monthname dd, YYYY", "[Locale Numeric]: YYYY/mm/dd (weekday)"],
"en"=>["[LocaleString]: weekday, monthname dd, YYYY", "[en-US format]: weekday, monthname dd, YYYY", "[Locale Numeric]: weekday, mm/dd/YYYY"]
}
TimeTypes = ["12_hour_clock", "24_hour_clock"]
def week_title
if self.days.present?
self.days
else
(0...7).map{|i| I18n.t("calendar.day.#{i}")}
end
end
def get_week_title(locale)
tmp = self.days_translations[locale]
if tmp.present?
tmp
else
I18n.with_locale(locale){All_days.map{|i| I18n.t("calendar.day.#{i}")}}
end
end
def sunday_first
val = super
if val.nil?
if I18n.locale == :zh_tw
val = false
else
val = true
end
end
val
end
def get_sunday_first(locale)
val = self.sunday_first_translations[locale]
if val.nil?
if locale == :zh_tw
val = false
else
val = true
end
end
val
end
def get_mode(mode, locale)
tmp = self.modes_translations[locale] || {}
val = tmp[mode]
if val.nil?
val = I18n.with_locale(locale){I18n.t("calendar.mode.#{mode}")}
end
val
end
def get_modes(use_default=false)
tmp = self.modes_translations[I18n.locale]
if tmp.blank? || use_default
All_modes.map{|m| [m, I18n.t("calendar.mode.#{m}")]}.to_h
else
tmp
end
end
def get_modes_info(use_default=false)
tmp = self.get_modes(use_default || !(self.enable))
tmp.map{|m, trans| [trans, ModesInfo[m]]}
end
def titleFormat
tmp = super
if tmp.nil?
tmp = 'dddd, MMMM D, YYYY'
end
tmp
end
end