forked from Trustie/forgeplus
148 lines
5.1 KiB
Ruby
148 lines
5.1 KiB
Ruby
class Admins::PlatformCommunicatesController < Admins::BaseController
|
|
before_action :get_communicate, only: [:edit, :update, :destroy, :show, :online_switch, :new_vote, :create_vote, :edit_vote, :update_vote]
|
|
|
|
def index
|
|
params[:location] = "pc"
|
|
sort_by = PlatformCommunicate.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'order_index'
|
|
sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
|
|
q = PlatformCommunicate.where(location: params[:location]).ransack(title_cont: params[:search])
|
|
communicates = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
|
|
@communicates = kaminari_paginate(communicates)
|
|
end
|
|
|
|
def applet
|
|
params[:location] = "applet"
|
|
sort_by = PlatformCommunicate.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'order_index'
|
|
sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
|
|
q = PlatformCommunicate.where(location: params[:location]).ransack(title_cont: params[:search])
|
|
communicates = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
|
|
@communicates = kaminari_paginate(communicates)
|
|
end
|
|
|
|
def show
|
|
watchers = @communicate.watchers
|
|
if params[:search].present?
|
|
like_sql = 'users.lastname LIKE :keyword OR users.nickname LIKE :keyword OR users.mail LIKE :keyword OR users.phone LIKE :keyword OR users.login LIKE :keyword OR users.company_name LIKE :keyword'
|
|
watchers = watchers.left_joins(:user).where(like_sql, keyword: "%#{params[:search]}%")
|
|
end
|
|
if params[:user_type].present?
|
|
watchers = watchers.left_joins(:user).where("users.user_type = ?", params[:user_type])
|
|
end
|
|
if params[:professional_field].present?
|
|
watchers = watchers.left_joins(:user).where("users.professional_field = ?", params[:professional_field])
|
|
end
|
|
@watchers = kaminari_paginate watchers
|
|
end
|
|
|
|
def new
|
|
location = params[:location] || "pc"
|
|
@communicate = PlatformCommunicate.new(location: location)
|
|
end
|
|
|
|
def create
|
|
@communicate = PlatformCommunicate.new(communicate_params)
|
|
if @communicate.save
|
|
redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:success] = '创建成功'
|
|
else
|
|
redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:danger] = "创建失败"
|
|
end
|
|
end
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
def update
|
|
@communicate.attributes = communicate_params
|
|
if @communicate.save
|
|
redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:success] = '更新成功'
|
|
else
|
|
redirect_to @communicate.location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:danger] = '更新失败'
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
location = @communicate.location
|
|
if @communicate.destroy
|
|
redirect_to location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:success] = '删除成功'
|
|
else
|
|
redirect_to location == 'pc' ? "/admins/platform_communicates?location=pc" : "/admins/platform_communicates/applet?location=applet"
|
|
flash[:danger] = '删除失败'
|
|
end
|
|
end
|
|
|
|
def online_switch
|
|
if @communicate.status
|
|
@communicate.update_attributes!(status: false)
|
|
else
|
|
@communicate.update_attributes!(status: true)
|
|
end
|
|
# render_ok
|
|
end
|
|
|
|
def new_vote
|
|
@vote = Vote.new(platform_communicate_id: @communicate.id)
|
|
2.times { @vote.vote_options.build }
|
|
end
|
|
|
|
def create_vote
|
|
@vote = Vote.new(platform_communicate_id: @communicate.id)
|
|
@vote.attributes = vote_params
|
|
|
|
if @vote.save
|
|
redirect_to applet_admins_platform_communicates_path, notice: '投票创建成功'
|
|
else
|
|
# 确保至少有2个选项显示在表单中
|
|
@vote.vote_options.build if @vote.vote_options.empty?
|
|
@vote.vote_options.build if @vote.vote_options.size == 1
|
|
|
|
render :new_vote
|
|
end
|
|
end
|
|
|
|
def edit_vote
|
|
@vote = @communicate.vote
|
|
# 确保至少有2个选项显示在表单中
|
|
@vote.vote_options.build if @vote.vote_options.empty?
|
|
@vote.vote_options.build if @vote.vote_options.size == 1
|
|
end
|
|
|
|
def update_vote
|
|
@vote = @communicate.vote
|
|
if @vote.update(vote_params)
|
|
redirect_to applet_admins_platform_communicates_path, notice: '投票更新成功'
|
|
else
|
|
render :edit_vote
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def get_communicate
|
|
@communicate = PlatformCommunicate.find_by_id(params[:id])
|
|
end
|
|
|
|
def communicate_params
|
|
params.require(:platform_communicate).permit!
|
|
end
|
|
|
|
def vote_params
|
|
params.require(:vote).permit(
|
|
:description,
|
|
:is_multiple_choice,
|
|
:max_choice_vote_options_count,
|
|
:due_at,
|
|
vote_options_attributes: [
|
|
:id,
|
|
:title,
|
|
:order_index,
|
|
:_destroy
|
|
]
|
|
)
|
|
end
|
|
end |