From 308370c4dfa6e831d7c300d52e4a72e1d0b55aba Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 11 Sep 2015 20:28:41 +0800 Subject: [PATCH] added rss feeds --- .../admin/announcements_controller.rb | 2 +- .../announcement_feeds_controller.rb | 70 +++++++++++++------ app/views/admin/announcements/_feed.html.erb | 3 + .../announcement_feeds/rssfeed.rss.builder | 16 +++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 1 + 7 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 app/views/announcement_feeds/rssfeed.rss.builder diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index f04ce6a..03fe0d7 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -30,7 +30,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def feed - @table_feed_fields = ["announcement.feed_name", :tags] + @table_feed_fields = ["announcement.feed_name", :tags, "announcement.rssfeed"] @feeds = BulletinFeed.all.asc(:created_at) end diff --git a/app/controllers/announcement_feeds_controller.rb b/app/controllers/announcement_feeds_controller.rb index 5f47a10..68d0f75 100644 --- a/app/controllers/announcement_feeds_controller.rb +++ b/app/controllers/announcement_feeds_controller.rb @@ -1,7 +1,50 @@ +require "rss" class AnnouncementFeedsController < ApplicationController - + include Admin::AnnouncementsHelper def feed uid = params[:uid] + anns = get_announcements(uid) + render :json => anns.to_json + end + + def rssfeed + uid = params[:uid] + @bf = BulletinFeed.find_by(:uid => uid) rescue nil + if !@bf.nil? + tags = @bf.tag_ids + if !tags.empty? + @announcements = Bulletin.can_display.is_approved.filter_by_tags(tags) + end + end + respond_to do |format| + format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"} + format.rss + end + end + + def feeds + feeds = [] + BulletinFeed.all.each do |bf| + feed = {} + feed["title_translations"] = bf.title_translations + feed["uid"] = bf.uid + feed["url"] = "#{request.base_url}/xhr/announcements/feed/#{bf.uid}" + feed["xml_url"] = "#{request.base_url}/xhr/announcements/rssfeed/#{bf.uid}.rss" + feed["tags"] = [] + bf.tag_ids.each do |t| + tag = Tag.find(t) + d = {} + d["name_translations"] = tag.name_translations + feed["tags"] << d + end + feeds << feed + end + render :json => {"feeds" => feeds}.to_json + end + + private + + def get_announcements(uid) bf = BulletinFeed.find_by(:uid => uid) rescue nil if !bf.nil? tags = bf.tag_ids @@ -57,28 +100,11 @@ class AnnouncementFeedsController < ApplicationController end all_anns << a end - render :json => {"announcements" => all_anns, "tags" => tag_names}.to_json + { + "announcements" => all_anns, + "tags" => tag_names + } end - - def feeds - feeds = [] - BulletinFeed.all.each do |bf| - feed = {} - feed["title_translations"] = bf.title_translations - feed["uid"] = bf.uid - feed["url"] = "#{request.base_url}/xhr/announcements/feed/#{bf.uid}" - feed["tags"] = [] - bf.tag_ids.each do |t| - tag = Tag.find(t) - d = {} - d["name_translations"] = tag.name_translations - feed["tags"] << d - end - feeds << feed - end - render :json => {"feeds" => feeds}.to_json - end - end diff --git a/app/views/admin/announcements/_feed.html.erb b/app/views/admin/announcements/_feed.html.erb index f08181d..5c92dcd 100644 --- a/app/views/admin/announcements/_feed.html.erb +++ b/app/views/admin/announcements/_feed.html.erb @@ -31,4 +31,7 @@ + + Rss Feed + \ No newline at end of file diff --git a/app/views/announcement_feeds/rssfeed.rss.builder b/app/views/announcement_feeds/rssfeed.rss.builder new file mode 100644 index 0000000..a4c7219 --- /dev/null +++ b/app/views/announcement_feeds/rssfeed.rss.builder @@ -0,0 +1,16 @@ +xml.instruct! :xml, :version => "1.0" +xml.rss :version => "2.0" do + xml.channel do + xml.title @bf.title + xml.link "/xhr/announcements/rssfeed/73999228.rss" + + for announcement in @announcements + xml.item do + xml.title announcement.title + xml.description announcement.subtitle + xml.pubDate announcement.created_at.to_s(:rfc822) + xml.link page_for_bulletin(announcement) + end + end + end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index db8851d..0b69832 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,6 +13,7 @@ en: view_count: View Count add_new: Add New feed_name: Feed Name + rssfeed: Rss Feed Link feed_list: Feed List approve: Approve all_articles: All Articles diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index d08484a..0fee564 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -14,6 +14,7 @@ zh_tw: add_new: 新建 approve: 通過 feed_name: Feed 標題 + rssfeed: Rss Feed Link feed_list: 訂閱清單 all_articles: 文章列表 announcement: 公告 diff --git a/config/routes.rb b/config/routes.rb index 81add08..bd2fddb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,7 @@ Rails.application.routes.draw do end get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed" + get "/xhr/announcements/rssfeed/:uid" => "announcement_feeds#rssfeed" get "/xhr/announcements/feeds" => "announcement_feeds#feeds" end