Skip to content

Commit

Permalink
Merge branch 'f741' into 'main'
Browse files Browse the repository at this point in the history
https://gitlab.com/pasosdeJesus/sivel2_gen/-/issues/741 Combatientes ahora con turbo

See merge request pasosdeJesus/sivel2_gen!523
  • Loading branch information
vtamara committed Nov 28, 2024
2 parents e68928d + 6323f10 commit 289fcee
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 67 deletions.
37 changes: 4 additions & 33 deletions app/controllers/sivel2_gen/combatientes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,10 @@
require 'date'

require 'sivel2_gen/concerns/controllers/combatientes_controller'

module Sivel2Gen
class CombatientesController < ApplicationController
load_and_authorize_resource class: Sivel2Gen::Combatiente

# Crea un nuevo registro para el caso que recibe por parametro
# params[:caso_id]. Pone valores simples en los campos requeridos
def nuevo
if params[:caso_id]
@combatiente = Combatiente.new
@combatiente.nombre = 'N'
@combatiente.sexo = Msip::Persona.convencion_sexo[:sexo_masculino].to_s
@combatiente.resagresion_id = 1
@combatiente.caso_id = params[:caso_id]
if @combatiente.save
respond_to do |format|
format.js { render json: {'combatiente' => @combatiente.id.to_s } }
format.json { render json: {'combatiente' => @combatiente.id.to_s },
status: :created }
format.html { render json: {'combatiente' => @combatiente.id.to_s } }
end
else
respond_to do |format|
format.html {
render inline: 'Error: ' + @combatiente.errors.messages.to_s
}
format.json { render json: @combatiente.errors.messages,
status: :unprocessable_entity }
end
end
else
respond_to do |format|
format.html { render inline: 'Falta identificacion del caso' }
end
end
end

include Sivel2Gen::Concerns::Controllers::CombatientesController
end
end
49 changes: 29 additions & 20 deletions app/views/sivel2_gen/casos/_combatiente_campos.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class='control-group nested-fields seccioncombatiente'>
<div class="controls">
<%= f.input :id, as: :hidden,
<%= turbo_frame_tag "combatiente_#{f.index}" do %>
<div class='control-group nested-fields seccioncombatiente'>
<div class="controls">
<%= f.input :id, as: :hidden,
input_html: {
"data-controller": "sivel2-gen--motor"
}
%>
%>
<div class="row">
<div class="col-md-6">
<%= f.input :nombre %>
Expand All @@ -31,7 +32,7 @@
include_blank: false,
label_method: :nombre,
value_method: :id %>
</div>
</div>
</div>

<div class="row">
Expand All @@ -52,7 +53,7 @@
include_blank: false,
label_method: :nombre,
value_method: :id %>
</div>
</div>
</div>

<div class="row">
Expand All @@ -73,7 +74,7 @@
include_blank: false,
label_method: :nombre,
value_method: :id %>
</div>
</div>
</div>

<div class="row">
Expand All @@ -94,7 +95,7 @@
include_blank: false,
label_method: :nombre,
value_method: :id %>
</div>
</div>
</div>

<div class="row">
Expand All @@ -112,22 +113,30 @@
collection: Msip::ModeloHelper.opciones_tabla_basica(
Sivel2Gen::Antecedente, f, :antecedente
),
include_blank: false,
label_method: :nombre,
value_method: :id,
input_html: {
class: "tom-select"
}%>
</div>
include_blank: false,
label_method: :nombre,
value_method: :id,
input_html: {
class: "tom-select"
}%>
</div>
</div>
<div style='margin-bottom: 1em'>
<%= link_to t('Crea Copia'), '#',
class: 'btn btn-sm',
onclick: 'Sivel2Gen__Motor.crearCopiaDeCombatiente(this, event)' %>
<%= link_to_remove_association "Eliminar Combatiente", f, {
class: 'btn btn-sm btn-danger',
'data-existing': 'true'
} %>
<%= f.submit "Eliminar",
formaction: sivel2_gen.eliminar_combatiente_path(
id: f.object.id,
index: f.index),
formmethod: :delete,
formnovalidate: true,
class: 'btn btn-sm btn-danger',
data: {
turbo_frame: "combatiente_#{f.index}",
disable_with: false
} %>
</div>
</div>
</div>
</div>
<% end %>
31 changes: 17 additions & 14 deletions app/views/sivel2_gen/casos/_combatientes.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<%= f.simple_fields_for :combatiente do |vc| %>
<%= render 'combatiente_campos', f: vc %>
<% end %>
<div class="links">
<%= link_to_add_association 'Añadir Combatiente', f,
:combatiente,
partial: 'combatiente_campos',
class: 'btn btn-sm btn-primary agrega-comb',
"data-ajax": File.join(
Rails.configuration.relative_url_root,
"combatientes/nuevo"
),
"data-ajaxdata": "caso_id"
%>
<div id="combatientes_marco">
<%= f.simple_fields_for :combatiente,
child_index: params[:index] do |com| %>
<%= render '/sivel2_gen/casos/combatiente_campos', :f => com %>
<% end %>
</div>

<%= f.submit "Agregar Combatiente",
formaction: sivel2_gen.crear_combatiente_path(index: @caso.combatiente.size),
formmethod: :post,
formnovalidate: true,
class: 'btn btn-sm btn-primary',
name: "agregar-combatiente",
id: "agregar-combatiente",
input_html: {class: "tom-select"},
data: {
disable_with: false } %>

19 changes: 19 additions & 0 deletions app/views/sivel2_gen/combatientes/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= simple_form_for @caso do |form| %>
<%= form.simple_fields_for :combatiente,
child_index: params[:index] do |com_form| %>
<%= turbo_stream.replace "agregar-combatiente" do %>
<%= form.submit "Agregar Combatiente",
formaction: sivel2_gen.crear_combatiente_path(com_form.index.to_i + 1),
formmethod: :post,
formnovalidate: true,
class: 'btn btn-sm btn-primary agregar-vic',
id: "agregar-combatiente",
data: {
disable_with: false } %>
<% end %>

<%= turbo_stream.append "combatientes_marco" do %>
<%= render "/sivel2_gen/casos/combatiente_campos", f: com_form %>
<% end %>
<% end %>
<% end %>
9 changes: 9 additions & 0 deletions app/views/sivel2_gen/combatientes/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= fields model: @caso do |form| %>
<%= form.simple_fields_for :combatiente,
child_index: params[:index] do |com_form| %>
<%= turbo_frame_tag "combatiente_#{com_form.index}" do %>
<%= com_form.hidden_field :id, value: params[:id] %>
<%= com_form.hidden_field :_destroy, value: true %>
<% end %>
<% end %>
<% end %>
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
end
end

resources :combatiente, only: [], param: :index do
member do
delete '(:id)', to: "combatientes#destroy", as: "eliminar"
post '/' => "combatientes#create", as: "crear"
end
end

resources :familiar, only: [], param: :index do
member do
delete '(:id)', to: "familiares#eliminar_familiar", as: "eliminar"
Expand Down
67 changes: 67 additions & 0 deletions lib/sivel2_gen/concerns/controllers/combatientes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'date'

module Sivel2Gen
module Concerns
module Controllers
module CombatientesController
extend ActiveSupport::Concern

included do
include ActionView::Helpers::AssetUrlHelper

load_and_authorize_resource class: Sivel2Gen::Caso
before_action :prepara_caso

# Crea un nuevo registro para el caso que recibe por parametro
# params[:caso_id]. Pone valores simples en los campos requeridos
def nuevo
if params[:caso_id]
@combatiente = Combatiente.new
@combatiente.nombre = 'N'
@combatiente.sexo = Msip::Persona.convencion_sexo[:sexo_masculino].to_s
@combatiente.resagresion_id = 1
@combatiente.caso_id = params[:caso_id]
if @combatiente.save
respond_to do |format|
format.js { render json: {'combatiente' => @combatiente.id.to_s } }
format.json { render json: {'combatiente' => @combatiente.id.to_s },
status: :created }
format.html { render json: {'combatiente' => @combatiente.id.to_s } }
end
else
respond_to do |format|
format.html {
render inline: 'Error: ' + @combatiente.errors.messages.to_s
}
format.json { render json: @combatiente.errors.messages,
status: :unprocessable_entity }
end
end
else
respond_to do |format|
format.html { render inline: 'Falta identificacion del caso' }
end
end
end

def destroy
end

def create
end

def update
end

private

def prepara_caso
@caso = Sivel2Gen::Caso.new(
combatiente: [Sivel2Gen::Combatiente.new])
end
end # included

end
end
end
end

0 comments on commit 289fcee

Please sign in to comment.