|
class MissionsController < ApplicationController
before_action :set_mission, only: [:show, :show_agents, :edit, :update, :destroy]
# GET /missions
# GET /missions.json
def index
@featured_missions = Mission.where(status: 2).last
@open_missions = Mission.where(status: 2)
@open_missions.delete_if { |m| m == @featured_missions }
@finished_missions = Mission.where("status = ? OR status = ?", 3, 4).order('launch_date ASC').limit(3)
@finished_missions.delete_if { |m| m == @featured_missions }
@analytics.track('Mission list view')
end
def mission_control
@mission = Mission.friendly.find(params[:id])
@submissions = @mission.step_submissions
if current_user != @mission.owner
redirect_to missions_path, alert: (t 'mission.access_denied_error')
end
end
# GET /missions/1
# GET /missions/1.json
def show
end
def show_agents
@agents = @mission.mission_agents.order('instance_source_id DESC')
end
def show_agent_details
@mission = Mission.friendly.find(params[:id])
@agent = @mission.mission_agents.friendly.find(params[:agent])
end
# GET /missions/new
def new
@mission = Mission.new
@mission.owner = current_user
end
# GET /missions/1/edit
def edit
end
# POST /missions
# POST /missions.json
def create
@mission = Mission.new(mission_params)
@mission.owner = current_user
@analytics.track('Mission created')
respond_to do |format|
if @mission.save
format.html { redirect_to mission_agents_path(@mission), notice: 'Mission was successfully created.' }
format.json { render action: 'show', status: :created, location: @mission }
else
format.html { render action: 'new' }
format.json { render json: @mission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /missions/1
# PATCH/PUT /missions/1.json
def update
respond_to do |format|
if @mission.update(mission_params)
puts mission_params
format.html { redirect_to mission_editor_launch_path(@mission), notice: 'Mission was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @mission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /missions/1
# DELETE /missions/1.json
def destroy
@mission.destroy
respond_to do |format|
format.html { redirect_to missions_url }
format.json { head :no_content }
end
end
def take_agent_role
if user_signed_in?
mission = Mission.friendly.find(params[:id])
agent = mission.mission_agents.friendly.find(params[:agent])
candidate = MissionCandidate.create!(user: current_user, mission_agent: agent, status: 1, mode: 'take_agent_role')
agents = []
if agent.instances > 1
new_agent = agent.deep_clone :include => [{:agent_steps => :step_validations}]
new_agent.user = current_user
new_agent.instances = 1
new_agent.instance_source = agent
agent.instances = agent.instances - 1
agents << agent
agents << new_agent
else
agent.user = current_user
new_agent = agent
agents << new_agent
end
if agents.each { |agent| agent.save }
@analytics.track('Agent role selected')
redirect_to mission_agent_details_path(mission, new_agent), notice: (t 'mission.take_agent_profile_confirmation')
else
redirect_to mission_agent_details_path(mission, new_agent), alert: (t 'mission.take_agent_profile_error')
end
else
redirect_to new_user_session_path, notice: (t 'user.not_logged_in')
end
end
def step_submission
mission = Mission.friendly.find(params[:id])
agent = mission.mission_agents.friendly.find(params[:agent])
step = agent.agent_steps.where(id: params[:step]).last
submission = step.create_submission
respond_to do |format|
if submission.save
@analytics.track('Step submitted')
format.html { redirect_to mission_agent_details_path(mission, agent), notice: (t 'mission.step_submission_confirmation') }
format.json { head :no_content }
else
format.html { redirect_to mission_agent_details_path(mission, agent), alert: (t 'mission.step_submission_error') }
format.json { render json: @mission.errors, status: :unprocessable_entity }
end
end
end
def step_submission_with_validations
mission = Mission.friendly.find(params[:id])
agent = mission.mission_agents.friendly.find(params[:agent])
step = agent.agent_steps.where(id: params[:step]).last
submission = step.create_submission_with_validations(step_submission_params, params[:step_submission][:submission_contents_attributes].values)
respond_to do |format|
if submission.save
@analytics.track('Step submitted')
format.html { redirect_to mission_agent_details_path(mission, agent), notice: (t 'mission.step_submission_confirmation') }
format.json { head :no_content }
else
format.html { redirect_to mission_agent_details_path(mission, agent), alert: (t 'mission.step_submission_error') }
format.json { render json: @mission.errors, status: :unprocessable_entity }
end
end
end
def accept_step_submission
mission = Mission.friendly.find(params[:id])
if current_user != mission.owner
redirect_to missions_path, alert: (t 'mission.access_denied_error')
else
submission = StepSubmission.find(params[:step_submission_id])
submission.validated = true
submission.validated_by = current_user
submission.date_validated = Time.now
submission.save
submission.agent_step.update(completed: true)
@analytics.track('Step accepted')
Resque.enqueue(SendStepNotification, submission.agent_step.id, 'validated')
#MissionMailer.step_validation_notification(submission.agent_step).deliver
mission.check_for_completion
redirect_to mission_control_path(mission)
end
end
def reject_step_submission
mission = Mission.friendly.find(params[:id])
if current_user != mission.owner
redirect_to missions_path, alert: (t 'mission.access_denied_error')
else
submission = StepSubmission.find(params[:step_submission_id])
submission.validated = false
submission.validated_by = current_user
submission.date_validated = Time.now
submission.save
@analytics.track('Step denied')
Resque.enqueue(SendStepNotification, submission.agent_step.id, 'denied')
redirect_to mission_control_path(mission)
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_mission
@mission = Mission.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def mission_params
params.require(:mission).permit(:mission_agents_id, :title, :objective, :briefing, :owner_id, :status, :launched, :language, :cover_img, :slug, :end_date, :duration_scale, :duration_number, :video)
end
def step_submission_params
params.require(:step_submission).permit(:agent_step_id, :validated, :validated_by_id, :date_validated, :created_at, :updated_at, :language, :cover_img, :slug, :end_date, :submission_contents_attributes => [:submission_content_id, :submission_type, :created_at, :updated_at, :submission_attributes => [:content, :submission_content_id]])
end
end
|