osem-fcy/app/controllers/admin/questions_controller.rb

90 lines
3.3 KiB
Ruby
Raw Normal View History

2014-08-06 21:14:00 +03:00
module Admin
class QuestionsController < ApplicationController
2014-08-12 11:51:59 +03:00
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource through: :conference, except: [:new, :create]
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def index
2014-08-12 11:51:59 +03:00
authorize! :update, Question.new(conference_id: @conference.id)
2014-08-12 15:48:29 +03:00
@questions = Question.where(global: true).all | Question.where(conference_id: @conference.id)
2014-08-06 21:14:00 +03:00
@questions_conference = @conference.questions
@new_question = @conference.questions.new
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def new
2014-08-12 11:51:59 +03:00
@question = Question.new(conference_id: @conference.id)
authorize! :create, @question
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def create
@question = @conference.questions.new(params[:question])
@question.conference_id = @conference.id
2014-08-12 11:51:59 +03:00
authorize! :create, @question
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
respond_to do |format|
if @conference.save
format.html { redirect_to admin_conference_questions_path, notice: 'Question was successfully created.' }
else
flash[:error] = "Oops, couldn't save. Question and answer(s) have titles?"
format.html { redirect_to admin_conference_questions_path }
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# GET questions/1/edit
def edit
2014-08-12 11:51:59 +03:00
if @question.global == true && !(current_user.has_role? :organizer, @conference)
2014-08-12 15:48:29 +03:00
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), alert: "Sorry, you cannot edit global questions. Create a new one.")
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# PUT questions/1
def update
if @question.update_attributes(params[:question])
2014-08-12 15:48:29 +03:00
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), notice: "Question '#{@question.title}' for #{@conference.short_title} successfully updated.")
2014-08-06 21:14:00 +03:00
else
2014-08-12 15:48:29 +03:00
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), notice: "Update of questions for #{@conference.short_title} failed.")
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# Update questions used for the conference
def update_conference
if @conference.update_attributes(params[:conference])
2014-08-12 15:48:29 +03:00
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), notice: "Questions for #{@conference.short_title} successfully updated.")
2014-08-06 21:14:00 +03:00
else
2014-08-12 15:48:29 +03:00
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), notice: "Update of questions for #{@conference.short_title} failed.")
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# DELETE questions/1
def destroy
2014-02-09 18:05:57 +02:00
2014-08-12 11:51:59 +03:00
if can? :destroy, @question
2014-08-06 21:14:00 +03:00
# Do not delete global questions
if @question.global == false
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# Delete question and its answers
begin
Question.transaction do
2014-02-09 18:05:57 +02:00
2014-08-12 11:51:59 +03:00
@question.destroy
2014-08-06 21:14:00 +03:00
@question.answers.each do |a|
a.delete
end
flash[:notice] = "Deleted question: #{@question.title} and its answers: #{@question.answers.map {|a| a.title}.join ','}"
2014-08-12 11:51:59 +03:00
end
2014-08-06 21:14:00 +03:00
rescue ActiveRecord::RecordInvalid
flash[:error] = "Could not delete question."
2014-07-21 14:27:32 +02:00
end
2014-08-06 21:14:00 +03:00
else
flash[:error] = "You cannot delete global questions."
2014-02-09 18:05:57 +02:00
end
else
2014-08-06 21:14:00 +03:00
flash[:error] = "You must be an admin to delete a question."
2014-02-09 18:05:57 +02:00
end
2014-08-12 15:48:29 +03:00
@questions = Question.where(global: true).all | Question.where(conference_id: @conference.id)
2014-08-06 21:14:00 +03:00
@questions_conference = @conference.questions
end
2014-02-09 18:05:57 +02:00
end
2014-06-30 17:07:53 +02:00
end