From 88c66108b1e02f38c07f298d93bd1f7e0f79a378 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 13 Aug 2015 19:50:49 +0800 Subject: [PATCH] added new widget for nccu --- app/controllers/news_controller.rb | 61 ++++++++++++++++++++++++++++++ lib/news/engine.rb | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index 2cae1aa..2936bc1 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -180,6 +180,67 @@ class NewsController < ApplicationController } end + def widget2 + more_stories = {"en" => "More Stories+", "zh_tw" => "更多新聞+"} + categories = OrbitHelper.widget_categories + ma = ModuleApp.find_by_key("news") + page = Page.where(:module => "news").first rescue nil + if categories.first == "all" || categories.count == 0 + categories = ma.categories.enabled.limit(3) + else + if categories.count > 3 + categories = Category.find(categories.sample(3)) + else + categories = Category.find(categories) + end + end + categories_data = [] + categories_for_dropdown = ma.categories.enabled.collect do |cat| + link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}" + { + "name" => cat.title, + "category-link" => link + } + end + link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/" + categories_for_dropdown.unshift({"name" => t("news.all"), "category-link" => link}) + + hot_news = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :category_id.in => categories.collect{|cat| cat.id}, :is_hot => true).can_display.is_approved.desc(:created_at).limit(1) + hot_news = hot_news.first + categories.each_with_index do |category,index| + if hot_news.nil? + nbs = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :category_id => category.id).can_display.is_approved.desc(:created_at).limit(OrbitHelper.widget_data_count) + else + nbs = NewsBulletin.where(:id.ne => hot_news.id, :title.ne => "",:is_preview.in=>[false,nil], :category_id => category.id).can_display.is_approved.desc(:created_at).limit(OrbitHelper.widget_data_count) + end + temp = nbs.collect do |nb| + { + "title" => nb.title, + "link_to_show" => OrbitHelper.widget_item_url(nb.to_param) + } + end + link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/?category=#{category.to_param}" + categories_data << { + "name" => category.title, + "category-link" => link, + "news" => temp + } + end + image_description = hot_news.nil? || hot_news.image_description.blank? ? "裝飾性圖片" : hot_news.image_description + { + "categorized_news" => categories_data, + "categories" => categories_for_dropdown, + "extras" => { + "more_stories" => more_stories[I18n.locale.to_s], + "hot_news_title" => (hot_news.nil? ? "" : hot_news.title), + "hot_news_subtitle" => (hot_news.nil? ? "" : hot_news.subtitle), + "hot_news_image" => (hot_news.nil? ? "" : hot_news.image.mobile.url), + "hot_news_image_description" => image_description, + "hot_news_link_to_show" => (hot_news.nil? ? "" : OrbitHelper.widget_item_url(hot_news.to_param)) + } + } + end + def show params = OrbitHelper.params news = NewsBulletin.find_by(:uid=>params[:uid]) diff --git a/lib/news/engine.rb b/lib/news/engine.rb index 9b76074..b8c0cdc 100644 --- a/lib/news/engine.rb +++ b/lib/news/engine.rb @@ -4,7 +4,7 @@ module News OrbitApp.registration "News", :type => "ModuleApp" do module_label "news.news" base_url File.expand_path File.dirname(__FILE__) - widget_methods ["widget"] + widget_methods ["widget","widget2"] widget_settings [{"data_count"=>12}] models_to_cache [:news_bulletin] # taggable "NewsBulletin"