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

76 lines
1.9 KiB
Ruby

# 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
def index
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
@bulletins = Bulletin.order_by(sort).with_categories(filters("category")).with_tags(filters("tag")).with_status(filters("status")).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def new
@tags =@module_app.tags
@categories = @module_app.categories.enabled
@statuses = []
@bulletin = Bulletin.new
end
def create
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"
end
def edit
@tags =@module_app.tags
@categories = @module_app.categories.enabled
@statuses = []
end
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
def destroy
@bulletin.destroy
redirect_to "/admin/announcements"
end
def delete
if params[:ids]
bulletins = Bulletin.any_in(:uid => params[:ids]).destroy_all
end
redirect_to "/admin/announcements"
end
private
def set_bulletin
@bulletin = Bulletin.find(params[:id])
end
def bulletin_params
params.require(:bulletin).permit!
end
end