announcement-test/app/controllers/admin/announcements_controller.rb

111 lines
3.0 KiB
Ruby
Raw Normal View History

2014-05-01 08:41:00 +00:00
# encoding: utf-8
class Admin::AnnouncementsController < OrbitAdminController
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_bulletin, only: [:edit, :destroy]
def initialize
super
@app_title = "announcement"
end
2014-05-01 08:41:00 +00:00
def index
@tags = @module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-01 08:41:00 +00:00
@filter_fields = {
:status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}],
:category=>@categories.map{|c| {:title=>c.title, :id=>c.id}},
:tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}}
}
end
2014-05-02 10:21:51 +00:00
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
2014-05-01 08:41:00 +00:00
def sort
unless params[:sort].blank?
case params[:sort]
when "status"
@sort = [[:is_top, params[:order]],
[:is_hot, params[:order]],
[:is_hidden,params[:order]]]
when "category"
@sort = {:category_id=>params[:order]}
when "title"
@sort = {:title=>params[:order]}
when "start_date"
@sort = {:postdate=>params[:order]}
when "end_date"
@sort = {:deadline=>params[:order]}
when "last_modified"
@sort = {:update_user_id=>params[:order]}
end
else
@sort = {:created_at=>'desc'}
end
@sort
end
2014-04-03 03:18:02 +00:00
def new
2014-05-01 08:41:00 +00:00
@tags =@module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-01 08:41:00 +00:00
@statuses = []
@bulletin = Bulletin.new
2014-04-03 03:18:02 +00:00
end
def create
2014-05-01 08:41:00 +00:00
bulletin = Bulletin.new(bulletin_params)
bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id
bulletin.save
redirect_to "/admin/announcements"
2014-04-03 03:18:02 +00:00
end
2014-05-01 08:41:00 +00:00
def edit
@tags =@module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-01 08:41:00 +00:00
@statuses = []
2014-04-10 03:52:59 +00:00
end
2014-05-01 08:41:00 +00:00
def update
uid = params[:id].split('-').last
bulletin = Bulletin.find_by(:uid=>uid)
bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags]
bulletin.update_attributes(bulletin_params)
bulletin.save
redirect_to "/admin/announcements"
end
2014-04-03 03:18:02 +00:00
2014-05-01 08:41:00 +00:00
def destroy
@bulletin.destroy
2014-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
end
def delete
if params[:ids]
bulletins = Bulletin.any_in(:uid => params[:ids]).destroy_all
2014-04-03 03:18:02 +00:00
end
2014-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
end
private
def set_bulletin
@bulletin = Bulletin.find(params[:id])
2014-05-01 08:41:00 +00:00
end
def bulletin_params
params.require(:bulletin).permit!
2014-05-01 08:41:00 +00:00
end
2014-04-03 03:18:02 +00:00
end