From 6ff4eecbc133bb4343b37f2ee76bc46fcd954b97 Mon Sep 17 00:00:00 2001 From: "BOYA,CHIU" Date: Thu, 5 Aug 2021 18:19:42 +0800 Subject: [PATCH] fix bug for anns cache --- app/controllers/announcements_controller.rb | 25 +++++++++++++-------- app/models/anns_cache.rb | 1 + app/models/bulletin.rb | 5 +++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 02a2488..6f4d3b7 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -59,6 +59,9 @@ class AnnouncementsController < ApplicationController a end end + if anns.count == 0 && params.any?{|k,v| v.class==Array ? v.any?{|v1| v1.to_s.match(/\*|\(|\)/)} : v.to_s.match(/\*|\(|\)/)} + return nil + end #If no data , hide title&table if sorted.count == 0 display = "hide" @@ -447,25 +450,29 @@ class AnnouncementsController < ApplicationController anns_cache = AnnsCache.where(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s) set_image_version_for_widget() devide_flag = (!(defined? SiteFeed).nil?) - if anns_cache.count != 1 || is_random + anns_cache.where(:invalid_date.lte => Time.now).destroy + if anns_cache.count > 1 + anns_cache.skip(1).destroy + end + if anns_cache.count == 0 || is_random Bulletin.remove_expired_status uid = OrbitHelper.params[:uid] rescue "" - sorted_anns = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil], :uid.ne => uid) - .can_display_and_sorted.is_approved - .filter_by_widget_categories(cats,false).filter_by_tags(tags) + anns_for_locale = Bulletin.where(:title.nin => ["",nil], :uid.ne => uid).is_approved.filter_cats_and_tags(cats,tags) + sorted_anns = anns_for_locale.can_display_and_sorted if !is_random sorted_anns = sorted_anns.limit(widget_data_count) - if anns_cache.count > 1 - anns_cache.destroy - end + first_deadline = sorted_anns.pluck(:deadline).compact.sort[0] + first_postdate = anns_for_locale.open_in_future.limit(1).pluck(:postdate)[0] + invalid_date = [first_postdate,first_deadline].compact.sort[0] + if devide_flag now_anns = sorted_anns.to_a top_anns = now_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v,set_tags)} not_top_anns = now_anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v,set_tags)} - AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns}) + AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns},invalid_date: invalid_date) else anns = sorted_anns.map{|v| data_to_human_type(v,set_tags)} - AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: anns) + AnnsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: anns,invalid_date: invalid_date) end else if devide_flag diff --git a/app/models/anns_cache.rb b/app/models/anns_cache.rb index 7952c4e..346ea23 100644 --- a/app/models/anns_cache.rb +++ b/app/models/anns_cache.rb @@ -3,4 +3,5 @@ class AnnsCache field :parent_id field :filter_result field :locale,type: String,default: 'zh_tw' + field :invalid_date, type: DateTime end \ No newline at end of file diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 1076a5f..49940ab 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -78,9 +78,10 @@ class Bulletin before_destroy :destroy_email - scope :can_display_and_sorted, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)} + scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)} + scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)} scope :is_approved, ->{where(:approved => true)} - + scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)} before_create :set_expire before_save :check_limit def to_calendar_param