|
class MissionsController < ApplicationController
before_action :set_mission, only: [:show, :show_agents, :edit, :update, :destroy]
# GET /missions
# GET /missions.json
def index
@featured_missions = Mission.last
@open_missions = Mission.where("status = ? OR status = ?", 1, 2)
@finished_missions = Mission.where("status = ? OR status = ?", 3, 4)
end
def mission_control
@mission = Mission.find(params[:id])
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
end
def show_agent_details
@mission = Mission.friendly.find(params[:id])
@agent = @mission.mission_agents.find(params[:agent])
end
# GET /missions/new
def new
@mission = Mission.new
end
# GET /missions/1/edit
def edit
end
# POST /missions
# POST /missions.json
def create
@mission = Mission.new(mission_params)
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
# 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
mission = Mission.find(params[:id])
agent = mission.mission_agents.find(params[:agent])
candidate = MissionCandidate.create!(user: current_user, mission_agent: agent, status: 'closed', mode: 'take_agent_role')
agent.user = current_user
respond_to do |format|
if agent.save
format.html { redirect_to mission_agent_details_path(mission, agent), notice: (t 'mission.take_agent_profile_confirmation') }
format.json { head :no_content }
else
format.html { redirect_to mission_agent_details_path(mission, agent), alert: (t 'mission.take_agent_profile_error') }
format.json { render json: @mission.errors, status: :unprocessable_entity }
end
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)
end
end
|