# encoding: utf-8 class Admin::ActivesController < OrbitAdminController include Admin::ActivesHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_act, only: [:edit, :destroy, :act_signup, :export] def initialize super @app_title = "active" end def filter_fields(categories) { :category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}} } end def index @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories) @table_fields = [:category, 'act.title', 'act.sign_up_time_range', 'act.act_time_range', 'act.postdate', 'act.end_date', 'act.sign_up_count', 'act.export'] if !params[:sort].blank? if params[:sort] == 'act_time_range' sort = {:act_start_date.to_sym=>params[:order]} elsif params[:sort] == 'sign_up_time_range' sort = {:sign_start_date.to_sym=>params[:order]} else sort = {params[:sort].to_sym=>params[:order]} end else sort = {:sign_start_date=>"desc"} end @acts = Act.order_by(sort).with_categories(filters("category")) @acts = search_data(@acts,[:title]).page(params[:page]).per(10) if request.xhr? render :partial => "index" end end def new @tags = @module_app.tags @act = Act.new(id: nil) end def create if !act_params['act_links_attributes'].nil? act_params['act_links_attributes'].each do |idx,link| act_params['act_links_attributes'].delete(idx.to_s) if link['url'].blank? end end act = Act.new(act_params) act.create_user_id = current_user.id act.update_user_id = current_user.id act.save redirect_to params['referer_url'] end def edit if can_edit_or_delete?(@act) @categories = @module_app.categories.enabled else render_401 end end def update uid = params[:id].split('-').last act = Act.find_by(:uid=>uid) if !act_params['act_links_attributes'].nil? act_params['act_links_attributes'].each do |idx,link| act_params['act_links_attributes'].delete(idx.to_s) if link['url'].blank? end end act.update_attributes(act_params) redirect_to params['referer_url'] end def destroy @act.destroy redirect_to "/admin/actives" end def export title = @act.title.gsub(/\\|\//,'_') respond_to do |format| format.xlsx { response.headers['Content-Disposition'] = "attachment; filename=\"#{title}.xlsx\"" } end end def act_signup @act_signups = ActSignup.where(:act_id => @act.id).page(params[:page]).per(10) end def edit_act_signup @act_signup = ActSignup.find(params[:id]) @act = Act.find(@act_signup.act_id) end def delete_act_signup @act_signup = ActSignup.find(params[:id]) @act_id = @act_signup.act_id @act_signup.destroy redirect_to "/admin/actives/@act_id.to_s/act_signup" end private def set_act @act = Act.find(params[:id]) end def act_params params.require(:act).permit! end end