2019-04-05 22:40:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-06 20:41:44 +00:00
|
|
|
# API para posiciones
|
|
|
|
#
|
2019-04-05 22:40:28 +00:00
|
|
|
# Las posiciones solo se pueden agregar a su consenso
|
2019-04-06 20:41:44 +00:00
|
|
|
#
|
|
|
|
# @see PiratasController#create
|
2019-04-05 22:40:28 +00:00
|
|
|
class PosicionesController < ApplicationController
|
2019-04-06 20:41:44 +00:00
|
|
|
# Necesitamos autenticarnos
|
2019-04-06 19:45:07 +00:00
|
|
|
before_action :authenticate!
|
2019-08-03 16:23:52 +00:00
|
|
|
before_action :find_barca!
|
2019-04-06 19:45:07 +00:00
|
|
|
|
2019-08-03 16:23:52 +00:00
|
|
|
# POST /barcas/:barca_id/consensos/:consenso_id/posiciones
|
2019-04-06 20:41:44 +00:00
|
|
|
#
|
|
|
|
# Crea una posición dentro de un consenso
|
|
|
|
#
|
|
|
|
# @param :consenso_id [Integer] El ID del consenso
|
2019-08-03 21:44:28 +00:00
|
|
|
# @param :posicion [Hash] { posicion: { estado: @string,
|
|
|
|
# comentario: @string } }
|
2019-04-21 04:07:14 +00:00
|
|
|
# @return [Hash] { id: @int, estado: @string, comentario: @string,
|
|
|
|
# pirata: @pirata }
|
2019-04-05 22:40:28 +00:00
|
|
|
def create
|
2019-08-03 22:45:04 +00:00
|
|
|
find_consenso!
|
|
|
|
|
2019-04-06 19:45:07 +00:00
|
|
|
@posicion = @consenso.try(:posiciones).try(:build, posicion_params)
|
|
|
|
@posicion.try(:pirata=, current_pirata)
|
2019-04-05 22:40:28 +00:00
|
|
|
|
2019-04-06 19:45:07 +00:00
|
|
|
if @posicion.try(:save)
|
2019-08-03 22:45:04 +00:00
|
|
|
notify subject: :create
|
2019-04-06 19:45:07 +00:00
|
|
|
render status: :created
|
|
|
|
else
|
2019-04-20 15:49:31 +00:00
|
|
|
render json: @posicion.try(:errors).try(:messages),
|
2019-04-06 19:45:07 +00:00
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
2019-04-05 22:40:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def posicion_params
|
|
|
|
params.require(:posicion).permit(:estado, :comentario)
|
|
|
|
end
|
2019-08-03 16:23:52 +00:00
|
|
|
|
|
|
|
def find_barca!
|
|
|
|
@barca = Barca.find(params[:barca_id])
|
|
|
|
end
|
2019-08-03 22:45:04 +00:00
|
|
|
|
|
|
|
def find_consenso!
|
|
|
|
@consenso = @barca.consensos.find(params[:consenso_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_subject(view)
|
|
|
|
I18n.t("posiciones.#{view}.subject",
|
|
|
|
barca: @barca.nombre,
|
|
|
|
consenso: @consenso.titulo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_posicion(estado)
|
|
|
|
I18n.t("pirata.posiciones.tercera_persona.#{estado}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_message(view)
|
|
|
|
I18n.t("posiciones.#{view}.message",
|
|
|
|
consenso: @consenso.texto[0..140],
|
|
|
|
posicion: get_posicion(@posicion.estado),
|
|
|
|
comentario: @posicion.comentario,
|
|
|
|
nick: current_pirata.nick)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_endpoint(_)
|
|
|
|
barca_consenso_path(@barca, @consenso)
|
|
|
|
end
|
2019-04-05 22:40:28 +00:00
|
|
|
end
|