Sha256: d420e7ed67d9074524375c83307975ff173db7799748566969e5ef0cc98126f6

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

class SimpleForm::Inputs::AjaxSelect2Input < SimpleForm::Inputs::StringInput
  extend ActiveSupport::Autoload

  def input_html_options
    url_params = {}
    url_params[:class_name] = options[:search_class] if options[:search_class].present?
    url_params[:search_method] = options[:search_method] if options[:search_method].present?
    url_params[:default_class_name] = options[:default_class_name] if options[:default_class_name].present?
    url_params[:default_text_column] = options[:default_text_column] if options[:default_text_column].present?
    url_params[:default_id_column] = options[:default_id_column] if options[:default_id_column].present?
    url_params[:select2_options] = options[:select2_options] if options[:select2_options].present?
    href = template.select2_autocompletes_path(url_params)
    input_options = super
    input_options[:data] = input_options[:data].to_h.merge({
        s2_href: href,
        s2_options: url_params[:select2_options].to_h.merge(multiple: multiple?)
    })
    css_classes = [input_options[:class], 'auto-ajax-select2', 'input-hol']
    if multiple?
      css_classes << 'multiple'
    end
    input_options[:class] = css_classes.compact
    input_options[:value] = value
    input_options
  end

  def multiple?
    multiple_by_options? || multiple_by_association?
  end

  def value
    val = object.send(attribute_name)
    val = val.join(',') if val.is_a?(Array)
    val
  end

  def multiple_by_association?
    reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
  end

  def multiple_by_options?
    options[:multiple] || (options[:input_html] && options[:input_html][:multiple])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_form_auto_select2-0.0.5 lib/simple_form/inputs/ajax_select2_input.rb