From 9da9284a7f88e3006878ee8bff1d5cf325158cd2 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 16 Sep 2013 02:53:16 +0800 Subject: [PATCH] First version of new feed for announcement --- .../back_end/bulletins_controller.rb | 39 ++++++++++++++++++- .../announcement/config/routes.rb | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb index edc7c012b..3339f46de 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb @@ -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] - before_filter :for_app_sub_manager,:except => [: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 :only => [ :new,:create,:edit,:update,:create] do |controller| controller.get_categorys('BulletinCategory') @@ -312,6 +312,41 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]) end + # ================================================================ + # language: + # - default: zh_tw + # - specify language: locale=[string] + # category: + # - default: no category + # - specify: category_id=[ID] + # pagination: + # - default: no pagination + # - use it: paginate=true + # - specify the number of results: per=[number] + # - specify the page: page=[number] + # ================================================================ + def get_bulletins_as_json + I18n.locale = params[:locale] || :zh_tw + bulletins = params[:category_id] ? Bulletin.where(bulletin_category_id: params[:category_id]).order_by(:created_at, :desc) : Bulletin.admin_manager_all.order_by(:created_at, :desc) + bulletins = Kaminari.paginate_array(bulletins).page(params[:page]).per(params[:per] || 10) if params[:paginate] + + 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) + end + protected diff --git a/vendor/built_in_modules/announcement/config/routes.rb b/vendor/built_in_modules/announcement/config/routes.rb index adb637038..68d11ef02 100644 --- a/vendor/built_in_modules/announcement/config/routes.rb +++ b/vendor/built_in_modules/announcement/config/routes.rb @@ -17,6 +17,7 @@ Rails.application.routes.draw do get 'load_quick_edit' end collection do + get "get_bulletins_as_json" post "preview" put "preview" get 'delete'