From b989f458d3321e83dd00469d3c958f174ef9132e Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Tue, 31 Jul 2012 18:22:40 +0800 Subject: [PATCH] Do not show news/anc if no context. --- lib/tasks/data_migration.rake | 15 +++++++ .../front_end/bulletins_controller.rb | 10 ++--- .../widget/bulletins_controller.rb | 10 ++--- .../announcement/back_end/bulletins_helper.rb | 8 +++- .../announcement/app/models/bulletin.rb | 24 ++++++++++- .../back_end/bulletins/_form.html.erb | 41 ++----------------- .../front_end/news_bulletins_controller.rb | 10 ++--- .../news/widget/news_bulletins_controller.rb | 16 ++++---- .../news/back_end/news_bulletins_helper.rb | 7 +++- .../news/app/models/news_bulletin.rb | 30 +++++++++++--- .../back_end/news_bulletins/_form.html.erb | 3 +- 11 files changed, 102 insertions(+), 72 deletions(-) diff --git a/lib/tasks/data_migration.rake b/lib/tasks/data_migration.rake index 842c327d..5d5f5446 100644 --- a/lib/tasks/data_migration.rake +++ b/lib/tasks/data_migration.rake @@ -44,4 +44,19 @@ namespace :data_migration do sr.save! end + task :make_bulletins_and_news_has_language_flag => :environment do + [Bulletin,NewsBulletin].each do |bulletin| + puts "="*10 + bulletin.to_s + "="*10 + bulletin.all.each do |bt| + result = if (bt.save(:validate => false) rescue false ) + "OK" + else + 'Failed' + end + puts(bt.title+"----------#{result}") + end + end + puts("#{'='*10}Finished#{'='*10}") + end + end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb index 113c7f84..d970ee1c 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb @@ -15,18 +15,18 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController search = Redis::Search.query("Bulletin", params[:search_query], :conditions =>search_cond,:limit=>Bulletin.all.count) search_result = search.collect{|t| t["id"]} - @bulletins = Bulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(15) + @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).page( params[:page_main]).per(15) else date_now = Time.now if !params[:category_id].blank? - @bulletins = Bulletin.all.can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) + @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil elsif !params[:tag_id].blank? @tag = AnnouncementTag.find(params[:tag_id]) rescue nil @tag = AnnouncementTag.where(key: params[:tag_id])[0] unless @tag - @bulletins = @tag.bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) + @bulletins = @tag.bulletins.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) else - @bulletins = Bulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) + @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(15) end end @@ -36,7 +36,7 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController if params[:preview] == "true" preview_content else - @bulletin = Bulletin.all.can_display.where(_id: params[:id]).first + @bulletin = Bulletin.all.available_for_lang(I18n.locale).can_display.where(_id: params[:id]).first unless @bulletin.disable? #impressionist(@bulletin) get_categorys diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb index 07870ae5..42339d63 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/widget/bulletins_controller.rb @@ -11,14 +11,14 @@ class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController @title = params[:part_title] date_now = Time.now if !params[:category_id].blank? - @bulletins = Bulletin.all.can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) + @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil elsif !params[:tag_id].blank? @tag = AnnouncementTag.find(params[:tag_id]) rescue nil @tag = AnnouncementTag.where(key: params[:tag_id])[0] unless @tag - @bulletins = @tag.bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) + @bulletins = @tag.bulletins.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) else - @bulletins = Bulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) + @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10) end get_categorys @@ -28,13 +28,13 @@ class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController def bulletins_and_web_links @tags = AnnouncementTag.any_in(key: ['students', 'alumni', 'employee', 'guest']).asc(:order) @selected_tag = AnnouncementTag.find(params[:id]) rescue @tags[0] - @bulletins = @selected_tag.get_visible_bulletins.can_display.page(params[:page]).per(5) rescue nil + @bulletins = @selected_tag.get_visible_bulletins.available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links.page(params[:page]).per(5) rescue nil end def reload_bulletins @selected_tag = AnnouncementTag.find(params[:tag_id]) - @bulletins = @selected_tag.get_visible_bulletins.can_display.page(params[:page]).per(5) rescue nil + @bulletins = @selected_tag.get_visible_bulletins.available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil end def reload_web_links diff --git a/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb b/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb index 5de4a826..a71b4965 100644 --- a/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb +++ b/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb @@ -1,5 +1,10 @@ module Panel::Announcement::BackEnd::BulletinsHelper - + def show_form_status_field(bulletin) + by_bulletin = (!bulletin.is_expired? and bulletin.is_pending?) + by_user = ((bulletin.bulletin_category.authed_users('fact_check').include?(current_user) rescue nil) or is_manager? or is_admin?) + by_bulletin and by_user + end + def show_approval_link(bulletin) by_bulletin = (!bulletin.is_expired? and bulletin.is_pending?) by_user = ((bulletin.bulletin_category.authed_users('fact_check').include?(current_user) rescue nil) or is_manager? or is_admin?) @@ -37,4 +42,5 @@ module Panel::Announcement::BackEnd::BulletinsHelper end end + end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 0241a478..e6986466 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -42,7 +42,8 @@ class Bulletin field :public, :type => Boolean, :default => true scope :can_display,where(is_checked: true) - + scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) } + mount_uploader :image, ImageUploader @@ -59,7 +60,7 @@ class Bulletin validates :title, :presence => true - before_save :check_deadline,:update_status + before_save :check_deadline,:update_status,:update_avliable_language after_save :save_bulletin_links after_save :save_bulletin_files @@ -75,14 +76,24 @@ class Bulletin # # [s_title,s_title_en,s_text_en,s_text_zh_tw].join(' ') # end + #alias_method + def s_title self.title_translations["zh_tw"] end + def s_title_was + self.title_was["zh_tw"] + end + def s_title_en self.title_translations["en"] end + def s_title_en_was + self.title_was["en"] + end + # def s_text_en # Nokogiri::HTML(self.text.en).text # end @@ -219,5 +230,14 @@ class Bulletin return true end + def update_avliable_language + VALID_LOCALES.each do |locale| + unless title[locale].blank? || subtitle[locale].blank? || text[locale].blank? + self["available_for_#{locale}".to_sym] = false + else + self["available_for_#{locale}".to_sym] = true + end + end + end end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb index f58cfe2b..47f24fe8 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb @@ -4,42 +4,6 @@ <%= f.error_messages %>
-
-
- -
-

Picture

-
-
- -
- - <% if @bulletin.image %> - <%= image_tag @bulletin.image rescue ''%> - <% else %> - - <% end %> - -
-
- - -
- -
- <% if @bulletin.image.file %> - <%= f.check_box :remove_image %> - <%= t('announcement.刪除已上傳檔案') %> - <% end %> -
-
-
-
- -
@@ -61,7 +25,8 @@
- + + <% if show_form_status_field(@bulletin)%>
@@ -84,7 +49,7 @@
- + <% end %>
diff --git a/vendor/built_in_modules/news/app/controllers/panel/news/front_end/news_bulletins_controller.rb b/vendor/built_in_modules/news/app/controllers/panel/news/front_end/news_bulletins_controller.rb index d6e2af7f..142017b7 100644 --- a/vendor/built_in_modules/news/app/controllers/panel/news/front_end/news_bulletins_controller.rb +++ b/vendor/built_in_modules/news/app/controllers/panel/news/front_end/news_bulletins_controller.rb @@ -14,7 +14,7 @@ class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController search_cond.merge!({:news_bulletin_category_id => "#{params[:category_id]}" }) if !params[:category_id].blank? search = Redis::Search.query("NewsBulletin", params[:search_query], :conditions =>search_cond,:limit=>NewsBulletin.all.count) search_result = search.collect{|t| t["id"]} - @news_bulletins = NewsBulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(10) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).page( params[:page_main]).per(10) else date_now = Time.now if !params[:tag_id].blank? @@ -24,11 +24,11 @@ class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController end if !params[:category_id].blank? - @news_bulletins = NewsBulletin.all.can_display.where(:news_bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(:news_bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) elsif !params[:tag_id].blank? - @news_bulletins = tmp.news_bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) rescue nil + @news_bulletins = tmp.news_bulletins.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) rescue nil else - @news_bulletins = NewsBulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10) end end get_categorys @@ -41,7 +41,7 @@ class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController if params[:preview] == "true" preview_content else - @news_bulletin = NewsBulletin.all.can_display.where(_id: params[:id]).first + @news_bulletin = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(_id: params[:id]).first unless @news_bulletin.disable? # impressionist(@news_bulletin) get_categorys diff --git a/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb b/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb index 9a8ac884..bb35dc85 100644 --- a/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb +++ b/vendor/built_in_modules/news/app/controllers/panel/news/widget/news_bulletins_controller.rb @@ -13,14 +13,14 @@ class Panel::News::Widget::NewsBulletinsController < OrbitWidgetController @title = params[:part_title] date_now = Time.now if !params[:category_id].blank? - @news_bulletins = NewsBulletin.all.can_display.where(:news_bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(:news_bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) @current_category = NewsBulletinCategory.from_id(params[:category_id]) rescue nil elsif !params[:tag_id].blank? @tag = NewsTag.find(params[:tag_id]) rescue nil @tag = NewsTag.where(key: params[:tag_id])[0] unless @tag - @news_bulletins = @tag.news_bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) rescue nil + @news_bulletins = @tag.news_bulletins.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) rescue nil else - @news_bulletins = NewsBulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(5) end end @@ -28,13 +28,13 @@ class Panel::News::Widget::NewsBulletinsController < OrbitWidgetController def news_bulletins_and_web_links @tags = NewsTag.all @selected_tag = NewsTag.find(params[:id]) rescue @tags[0] - @news_bulletins = @selected_tag.get_visible_news_bulletins.can_display.page(params[:page]).per(5) rescue nil + @news_bulletins = @selected_tag.get_visible_news_bulletins.available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links.page(params[:page]).per(5) rescue nil end def reload_news_bulletins @selected_tag = NewsTag.find(params[:tag_id]) - @news_bulletins = @selected_tag.get_visible_news_bulletins.can_display.page(params[:page]).per(5) rescue nil + @news_bulletins = @selected_tag.get_visible_news_bulletins.available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil end def reload_web_links @@ -53,11 +53,11 @@ class Panel::News::Widget::NewsBulletinsController < OrbitWidgetController def home_banner if !params[:category_id].blank? - @news_bulletins = NewsBulletin.all.can_display.where(:news_bulletin_category_id => params[:category_id]).widget_datas.limit(9) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(:news_bulletin_category_id => params[:category_id]).widget_datas.limit(9) elsif !params[:tag_id].blank? - @news_bulletins = NewsTag.find(params[:tag_id]).news_bulletins.can_display.widget_datas.limit(9) rescue nil + @news_bulletins = NewsTag.find(params[:tag_id]).news_bulletins.available_for_lang(I18n.locale).can_display.widget_datas.limit(9) rescue nil else - @news_bulletins = NewsBulletin.all.can_display.widget_datas.limit(9) + @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.widget_datas.limit(9) end get_categorys diff --git a/vendor/built_in_modules/news/app/helpers/panel/news/back_end/news_bulletins_helper.rb b/vendor/built_in_modules/news/app/helpers/panel/news/back_end/news_bulletins_helper.rb index bb64dbc7..5f4daa62 100644 --- a/vendor/built_in_modules/news/app/helpers/panel/news/back_end/news_bulletins_helper.rb +++ b/vendor/built_in_modules/news/app/helpers/panel/news/back_end/news_bulletins_helper.rb @@ -1,5 +1,10 @@ module Panel::News::BackEnd::NewsBulletinsHelper - + def news_show_form_status_field(news_bulletin) + by_news_bulletin = (!news_bulletin.is_expired? and news_bulletin.is_pending?) + by_user = (news_bulletin.news_bulletin_category.authed_users('fact_check').include?(current_user) or is_manager? or is_admin?) + by_news_bulletin and by_user + end + def news_show_approval_link(news_bulletin) by_news_bulletin = (!news_bulletin.is_expired? and news_bulletin.is_pending?) by_user = (news_bulletin.news_bulletin_category.authed_users('fact_check').include?(current_user) or is_manager? or is_admin?) diff --git a/vendor/built_in_modules/news/app/models/news_bulletin.rb b/vendor/built_in_modules/news/app/models/news_bulletin.rb index 350a643d..e33c638b 100644 --- a/vendor/built_in_modules/news/app/models/news_bulletin.rb +++ b/vendor/built_in_modules/news/app/models/news_bulletin.rb @@ -44,7 +44,8 @@ class NewsBulletin field :public, :type => Boolean, :default => true scope :can_display,where(is_checked: true) - + scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) } + mount_uploader :image, ImageUploader belongs_to :unit_list_for_anc @@ -60,7 +61,7 @@ class NewsBulletin validates_presence_of :title - before_save :update_status + before_save :update_status,:update_avliable_language after_save :save_news_bulletin_links after_save :save_news_bulletin_files @@ -100,13 +101,21 @@ class NewsBulletin # # [s_title,s_title_en,s_text_en,s_text_zh_tw].join(' ') # end - def s_title - self.title_translations["zh_tw"] + def s_title + self.title_translations["zh_tw"] + end + + def s_title_was + self.title_was["zh_tw"] end - def s_title_en + def s_title_en self.title_translations["en"] - end + end + + def s_title_en_was + self.title_was["en"] + end # def s_text_en # Nokogiri::HTML(self.text.en).text @@ -194,6 +203,15 @@ class NewsBulletin end protected + def update_avliable_language + VALID_LOCALES.each do |locale| + unless title[locale].blank? || subtitle[locale].blank? || text[locale].blank? + self["available_for_#{locale}".to_sym] = false + else + self["available_for_#{locale}".to_sym] = true + end + end + end def update_status if !self.is_pending diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb index 52b9f662..4a5a4577 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb @@ -62,6 +62,7 @@
+ <% if news_show_form_status_field( @news_bulletin)%>
@@ -84,7 +85,7 @@
- + <% end %>