Changes to have both json and rss, add categories

This commit is contained in:
chris 2013-09-17 19:57:54 +08:00
parent 64d07a0c95
commit 2d714dfa4b
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 # @module_app = ModuleApp.where(:title=>'Announcement').first
# end # 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_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 :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] before_filter :for_app_sub_manager,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
def index def index
@bulletin_categorys = get_categories_for_index("BulletinCategory") @bulletin_categorys = get_categories_for_index("BulletinCategory")
@ -53,6 +53,24 @@ class Panel::Announcement::BackEnd::BulletinCategorysController < OrbitBackendCo
render :json => JSON.pretty_generate(data) render :json => JSON.pretty_generate(data)
end 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 def get_bulletins_json
bulletin = BulletinCategory.find(params[:bulletin_category_id]).bulletins bulletin = BulletinCategory.find(params[:bulletin_category_id]).bulletins
p bulletin p bulletin

View File

@ -7,8 +7,8 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# before_filter :for_admin_only,:only => [:] # before_filter :for_admin_only,:only => [:]
# before_filter :for_app_manager,:only => [:index,:show,] # 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_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 :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_as_json] 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| before_filter :only => [ :new,:create,:edit,:update,:create] do |controller|
controller.get_categorys('BulletinCategory') controller.get_categorys('BulletinCategory')
@ -320,31 +320,37 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# - default: no category # - default: no category
# - specify: category_id=[ID] # - specify: category_id=[ID]
# pagination: # pagination:
# - default: no pagination # - default: 10
# - use it: paginate=true
# - specify the number of results: per=[number] # - specify the number of results: per=[number]
# - specify the page: page=[number] # - specify the page: page=[number]
# ================================================================ # ================================================================
def get_bulletins_as_json def get_bulletins
I18n.locale = params[:locale] || :zh_tw 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 = 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| respond_to do |format|
deadline = bulletin.deadline.nil? ? t(:no_deadline) : display_date_time(bulletin.deadline) format.json {
image_url = "#{'/' unless bulletin.image.url.start_with?('/')}#{bulletin.image.url}" custom_bulletins = @bulletins.inject([]) do |data, bulletin|
data << { deadline = bulletin.deadline.nil? ? t(:no_deadline) : display_date_time(bulletin.deadline)
title: bulletin.title, image_url = "#{'/' unless bulletin.image.url.start_with?('/')}#{bulletin.image.url}"
link: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin, category_id: bulletin.bulletin_category.id, locale: I18n.locale)}", data << {
image: "#{request.protocol}#{request.host_with_port}#{image_url}", title: bulletin.title,
category: bulletin.bulletin_category.title, link: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(bulletin, category_id: bulletin.bulletin_category.id, locale: I18n.locale)}",
content: bulletin.text, image: "#{request.protocol}#{request.host_with_port}#{image_url}",
postdate: display_date_time(bulletin.postdate), category: bulletin.bulletin_category.title,
deadline: deadline, content: bulletin.text,
tag: bulletin.sorted_tags.map{|tag| tag.name} 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 end
render json: JSON.pretty_generate(custom_bulletins)
end 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' get 'load_quick_edit'
end end
collection do collection do
get "get_bulletins_as_json" get "get_bulletins"
post "preview" post "preview"
put "preview" put "preview"
get 'delete' get 'delete'
@ -28,6 +28,7 @@ Rails.application.routes.draw do
resources :bulletin_categorys do resources :bulletin_categorys do
collection do collection do
get "get_bulletin_categories"
get 'get_categorys_json' get 'get_categorys_json'
end end
# if want to use json # if want to use json