From a40b9af2d1f3bf93ec878082d27ebb32915167e4 Mon Sep 17 00:00:00 2001 From: JiangRu Date: Tue, 17 Mar 2015 18:17:01 +0800 Subject: [PATCH] adjust json : {web_link}/admin/announcement.json --- app/controllers/admin/bulletins_controller.rb | 29 +++++++++++++++---- config/routes.rb | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/bulletins_controller.rb b/app/controllers/admin/bulletins_controller.rb index 07c2e54..97453e9 100644 --- a/app/controllers/admin/bulletins_controller.rb +++ b/app/controllers/admin/bulletins_controller.rb @@ -3,6 +3,8 @@ class Admin::BulletinsController < ApplicationController before_filter :set_I18n def get_bulletins + page = Page.where(:module => "announcement").first rescue nil + # 頁次 page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i # 每頁顯示的則數 @@ -16,11 +18,9 @@ class Admin::BulletinsController < ApplicationController bulletins = Bulletin.all 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(: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.collect do |b| @@ -66,13 +66,30 @@ class Admin::BulletinsController < ApplicationController "image" => image, "links" => links, "files" => files, - "author" => author + "author" => author, + "url" => "/#{I18n.locale.to_s + page.url}/#{b.to_param}" } 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 => { - "bulletins" => bulletins + "bulletins" => bulletins, + "bulletins_count" => bulletins.count, + "page_num" => page_num, + "total_pages" => total_pages, }.to_json end diff --git a/config/routes.rb b/config/routes.rb index 65ff35a..7f9a09b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ Rails.application.routes.draw do post 'announcement/preview', to: 'announcements#preview' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' - get "announcement/bulletins.json" => "bulletins#get_bulletins" + get 'announcement.json', to: 'bulletins#get_bulletins' resources :announcements end