added rss feeds

This commit is contained in:
Harry Bomrah 2015-09-11 20:28:41 +08:00
parent 1ff7b59123
commit 308370c4df
7 changed files with 71 additions and 23 deletions

View File

@ -30,7 +30,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def feed def feed
@table_feed_fields = ["announcement.feed_name", :tags] @table_feed_fields = ["announcement.feed_name", :tags, "announcement.rssfeed"]
@feeds = BulletinFeed.all.asc(:created_at) @feeds = BulletinFeed.all.asc(:created_at)
end end

View File

@ -1,7 +1,50 @@
require "rss"
class AnnouncementFeedsController < ApplicationController class AnnouncementFeedsController < ApplicationController
include Admin::AnnouncementsHelper
def feed def feed
uid = params[:uid] 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 bf = BulletinFeed.find_by(:uid => uid) rescue nil
if !bf.nil? if !bf.nil?
tags = bf.tag_ids tags = bf.tag_ids
@ -57,28 +100,11 @@ class AnnouncementFeedsController < ApplicationController
end end
all_anns << a all_anns << a
end end
render :json => {"announcements" => all_anns, "tags" => tag_names}.to_json {
"announcements" => all_anns,
"tags" => tag_names
}
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["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 end

View File

@ -31,4 +31,7 @@
</div> </div>
</div> </div>
</td> </td>
<td>
<a href="/xhr/announcements/rssfeed/<%= feed.uid %>.rss" target="_blank">Rss Feed</a>
</td>
</tr> </tr>

View File

@ -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

View File

@ -13,6 +13,7 @@ en:
view_count: View Count view_count: View Count
add_new: Add New add_new: Add New
feed_name: Feed Name feed_name: Feed Name
rssfeed: Rss Feed Link
feed_list: Feed List feed_list: Feed List
approve: Approve approve: Approve
all_articles: All Articles all_articles: All Articles

View File

@ -14,6 +14,7 @@ zh_tw:
add_new: 新建 add_new: 新建
approve: 通過 approve: 通過
feed_name: Feed 標題 feed_name: Feed 標題
rssfeed: Rss Feed Link
feed_list: 訂閱清單 feed_list: 訂閱清單
all_articles: 文章列表 all_articles: 文章列表
announcement: 公告 announcement: 公告

View File

@ -23,6 +23,7 @@ Rails.application.routes.draw do
end end
get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed" get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed"
get "/xhr/announcements/rssfeed/:uid" => "announcement_feeds#rssfeed"
get "/xhr/announcements/feeds" => "announcement_feeds#feeds" get "/xhr/announcements/feeds" => "announcement_feeds#feeds"
end end