adjust json : {web_link}/admin/announcement.json

This commit is contained in:
JiangRu 2015-03-17 18:17:01 +08:00
parent d5e85823fe
commit a40b9af2d1
2 changed files with 24 additions and 7 deletions

View File

@ -3,6 +3,8 @@ class Admin::BulletinsController < ApplicationController
before_filter :set_I18n before_filter :set_I18n
def get_bulletins def get_bulletins
page = Page.where(:module => "announcement").first rescue nil
# 頁次 # 頁次
page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i
# 每頁顯示的則數 # 每頁顯示的則數
@ -16,11 +18,9 @@ class Admin::BulletinsController < ApplicationController
bulletins = Bulletin.all bulletins = Bulletin.all
end end
bulletins = bulletins.where(:is_hot => params[:is_hot]) if !params[:is_hot].blank?
bulletins = bulletins.where(:category_id.in => params[:categories]) if !params[:categories].blank?
bulletins = bulletins.where(:tagged_ids.in => params[:tags]) if !params[:tags].blank?
bulletins = bulletins.where(:is_preview.in=>[false,nil]) bulletins = bulletins.where(:is_preview.in=>[false,nil])
bulletins = bulletins.where(:approved.ne => false , :rejected.ne => true)
bulletins = bulletins.where(:postdate.lt=>Time.now)
bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page) bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page)
bulletins = bulletins.collect do |b| bulletins = bulletins.collect do |b|
@ -66,13 +66,30 @@ class Admin::BulletinsController < ApplicationController
"image" => image, "image" => image,
"links" => links, "links" => links,
"files" => files, "files" => files,
"author" => author "author" => author,
"url" => "/#{I18n.locale.to_s + page.url}/#{b.to_param}"
} }
end end
# 計算總筆數 Start
if !params[:keyword].blank?
keyword = Regexp.new(".*"+params[:keyword]+".*")
bulletin_count = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
else
bulletin_count = Bulletin.all
end
bulletin_count = bulletin_count.where(:is_preview.in=>[false,nil])
bulletin_count = bulletin_count.where(:approved.ne => false , :rejected.ne => true)
bulletin_count = bulletin_count.where(:postdate.lt=>Time.now)
total_pages = bulletin_count.count
# End
render :json => { render :json => {
"bulletins" => bulletins "bulletins" => bulletins,
"bulletins_count" => bulletins.count,
"page_num" => page_num,
"total_pages" => total_pages,
}.to_json }.to_json
end end

View File

@ -7,7 +7,7 @@ Rails.application.routes.draw do
post 'announcement/preview', to: 'announcements#preview' post 'announcement/preview', to: 'announcements#preview'
get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview'
get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin'
get "announcement/bulletins.json" => "bulletins#get_bulletins" get 'announcement.json', to: 'bulletins#get_bulletins'
resources :announcements resources :announcements
end end