Class: Bureaucrat::Widgets::CheckboxSelectMultiple

Inherits:
SelectMultiple show all
Defined in:
lib/bureaucrat/widgets.rb

Constant Summary

Constants included from Utils

Utils::ESCAPES

Instance Attribute Summary

Attributes inherited from Select

#choices

Attributes inherited from Widget

#attrs, #is_required

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SelectMultiple

#has_changed?

Methods inherited from Select

#initialize, #render_option, #render_options

Methods inherited from Widget

#build_attrs, #has_changed?, #hidden?, #initialize, #initialize_copy, #needs_multipart?, #value_from_formdata

Methods included from Utils

#blank_value?, #conditional_escape, #escape, #flatatt, #format_string, #make_bool, #make_float, #mark_safe, #pretty_name

Constructor Details

This class inherits a constructor from Bureaucrat::Widgets::Select

Class Method Details

.id_for_label(id_) ⇒ Object



514
515
516
# File 'lib/bureaucrat/widgets.rb', line 514

def self.id_for_label(id_)
  id_.empty? ? id_ : id_ + '_0'
end

Instance Method Details

#render(name, values, attrs = nil, choices = []) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/bureaucrat/widgets.rb', line 518

def render(name, values, attrs=nil, choices=[])
  values ||= []
  multi_name = "#{name}[]"
  has_id = attrs && attrs.include?(:id)
  final_attrs = build_attrs(attrs, name: multi_name)
  output = ['<ul>']
  str_values = {}
  values.each {|val| str_values[(val.to_s)] = true}

  (@choices.to_a + choices.to_a).each_with_index do |opt_pair, i|
      opt_val, opt_label = opt_pair
      if has_id
        final_attrs[:id] = "#{attrs[:id]}_#{i}"
        label_for = " for=\"#{final_attrs[:id]}\""
      else
        label_for = ''
      end

      check_test = lambda{|value| str_values[value]}
      cb = CheckboxInput.new(final_attrs, check_test)
      opt_val = opt_val.to_s
      rendered_cb = cb.render(multi_name, opt_val)
      opt_label = conditional_escape(opt_label.to_s)
      output << "<li><label#{label_for}>#{rendered_cb} #{opt_label}</label></li>"
    end
  output << '</ul>'
  mark_safe(output.join("\n"))
end