Changes to have both json and rss, add categories

This commit is contained in:
chris 2013-09-17 19:57:54 +08:00 committed by saurabhbhatia
parent afe6972dfa
commit d95b050e57
5 changed files with 85 additions and 23 deletions

View File

@ -9,12 +9,12 @@ class Panel::Announcement::BackEnd::BulletinCategorysController < OrbitBackendCo
# @module_app = ModuleApp.where(:title=>'Announcement').first
# end
# =======
before_filter :for_app_manager,:except => [:index,:get_categorys_json,:get_bulletins_json]
before_filter :for_app_manager,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
before_filter :force_order_for_visitor,:only=>[:index,:get_categorys_json,:get_bulletins_json]
before_filter :force_order_for_user,:except => [:index,:get_categorys_json,:get_bulletins_json]
before_filter :for_app_sub_manager,:except => [:index,:get_categorys_json,:get_bulletins_json]
before_filter :force_order_for_user,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
before_filter :for_app_sub_manager,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
def index
@bulletin_categorys = get_categories_for_index("BulletinCategory")
@ -53,6 +53,24 @@ class Panel::Announcement::BackEnd::BulletinCategorysController < OrbitBackendCo
render :json => JSON.pretty_generate(data)
end
def get_bulletin_categories
I18n.locale = params[:locale] || :zh_tw
@categories = BulletinCategory.all.order_by(:title, :asc)
respond_to do |format|
format.json {
custom_categories = @categories.inject([]) do |data, category|
data << {
title: category.title,
link: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletins_path(bulletin_category_id: category.id, locale: I18n.locale)}",
link_json: "#{request.protocol}#{request.host_with_port}#{get_bulletins_panel_announcement_back_end_bulletins_path(category_id: category.id, locale: I18n.locale)}"
}
end
render json: JSON.pretty_generate(custom_categories)
}
format.rss {}
end
end
def get_bulletins_json
bulletin = BulletinCategory.find(params[:bulletin_category_id]).bulletins
p bulletin

View File

@ -7,8 +7,8 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# before_filter :for_admin_only,:only => [:]
# before_filter :for_app_manager,:only => [:index,:show,]
before_filter :force_order_for_visitor,:only=>[:index,:show,:get_sorted_and_filtered_bulletins]
before_filter :force_order_for_user,:except => [:index,:show,:get_sorted_and_filtered_bulletins, :get_bulletins_as_json]
before_filter :for_app_sub_manager,:except => [:index,:show,:get_sorted_and_filtered_bulletins, :get_bulletins_as_json]
before_filter :force_order_for_user,:except => [:index,:show,:get_sorted_and_filtered_bulletins, :get_bulletins]
before_filter :for_app_sub_manager,:except => [:index,:show,:get_sorted_and_filtered_bulletins, :get_bulletins]
before_filter :only => [ :new,:create,:edit,:update,:create] do |controller|
controller.get_categorys('BulletinCategory')
@ -320,31 +320,37 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# - default: no category
# - specify: category_id=[ID]
# pagination:
# - default: no pagination
# - use it: paginate=true
# - default: 10
# - specify the number of results: per=[number]
# - specify the page: page=[number]
# ================================================================
def get_bulletins_as_json
def get_bulletins
I18n.locale = params[:locale] || :zh_tw
bulletins = params[:category_id] ? Bulletin.where(bulletin_category_id: params[:category_id]).order_by(:created_at, :desc) : Bulletin.all.order_by(:created_at, :desc)
bulletins = Kaminari.paginate_array(bulletins).page(params[:page]).per(params[:per] || 10) if params[:paginate]
@bulletins = Kaminari.paginate_array(bulletins).page(params[:page]).per(params[:per] || 10)
custom_bulletins = bulletins.inject([]) do |data, bulletin|
deadline = bulletin.deadline.nil? ? t(:no_deadline) : display_date_time(bulletin.deadline)
image_url = "#{'/' unless bulletin.image.url.start_with?('/')}#{bulletin.image.url}"
data << {
title: bulletin.title,
link: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin, category_id: bulletin.bulletin_category.id, locale: I18n.locale)}",
image: "#{request.protocol}#{request.host_with_port}#{image_url}",
category: bulletin.bulletin_category.title,
content: bulletin.text,
postdate: display_date_time(bulletin.postdate),
deadline: deadline,
tag: bulletin.sorted_tags.map{|tag| tag.name}
respond_to do |format|
format.json {
custom_bulletins = @bulletins.inject([]) do |data, bulletin|
deadline = bulletin.deadline.nil? ? t(:no_deadline) : display_date_time(bulletin.deadline)
image_url = "#{'/' unless bulletin.image.url.start_with?('/')}#{bulletin.image.url}"
data << {
title: bulletin.title,
link: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin, category_id: bulletin.bulletin_category.id, locale: I18n.locale)}",
image: "#{request.protocol}#{request.host_with_port}#{image_url}",
category: bulletin.bulletin_category.title,
content: bulletin.text,
postdate: display_date_time(bulletin.postdate),
deadline: deadline,
tag: bulletin.sorted_tags.map{|tag| tag.name}
}
end
render json: JSON.pretty_generate(custom_bulletins)
}
format.rss {
@category = BulletinCategory.find(params[:category_id]) rescue nil
}
end
render json: JSON.pretty_generate(custom_bulletins)
end

View File

@ -0,0 +1,16 @@
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
xml.title t('announcement.announcement') + ' ' + t('announcement.categories')
xml.link "#{request.protocol}#{request.host_with_port}#{get_bulletin_categories_panel_announcement_back_end_bulletin_categorys_path(format: 'rss')}"
@categories.each do |category|
xml.item do
xml.title category.title
xml.pubDate category.created_at.to_s(:rfc822)
xml.link "#{request.protocol}#{request.host_with_port}#{get_bulletins_panel_announcement_back_end_bulletins_path(bulletin_category_id: category.id, locale: I18n.locale, format: 'rss')}"
xml.guid "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletins_path(bulletin_category_id: category.id)}"
end
end
end
end

View File

@ -0,0 +1,21 @@
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
if @category
xml.title @category.title
else
xml.title t('announcement.announcement')
end
xml.link "#{request.protocol}#{request.host_with_port}#{get_bulletins_panel_announcement_back_end_bulletins_path(bulletin_category_id: @category.try(:id))}.rss"
@bulletins.each do |bulletin|
xml.item do
xml.title bulletin.title
xml.pubDate bulletin.postdate.to_s(:rfc822)
xml.description bulletin.text
xml.link "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin, category_id: bulletin.bulletin_category.id, locale: I18n.locale)}"
xml.guid "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin)}"
end
end
end
end

View File

@ -17,7 +17,7 @@ Rails.application.routes.draw do
get 'load_quick_edit'
end
collection do
get "get_bulletins_as_json"
get "get_bulletins"
post "preview"
put "preview"
get 'delete'
@ -28,6 +28,7 @@ Rails.application.routes.draw do
resources :bulletin_categorys do
collection do
get "get_bulletin_categories"
get 'get_categorys_json'
end
# if want to use json