|
class MissionsController < ApplicationController
before_action :set_mission, only: [:show, :edit, :update, :destroy, :launch, :mission_control]
before_action :authenticate_user!
layout 'front_end'
# GET /missions
# GET /missions.json
def index
@user = User.find(current_user.id)
@missions = @user.mission_agent_invites.where(:status => 'accepted')
@missions += @user.mission_agent_invites.where(:status => 'Completed')
@mission_control = Mission.where(:owner => @user)
@mission_invites = @user.mission_agent_invites.where(:status => 'Invited')
end
def list
@missions = Mission.all
end
# GET /missions/1
# GET /missions/1.json
def show
@agent = @mission.mission_agents.where(:user_id => current_user.id)
end
def mission_control
end
# GET /missions/new
def new
@mission = Mission.new
@mission.mission_agents.build
@mission.mission_agents.each do |agent|
agent.mission_agent_steps.build
end
end
# GET /missions/1/edit
def edit
end
# POST /missions
# POST /missions.json
def create
@mission = Mission.new(mission_params)
@mission.status = 'Initializing'
@mission.owner = current_user
respond_to do |format|
if @mission.save
format.html { redirect_to mission_control_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)
format.html { redirect_to mission_control_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
def launch
@mission.status = 'Launched'
respond_to do |format|
if @mission.save
@mission.mission_agents.each do |agent|
agent.assign_agent
end
format.html { redirect_to mission_control_path(@mission), notice: 'Mission launched!' }
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 validate_agent_step
@step = MissionAgentStep.find(params[:mission_agent_step][:id])
@step.proof = params[:mission_agent_step][:proof]
@step.status = 'Waiting Validation'
@mission = @step.mission_agent.mission
if @step.save
redirect_to mission_path(@mission), notice: 'Step completed!'
end
end
def mission_agent_step_validate_check
@step = MissionAgentStep.find(params[:id])
@step.validated = true
@step.validated_by = current_user
@step.completed = true
@step.status = 'Completed'
@step.save
@mission_completed = true
@step.mission_agent.mission_agent_steps.each do |s|
if s.completed == false
@mission_completed = false
end
end
if @mission_completed == true
@step.mission_agent.mission_agent_invites.where(:user => @step.mission_agent.user).last.update(:status => "Completed")
end
@mission = @step.mission_agent.mission
@mission.validade_completion
redirect_to mission_control_path(@mission), notice: 'Step Validated!'
end
def mission_agent_step_invalidate_check
@step = MissionAgentStep.find(params[:id])
@step.validated = false
@step.validated_by = current_user
@step.completed = false
@step.status = 'Incomplete'
@mission = @step.mission_agent.mission
if @step.save
redirect_to mission_control_path(@mission), notice: 'Step Invalidated!'
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_mission
@mission = Mission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def mission_params
params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agents_attributes => [:id, :mission_id, :description, :_destroy, :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :proof, :_destroy]] )
end
def mission_agent_params
params.require(:mission_agent).permit(:id, :mission_id, :description, :_destroy, :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :proof, :_destroy])
end
def mission_agent_step_params
params.require(:mission_agent_step).permit(:id, :mission_agent_id, :description, :proof, :_destroy)
end
end
|