Sha256: 22465baa537304b9fb0ba37c8fa19a5c4b8e6755171e4dc423b43c6bf95d765d

Contents?: true

Size: 1.75 KB

Versions: 52

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

class PgAssociableInput < SimpleForm::Inputs::StringInput
  include ActionView::Helpers::FormTagHelper
  include Rails.application.routes.url_helpers
  include PgEngine::RouteHelper

  def hidden_input(_wrapper_options = {})
    @builder.hidden_field(attribute_name)
  end

  def controller
    # Para que no rompa polymorphic_url en NamespaceDeductor
  end

  def input(wrapper_options = nil)
    atributo = attribute_name.to_s.gsub('_id', '')
    url_modal = namespaced_path(clase_asociacion(atributo), prefix: :abrir_modal)
    url_search = namespaced_path(clase_asociacion(atributo), prefix: :buscar)

    input_html_options[:data] = { url_search:, url_modal: }
    input_html_options[:type] = 'text'

    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    build_input(merged_input_options)
  end

  def clase_asociacion(atributo)
    asociacion = object.class.reflect_on_all_associations.find { |a| a.name == atributo.to_sym }
    nombre_clase = asociacion.options[:class_name]
    nombre_clase = asociacion.name.to_s.camelize if nombre_clase.nil?
    Object.const_get(nombre_clase)
  end

  def build_input(input_options)
    content_tag('div', class: 'position-relative') do
      hidden_input + text_field_tag(nil, object.send(reflection.name).to_s,
                                    input_options) + limpiar + pencil
    end
  end

  def limpiar(_wrapper_options = nil)
    content_tag('a', href: 'javascript:void(0)', class: 'limpiar', title: 'Limpiar', tabindex: '0',
                     data: { action: 'asociable#selectItem' }) do
      '<i class="bi bi-x-lg"></i>'.html_safe
    end
  end

  def pencil(_wrapper_options = nil)
    '<i tabindex="-1" class="bi bi-pencil pencil"></i>'.html_safe
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
pg_rails-7.0.8.pre.alpha.56 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.55 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.54 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.53 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.52 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.51 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.50 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.49 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.48 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.47 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.46 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.45 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.44 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.43 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.42 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.41 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.40 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.39 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.38 pg_associable/app/inputs/pg_associable_input.rb
pg_rails-7.0.8.pre.alpha.37 pg_associable/app/inputs/pg_associable_input.rb