From 4ae1040fc84dd70e8747f0b8db6811155fbe6563 Mon Sep 17 00:00:00 2001 From: manson Date: Fri, 2 May 2014 18:21:51 +0800 Subject: [PATCH] Announcement backend index page filter --- .../admin/announcements_controller.rb | 18 ++- app/controllers/announcements_controller.rb | 17 ++- app/helpers/admin/announcements_helper.rb | 3 + app/models/bulletin.rb | 9 ++ app/views/admin/announcements/index.html.erb | 130 +++++++++--------- .../admin/announcements/index_table.html.erb | 36 +++++ config/routes.rb | 3 +- 7 files changed, 133 insertions(+), 83 deletions(-) create mode 100644 app/views/admin/announcements/index_table.html.erb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 15d75c9..e8c2954 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -8,13 +8,9 @@ class Admin::AnnouncementsController < OrbitAdminController @app_title = "announcement" end - def index @tags = @module_app.tags @categories = @module_app.categories - @statuses = [] - @bulletins = Kaminari.paginate_array(Bulletin.order_by(sort).all).page(params[:page]).per(10) - @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] @filter_fields = { :status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}], @@ -23,6 +19,19 @@ class Admin::AnnouncementsController < OrbitAdminController } end + def index_table + status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue [] + categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue [] + tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue [] + + @bulletins = Kaminari.paginate_array( + Bulletin.order_by(sort).with_categories(categories).with_tags(tags).with_status(status) + ).page(params[:page]).per(10) + + @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] + render :layout => false + end + def sort unless params[:sort].blank? case params[:sort] @@ -47,7 +56,6 @@ class Admin::AnnouncementsController < OrbitAdminController @sort end - def new @tags =@module_app.tags @categories = @module_app.categories diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 0fee79e..a422585 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,7 +1,7 @@ class AnnouncementsController < ApplicationController def index - announcements = Bulletin.order_by(:created_at=>'desc').filter_by_categories + announcements = Bulletin.can_display.order_by(:created_at=>'desc').filter_by_categories anns = announcements.collect do |a| { "title" => a.title, @@ -17,7 +17,7 @@ class AnnouncementsController < ApplicationController end def widget - announcements = Bulletin.order_by(:created_at=>'desc').filter_by_categories + announcements = Bulletin.can_display.order_by(:created_at=>'desc').filter_by_categories anns = announcements.collect do |a| { "title" => a.title, @@ -27,7 +27,6 @@ class AnnouncementsController < ApplicationController } end { - "extras" => {"link_to_index" => "index","name" => "Harry"}, "data" => anns } end @@ -35,12 +34,12 @@ class AnnouncementsController < ApplicationController def show params = OrbitHelper.params - announcement = Bulletin.find_by(:uid=>params[:uid]) - { - "title" => announcement.title, - "image" => (announcement.image.blank? ? "" : announcement.image.url), - "body" => announcement.text - } + announcement = Bulletin.can_display.find_by(:uid=>params[:uid]) + { + "title" => announcement.title, + "image" => (announcement.image.blank? ? "" : announcement.image.url), + "body" => announcement.text + } end end diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 5973e00..a1dc7ce 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -1,2 +1,5 @@ module Admin::AnnouncementsHelper + def page_for_bulletin(bulletin) + request.protocol+(request.host_with_port+Page.where(:categories.in=>[bulletin.category.id.to_s]).first.url+'/'+bulletin.to_param).gsub('//','/') + end end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 62fff8b..4864bde 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -38,4 +38,13 @@ class Bulletin self.update_user_id = user.id end + def self.with_status(status) + status = [status].flatten + self.any_of(status.map{|s| {s.to_sym=>true}}) + end + + def self.can_display() + self.any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}) + end + end diff --git a/app/views/admin/announcements/index.html.erb b/app/views/admin/announcements/index.html.erb index a17147a..f846c56 100644 --- a/app/views/admin/announcements/index.html.erb +++ b/app/views/admin/announcements/index.html.erb @@ -1,34 +1,3 @@ - - <% content_for :right_nav do %>