require "study_engine/config" module StudyEngine class Admin::AssessmentsController < Admin::ApplicationController before_action :require_admin, unless: -> { action_name == "index" && request.format.html? } expose :assessments, :study_name, :study_url, :events, :assessment, :report def index respond_to do |format| @assessments = current_user.admin? ? Assessment.find_each : Assessment.by_site(current_user.site_id).find_each @report = Report.new(assessments, params[:field] || "study_id", params[:direction] || "ASC") format.csv do @csv_options = { force_quotes: true } @output_encoding = "UTF-8" @streaming = true @filename = "#{params[:report]}-#{Time.now.utc.to_s(:db).parameterize.underscore}.csv" render params[:report] end format.html end end def new @assessments = ExistingAssessmentsForm.new end def create @assessments = ExistingAssessmentsForm.new(params[:study_engine_assessment]) if assessments.save redirect_to [:admin, :assessments], notice: "Assessment created!" else flash.now[:alert] = assessments.errors.full_messages.join(", ") render :new end end def edit if StudyID.new(params[:id]).valid? @assessments = UpdateAssessmentsForm.new(params[:id]) else @assessment = Assessment.find(params[:id]) end end def update if StudyID.new(params[:id]).valid? @assessments = UpdateAssessmentsForm.new(params[:id]) assessments.update_attributes! params[:study_engine_assessment] else @assessment = Assessment.find(params[:id]) assessment.update_attributes! params[:assessment].permit(:method_applied) end redirect_to [:admin, :assessments], notice: "Assessment has been updated!" end def destroy Assessment.destroy params[:id] redirect_to [:admin, :assessments], notice: "Assessment has been deleted!" end private def study_name StudyEngine.study_name end def study_url StudyEngine.study_url end def events StudyEngine.events end end end