From 3867b8fc6910590d673c7f7d2ba608a70a0045dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 21 Oct 2021 15:03:35 +0800 Subject: [PATCH] add all day settting --- app/controllers/event_news_controller.rb | 10 +++++----- app/helpers/event_news_helper.rb | 7 ++++--- app/models/event_news.rb | 11 ++++++++--- app/views/admin/event_news/_form.html.erb | 6 ++++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/controllers/event_news_controller.rb b/app/controllers/event_news_controller.rb index 7d25d1e..e21f5a0 100644 --- a/app/controllers/event_news_controller.rb +++ b/app/controllers/event_news_controller.rb @@ -21,7 +21,7 @@ class EventNewsController < ApplicationController target = a.is_external_link ? "_blank" : "_self" doc = Nokogiri::HTML(a.title) title = doc.text.empty? ? 'no content' : doc.text - event_time = (a.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + event_time = (a.event_date.strftime(a.date_parse_format) rescue "") if a.event_end_date event_time = "#{event_time.split(" ")[0]} ~ #{a.event_end_date.strftime('%Y-%m-%d')}" end @@ -532,7 +532,7 @@ class EventNewsController < ApplicationController event_news = top_event_news + rest_event_news end end - event_news.each{|a| a["postdate"] = a["postdate"].in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y-%m-%d %H:%M') rescue nil } + event_news.each{|a| a["postdate"] = a["postdate"].in_time_zone(Time.zone.utc_offset / 3600).strftime((a['all_day'] ? '%Y-%m-%d' : '%Y-%m-%d %H:%M')) rescue nil } event_news end @@ -623,7 +623,7 @@ class EventNewsController < ApplicationController pause_btn_title = (I18n.locale.to_s =="zh_tw") ? "暫停播放" : "pause" prev_btn_title = (I18n.locale.to_s =="zh_tw") ? "上一張" : "prev" next_btn_title = (I18n.locale.to_s =="zh_tw") ? "下一張" : "next" - event_time = (event_news.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + event_time = (event_news.event_date.strftime(event_news.date_parse_format) rescue "") if event_news.event_end_date event_time = "#{event_time.split(" ")[0]} ~ #{event_news.event_end_date.strftime('%Y-%m-%d')}" end @@ -713,7 +713,7 @@ class EventNewsController < ApplicationController event_date = DateTime.parse(event_news["event_date"].to_s) rescue nil event_end_date = DateTime.parse(event_news["event_end_date"].to_s) rescue nil - event_time = (event_date.strftime('%Y-%m-%d %H:%M') rescue "") + event_time = (event_date.strftime(event_news.date_parse_format) rescue "") if event_end_date event_time = "#{event_time.split(" ")[0]} ~ #{event_end_date.strftime('%Y-%m-%d')}" end @@ -809,7 +809,7 @@ class EventNewsController < ApplicationController target = a.is_external_link ? "_blank" : "_self" doc = Nokogiri::HTML(a.title) title = doc.text.empty? ? 'no content' : doc.text - event_time = (a.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + event_time = (a.event_date.strftime(a.date_parse_format) rescue "") if a.event_end_date event_time = "#{event_time.split(" ")[0]} ~ #{a.event_end_date.strftime('%Y-%m-%d')}" end diff --git a/app/helpers/event_news_helper.rb b/app/helpers/event_news_helper.rb index 3283632..0769b8c 100644 --- a/app/helpers/event_news_helper.rb +++ b/app/helpers/event_news_helper.rb @@ -118,12 +118,13 @@ module EventNewsHelper event_time_formated = a.event_time_formated s = DateTime.parse(fa["event_date"]) rescue nil e = DateTime.parse(fa["event_end_date"]) rescue nil + date_parse_format = fa["all_day"] ? '%Y-%m-%d' : '%Y-%m-%d %H:%M' if s.blank? && e.blank? event_time_formated = "" elsif s.blank? - event_time_formated = s.strftime('%Y-%m-%d %H:%M') + event_time_formated = s.strftime(date_parse_format) elsif e.blank? - event_time_formated = "~ " + e.strftime('%Y-%m-%d %H:%M') + event_time_formated = "~ " + e.strftime(date_parse_format) else if s.to_date == e.to_date date_str = s.strftime('%Y-%m-%d') @@ -131,7 +132,7 @@ module EventNewsHelper e_time = e.strftime('%H:%M') event_time_formated = "#{date_str} #{s_time} ~ #{e_time}" else - event_time_formated = s.strftime('%Y-%m-%d %H:%M') + " ~ " + e.strftime('%Y-%m-%d %H:%M') + event_time_formated = s.strftime(date_parse_format) + " ~ " + e.strftime(date_parse_format) end end x = { diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 6e28a5b..caf06f6 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -34,6 +34,7 @@ class EventNews field :calendar_start_date, :type => DateTime field :calendar_end_date, :type => DateTime field :calendar_all_day,type: Boolean,default: false + field :all_day,type: Boolean,default: false field :calendar_type_id field :event_id field :place, type: String, localize: true @@ -104,15 +105,19 @@ class EventNews self end end + def date_parse_format + all_day ? '%Y-%m-%d' : '%Y-%m-%d %H:%M' + end + def event_time_formated s = self.event_date e = self.event_end_date if s.blank? && e.blank? "" elsif e.blank? - s.strftime('%Y-%m-%d %H:%M') + s.strftime(self.date_parse_format) elsif s.blank? - "~ " + e.strftime('%Y-%m-%d %H:%M') + "~ " + e.strftime(self.date_parse_format) else if s.to_date == e.to_date date_str = s.strftime('%Y-%m-%d') @@ -120,7 +125,7 @@ class EventNews e_time = e.strftime('%H:%M') "#{date_str} #{s_time} ~ #{e_time}" else - s.strftime('%Y-%m-%d %H:%M') + " ~ " + e.strftime('%Y-%m-%d %H:%M') + s.strftime(self.date_parse_format) + " ~ " + e.strftime(self.date_parse_format) end end end diff --git a/app/views/admin/event_news/_form.html.erb b/app/views/admin/event_news/_form.html.erb index 41b05ef..c5b5101 100644 --- a/app/views/admin/event_news/_form.html.erb +++ b/app/views/admin/event_news/_form.html.erb @@ -76,6 +76,12 @@ <%= f.datetime_picker :event_end_date, :no_label => true, :new_record => @event_news.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> +
+ +
+ <%= f.check_box :all_day %> +
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index cc944cb..b8601dd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,6 +2,7 @@ en: module_name: event_news: Event News event_news: + all_day: All Day show_today_data_first: Show today data first custom_fields_title: Custom Fields Title custom_carousel_image_width_hint: "If blank, width will be default value." diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 07c9543..3d5bfc9 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -2,6 +2,7 @@ zh_tw: module_name: event_news: 活動公告 event_news: + all_day: 全天 show_today_data_first: 優先顯示當日資料 custom_fields_title: 欄位名稱設定 custom_carousel_image_width_hint: "未填寫,則使用預設寬度"