Skip to content

Commit

Permalink
Permitir editar aportes desde formulario persona. Closes #228
Browse files Browse the repository at this point in the history
  • Loading branch information
vtamara committed Sep 19, 2023
1 parent 432fa21 commit bb56e85
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 4 deletions.
24 changes: 24 additions & 0 deletions app/controllers/aportes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

class AportesController < ApplicationController

load_and_authorize_resource class: ::Aporte

before_action :prepara_persona

# Para llamar vista con formato turbo stream
def destroy
end

# Para llamar vista con formato turbo stream
def create
end

private

def prepara_persona
@persona = Msip::Persona.new(
aporte: [::Aporte.new]
)
end

end # included
25 changes: 21 additions & 4 deletions app/controllers/msip/personas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def atributos_show
:mesnac,
:dianac,
:sexo,
:pais,
:departamento,
:municipio,
:clase,
:pais_id,
:departamento_id,
:municipio_id,
:clase_id,
:nacionalde,
:familiares,
:ultimo_departamento_trabajo_id,
Expand Down Expand Up @@ -85,5 +85,22 @@ def destroy
destroy_gen
end

def lista_params
atributos_form - [
:aportes
] + [
:aporte_attributes => [
:anio,
:id,
:mes,
:valor,
:_destroy
]
]
end

def caso_params
params.require(:persona).permit(lista_params)
end
end
end
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def initialize(usuario = nil)
can([:read, :update], Mr519Gen::Encuestausuario)
can([:read], Msip::Homonimo)
can :manage, Sivel2Gen::Acto
can :manage, ::Aporte

if usuario && usuario.grupo.pluck(:id).include?(
GRUPO_DESAPARICION_CASOS,
Expand Down
4 changes: 4 additions & 0 deletions app/models/msip/persona.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class Persona < ActiveRecord::Base
has_many :aporte,
foreign_key: "persona_id",
validate: true,
dependent: :destroy,
class_name: "::Aporte"
accepts_nested_attributes_for :aporte,
allow_destroy: true,
reject_if: :all_blank

validates :tdocumento_id, presence: true, allow_blank: false
validates :numerodocumento, presence: true, allow_blank: false,
Expand Down
8 changes: 8 additions & 0 deletions app/views/aportes/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= simple_fields_for @persona do |f| %>
<%= f.simple_fields_for :aporte,
child_index: params[:index] do |subf| %>
<%= turbo_stream.append "aportes_marco" do %>
<%= render "msip/personas/aporte_campos", f: subf %>
<% end %>
<% end %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/aportes/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= fields model: @persona do |f| %>
<%= f.simple_fields_for :aporte,
child_index: params[:index] do |subf| %>
<%= turbo_frame_tag "aporte_#{subf.index}" do %>
<%= subf.hidden_field :id, value: params[:id] %>
<%= subf.hidden_field :_destroy, value: true %>
<% end %>
<% end %>
<% end %>

36 changes: 36 additions & 0 deletions app/views/msip/personas/_aporte_campos.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= turbo_frame_tag "aporte_#{f.index}",
class: "div-tabla-fila nested-fields" do %>
<% if f.object.nil? %>
<% f.object = ::Aporte.new %>
<% end %>
<div class="div-tabla-celda">
<%= f.input :anio,
collection: @anios,
label: false
%>
</div>
<div class="div-tabla-celda">
<%= f.input :mes,
collection: @meses,
label: false
%>
</div>
<div class="div-tabla-celda">
<%= f.input :valor,
label: false
%>
</div>
<div class="div-tabla-celda">
<%= f.input :id, as: :hidden %>
<%= f.submit "Eliminar",
formaction: main_app.eliminar_aporte_path(
id: f.object.id, index: f.index || 1),
formmethod: :delete,
formnovalidate: true,
class: 'btn btn-sm btn-danger',
data: {
turbo_frame: "aporte_#{f.index}",
disable_with: false
} %>
</div>
<% end %>
49 changes: 49 additions & 0 deletions app/views/msip/personas/_campo_aportes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div class="accordion" id="acordeonapo-<%= f.object.id %>">
<%= item_acordeon_bs(
'acordeonapo', "colapsaapo-#{f.object.id}",
'Aportes', false, {
'estilo_accordion-header': 'text-align: center',
'clase_accordion-body': 'table-responsive',
'estilo_accordion-body': 'overflow-x:scroll'
}) do %>
<% if f.object.id.nil? %>
Una vez guarde este registro y vuelva a editar podrá agregar aportes
<% else %>
<div id="aportes" class="panel-body table-responsive" >
<div class="div-tabla">
<div id="aportes_marco" class="div-tabla-cuerpo">
<div class="div-tabla-fila">
<div class="div-tabla-celda">Año</div>
<div class="div-tabla-celda">Mes</div>
<div class="div-tabla-celda">Valor</div>
<div class="div-tabla-celda">Acciones</div>
</div>
<%= f.simple_fields_for :aporte,
f.object.aporte.order([:anio,:mes]),
child_index: params[:index] do |apo| %>
<%= render partial: "aporte_campos",
locals: {f: apo}
%>
<% end %>
</div>
</div>
<div class="enlace-agregar-aporte">
<%= f.submit "Agregar Aporte",
formaction: main_app.crear_aporte_path(
index: @persona.aporte.size
),
formmethod: :post,
formnovalidate: true,
class: 'btn btn-sm btn-primary',
name: "agregar-aporte",
id: "agregar-aporte",
data: {
disable_with: false
} %>
</div>
</div>
<% end %>
<% end %>
</div>


3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Application < Rails::Application
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = "America/Bogota"

#Evita deshabilitar butones en formularios anidados dinámicos
config.action_view.automatically_disable_submit_tag = false

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :es
Expand Down
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
get "/orgsociales/jerarquia" => "msip/orgsociales#jerarquia",
as: :jerarquia_orgsociales

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

namespace :admin do
ab = Ability.new
ab.tablasbasicas.each do |t|
Expand Down

0 comments on commit bb56e85

Please sign in to comment.