Skip to content

Commit

Permalink
Merge branch 'f733' into 'main'
Browse files Browse the repository at this point in the history
https://gitlab.com/pasosdeJesus/sivel2_gen/-/issues/733 formularios arreglados opcioncs con turbo

See merge request pasosdeJesus/mr519_gen!29
  • Loading branch information
vtamara committed Nov 28, 2024
2 parents 6607019 + 6792c70 commit f0b8d5c
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 108 deletions.
3 changes: 2 additions & 1 deletion app/controllers/mr519_gen/opcionescs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

module Mr519Gen
class OpcionescsController < Msip::ModelosController
load_and_authorize_resource class: Mr519Gen::Encuestausuario
before_action :preparar_opcion_campo_formulario
load_and_authorize_resource class: Mr519Gen::Opcioncs
include Mr519Gen::Concerns::Controllers::OpcionescsController
end
end
43 changes: 22 additions & 21 deletions app/javascript/controllers/motor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,39 @@ export default class Mr519Gen__Motor {
document.addEventListener('change', event => {
if (event.target.matches("[id^=formulario_campo_attributes_]") &&
event.target.matches("[id$=tipo]") ) {
if (this.querySelectorAll('option:selected').length > 0 && (
this.querySelector('option:selected').innerText == 'Selección Múltiple' ||
this.querySelector('option:selected').innerText == 'Selección Simple')) {
this.parentElement.parentElement.parentElement.querySelector('.espopciones').style.display = 'block'
} else {
this.parentElement.parentElement.parentElement.querySelector('.espopciones').style.display = 'none'
}
if (this.querySelectorAll('option:selected').length > 0 && (
this.querySelector('option:selected').innerText == 'Selección Múltiple con Tabla Básica' ||
this.querySelector('option:selected').innerText == 'Selección Simple con Tabla Básica')) {
this.parentElement.parentElement.parentElement.querySelector('.tablabasica').style.display = "block"
} else {
this.parentElement.parentElement.parentElement.querySelector('.tablabasica').style.display = "none"
}
} else if (event.target.id == "formulario_nombre") {
idni = $(this).attr('id').replace('nombre', 'nombreinterno')
if (event.target.value){
const elementoSelect = event.target;
const indexSeleccionado = elementoSelect.selectedIndex;
const opcion = elementoSelect.options[indexSeleccionado].textContent;
if(opcion == 'Selección Múltiple' || opcion == 'Selección Simple') {
event.target.parentElement.parentElement.parentElement.querySelector('.espopciones').style.display = 'block'
} else {
event.target.parentElement.parentElement.parentElement.querySelector('.espopciones').style.display = 'none'
}
if(opcion == 'Selección Múltiple con Tabla Básica' || opcion == 'Selección Simple con Tabla Básica') {
event.target.parentElement.parentElement.parentElement.querySelector('.tablabasica').style.display = 'block'
} else {
event.target.parentElement.parentElement.parentElement.querySelector('.tablabasica').style.display = 'none'
}
}
} else if (event.target.id == "formulario_nombre") {
let idni = event.target.id.replace('nombre', 'nombreinterno')
if (document.querySelectorAll('#' + idni).length == 1 && (
document.querySelector('#' + idni).value == '' ||
document.querySelector('#' + idni).value == 'N')) {
document.querySelector('#' + idni).value = Mr519Gen__Motor.nombreANombreInterno(this.value)
document.querySelector('#' + idni).value = Mr519Gen__Motor.nombreANombreInterno(event.target.value)
}
} else if (event.target.matches("[id^=formulario_campo_attributes_]") &&
event.target.matches("[id$=_nombre]") ) {
// Cubre tanto nombre de campos como nombre de opciones
idni = this.getAttribute('id').replace('nombre', 'nombreinterno')
let idni = event.target.id.replace('nombre', 'nombreinterno')
if (document.querySelectorAll('#' + idni).length == 0) {
idni = this.getAttrribute('id').replace('nombre', 'valor')
idni = event.target.id.replace('nombre', 'valor')
}
if (document.querySelectorAll('#' + idni).length == 1 && (
document.querySelector('#' + idni).value == '' ||
document.querySelector('#' + idni).value == 'N')) {
document.querySelector('#' + idni).value = Mr519Gen__Motor.nombreANombreInterno(this.value)
document.querySelector('#' + idni).value = Mr519Gen__Motor.nombreANombreInterno(event.target.value)
}
}
})
Expand Down Expand Up @@ -92,7 +93,7 @@ export default class Mr519Gen__Motor {


static nombreANombreInterno(nombre) {
ni = nombre.replace(/[^A-Za-z0-9_]/g, '_')
let ni = nombre.replace(/[^A-Za-z0-9_]/g, '_')
ni = ni.toLowerCase()
ni = ni.substring(0, 60)
return ni
Expand Down
1 change: 1 addition & 0 deletions app/models/mr519_gen/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def initialize_mr519_gen(usuario = nil)
Mr519Gen::Encuestapersona,
Mr519Gen::Formulario,
Mr519Gen::Campo,
Mr519Gen::Opcioncs,
Mr519Gen::Planencuesta,
])
end
Expand Down
40 changes: 25 additions & 15 deletions app/views/mr519_gen/formularios/_fila_campos.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@
<% clasedisp = 'display: block' %>
<% end %>
<div class="espopciones" style="<%= clasedisp %>">
<% if f.object.opcioncs.count > 0 %>
<hr>
<%= f.simple_fields_for :opcioncs, f.object.opcioncs.order(:id) do |o| %>
<%= render 'mr519_gen/formularios/fila_opcionescs', f: o %>
<% end %>
<% end %>
<div class="links-opcionescs">
<%= link_to_add_association t('Agregar opción'), f, :opcioncs, {
#:"data-association-insertion-node" => "tbody#opciones",
#:"data-association-insertion-method" => "append",
partial: 'mr519_gen/formularios/fila_opcionescs',
class: 'btn btn-sm btn-primary',
"data-ajax" => mr519_gen.new_opcioncs_path,
"data-ajaxdata" => "formulario_campo_id"
} %>
<hr>
<div class="div-tabla">
<% index_campo = f.index %>
<div id=<%= "campo_#{index_campo}_opcioncs_marco" %> class="panel-body table responsive" >
<%= f.simple_fields_for :opcioncs,
child_index: params[:index] do |opf| %>
<%= render "mr519_gen/formularios/fila_opcionescs",
f: opf,
subf: f,
index_op: params[:index]
%>
<% end %>
</div>
<%= f.submit "Agregar opción",
formaction: mr519_gen.crear_opcioncs_path(
index: f.object.opcioncs.size,
index_campo: index_campo
),
formmethod: :post,
formnovalidate: true,
class: 'btn btn-sm btn-primary',
name: "agregar-opcion-#{index_campo}",
id: "agregar-opcion-#{index_campo}",
data: {
disable_with: false } %>
</div>
</div>

Expand Down
52 changes: 31 additions & 21 deletions app/views/mr519_gen/formularios/_fila_opcionescs.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
<div class="nested-fields opcioncs" style="display: flex">
<% #byebug %>
<% f.object = Mr519Gen::Opcioncs.new() if !f.object %>
<div>
<%= f.input :nombre,
label: false,
placeholder: 'Nombre'
%>
</div>
<div>
<%= f.input :valor,
label: false,
placeholder: 'Valor'
%>
</div>
<div>
<%= turbo_frame_tag "opcioncs_#{subf.index}_#{f.index}", class: 'div-tabla-fila nested-fields' do %>
<div class="nested-fields opcioncs">
<%= f.input :id, as: :hidden %>
<%= link_to_remove_association t("Eliminar"), f,
:class => 'btn btn-sm btn-danger', "data-existing" => true
%>
<div><%= f.error_notification %></div>
<div class="div-tabla-celda">
<%= f.input :nombre,
label: false,
placeholder: 'Nombre'
%>
</div>
<div class="div-tabla-celda">
<%= f.input :valor,
label: false,
placeholder: 'Valor'
%>
</div>
<div class="div-tabla-celda">
<%= f.submit "Eliminar",
formaction: mr519_gen.eliminar_opcioncs_path(
id: f.object.id,
index: f.index,
index_campo: subf.index
),
formmethod: :delete,
formnovalidate: true,
class: 'btn btn-sm btn-danger',
data: {
turbo_frame: "opcioncs_#{subf.index}_#{f.index}",
disable_with: false
} %>
<div><%= f.error_notification %></div>
</div>
</div>
</div>
<% end %>
26 changes: 26 additions & 0 deletions app/views/mr519_gen/opcionescs/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= simple_form_for @formulario do |form| %>
<%= form.simple_fields_for :campo,
child_index: params[:index_campo] do |subf| %>
<%= subf.simple_fields_for :opcioncs,
child_index: params[:index] do |opf| %>
<%= turbo_stream.replace "agregar-opcion-#{params[:index_campo]}" do %>
<%= opf.submit "Agregar opción",
formaction: mr519_gen.crear_opcioncs_path(
index: params[:index].to_i + 1,
index_campo: params[:index_campo].to_i
),
formmethod: :post,
formnovalidate: true,
class: 'btn btn-sm btn-primary',
name: "agregar-opcion-#{params[:index_campo]}",
id: "agregar-opcion-#{params[:index_campo]}",
data: {
disable_with: false
} %>
<% end %>
<%= turbo_stream.append "campo_#{params[:index_campo]}_opcioncs_marco" do %>
<%= render "mr519_gen/formularios/fila_opcionescs", f: opf, subf: subf %>
<% end %>
<% end %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/mr519_gen/opcionescs/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= simple_form_for @formulario do |form| %>
<%= form.simple_fields_for :campo, child_index: params[:index_campo] do |subf| %>
<%= subf.simple_fields_for :opcioncs do |opf| %>
<%= turbo_frame_tag "opcioncs_#{params[:index_campo]}_#{params[:index]}" do %>
<% opf.object = Mr519Gen::Opcioncs.find(params[:id]) %>
<%= opf.hidden_field :id, value: params[:id] %>
<%= opf.hidden_field :_destroy, value: 1 %>
<% end %>
<% end %>
<% end %>
<% end %>
9 changes: 7 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
end
end

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

resources :encuestaspersona, path_names: { new: "nueva", edit: "edita" }
resources :encuestasusuario, path_names: { new: "nueva", edit: "edita" }
resources :formularios, path_names: { new: "nuevo", edit: "edita" }
get "/formularios/copia/:formulario_id" =>
"formularios#copia",
as: :copia_formulario

resources :opcionescs, only: [:new, :destroy]

resources :planesencuesta,
path_names: { new: "nuevo", edit: "edita" }

Expand Down
18 changes: 8 additions & 10 deletions lib/mr519_gen/concerns/controllers/formularios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def index_reordenar(c)
end

# GET /formularios/new

def new
@registro = @formulario = Formulario.new
@formulario.nombre = "Formulario Nuevo"
Expand Down Expand Up @@ -104,7 +105,7 @@ def set_formulario

def mr519_gen_params
atributos_form +
[
[ :id,
campo_attributes: [
:ancho,
:ayudauso,
Expand All @@ -117,15 +118,12 @@ def mr519_gen_params
:tipo,
:tablabasica,
:_destroy,
{
opcioncs_attributes: [
:id,
:campo_id,
:nombre,
:valor,
:_destroy,
],
},
opcioncs_attributes: [
:id,
:nombre,
:valor,
:_destroy,
],
],
]
end
Expand Down
49 changes: 12 additions & 37 deletions lib/mr519_gen/concerns/controllers/opcionescs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,22 @@ module OpcionescsController
extend ActiveSupport::Concern

included do
# GET /opcionescs/new
def new
if params[:formulario_campo_id]
@opcioncs = Opcioncs.new
@opcioncs.campo_id = params[:formulario_campo_id]
@opcioncs.nombre = "N"
@opcioncs.valor = "N"
if @opcioncs.save(validate: false)
respond_to do |format|
format.js do
render(text: @opcioncs.id.to_s)
end
format.json do
render(json: @opcioncs.id.to_s, status: :created)
end
end
else
render(inline: "No implementado", status: :unprocessable_entity)
end
else
render(
inline: "Falta id de campo",
status: :unprocessable_entity,
)
end

def create
end

def destroy
return unless params[:id]
end

def update
end

private

@opcioncs = Opcioncs.find(params[:id])
@opcioncs.destroy
respond_to do |format|
format.html do
render(
inline: "No implementado",
status: :unprocessable_entity,
)
end
format.json { head(:no_content) }
end
def preparar_opcion_campo_formulario
@campo = Mr519Gen::Campo.new(opcioncs: [Mr519Gen::Opcioncs.new])
@formulario = Mr519Gen::Formulario.new(campo: [@campo])
@campo.opcioncs[0].campo = Mr519Gen::Campo.new()
end
end # included
end
Expand Down
2 changes: 2 additions & 0 deletions lib/mr519_gen/concerns/models/campo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module Campo
dependent: :delete_all,
class_name: "Mr519Gen::Opcioncs",
foreign_key: "campo_id",
inverse_of: :campo,
validate: true

accepts_nested_attributes_for :opcioncs,
allow_destroy: true,
reject_if: :all_blank
Expand Down
3 changes: 2 additions & 1 deletion lib/mr519_gen/concerns/models/opcioncs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Opcioncs

belongs_to :campo,
class_name: "Mr519Gen::Campo",
optional: false,
inverse_of: :opcioncs,
optional: true,
validate: true

validates :valor,
Expand Down

0 comments on commit f0b8d5c

Please sign in to comment.