From bb56e8542d9724d80e04d00b7559154478081022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20T=C3=A1mara=20Pati=C3=B1o?= Date: Tue, 19 Sep 2023 11:08:34 -0500 Subject: [PATCH] Permitir editar aportes desde formulario persona. Closes #228 --- app/controllers/aportes_controller.rb | 24 +++++++++ app/controllers/msip/personas_controller.rb | 25 ++++++++-- app/models/ability.rb | 1 + app/models/msip/persona.rb | 4 ++ app/views/aportes/create.turbo_stream.erb | 8 +++ app/views/aportes/destroy.html.erb | 10 ++++ .../msip/personas/_aporte_campos.html.erb | 36 ++++++++++++++ .../msip/personas/_campo_aportes.html.erb | 49 +++++++++++++++++++ config/application.rb | 3 ++ config/routes.rb | 7 +++ 10 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 app/controllers/aportes_controller.rb create mode 100644 app/views/aportes/create.turbo_stream.erb create mode 100644 app/views/aportes/destroy.html.erb create mode 100644 app/views/msip/personas/_aporte_campos.html.erb create mode 100644 app/views/msip/personas/_campo_aportes.html.erb diff --git a/app/controllers/aportes_controller.rb b/app/controllers/aportes_controller.rb new file mode 100644 index 0000000..20f89d0 --- /dev/null +++ b/app/controllers/aportes_controller.rb @@ -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 diff --git a/app/controllers/msip/personas_controller.rb b/app/controllers/msip/personas_controller.rb index 21d19cc..69b253d 100644 --- a/app/controllers/msip/personas_controller.rb +++ b/app/controllers/msip/personas_controller.rb @@ -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, @@ -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 diff --git a/app/models/ability.rb b/app/models/ability.rb index ca5509d..97226b5 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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, diff --git a/app/models/msip/persona.rb b/app/models/msip/persona.rb index 2cc7362..75678c9 100644 --- a/app/models/msip/persona.rb +++ b/app/models/msip/persona.rb @@ -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, diff --git a/app/views/aportes/create.turbo_stream.erb b/app/views/aportes/create.turbo_stream.erb new file mode 100644 index 0000000..0b88cbb --- /dev/null +++ b/app/views/aportes/create.turbo_stream.erb @@ -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 %> diff --git a/app/views/aportes/destroy.html.erb b/app/views/aportes/destroy.html.erb new file mode 100644 index 0000000..a59e2fd --- /dev/null +++ b/app/views/aportes/destroy.html.erb @@ -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 %> + diff --git a/app/views/msip/personas/_aporte_campos.html.erb b/app/views/msip/personas/_aporte_campos.html.erb new file mode 100644 index 0000000..14daf4c --- /dev/null +++ b/app/views/msip/personas/_aporte_campos.html.erb @@ -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 %> +
+ <%= f.input :anio, + collection: @anios, + label: false + %> +
+
+ <%= f.input :mes, + collection: @meses, + label: false + %> +
+
+ <%= f.input :valor, + label: false + %> +
+
+ <%= 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 + } %> +
+<% end %> diff --git a/app/views/msip/personas/_campo_aportes.html.erb b/app/views/msip/personas/_campo_aportes.html.erb new file mode 100644 index 0000000..77857d2 --- /dev/null +++ b/app/views/msip/personas/_campo_aportes.html.erb @@ -0,0 +1,49 @@ +
+ <%= 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 %> +
+
+
+
+
Año
+
Mes
+
Valor
+
Acciones
+
+ <%= 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 %> +
+
+
+ <%= 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 + } %> +
+
+ <% end %> + <% end %> +
+ + diff --git a/config/application.rb b/config/application.rb index b909bd8..87886a8 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 33760ba..dfaa5e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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|