Class: Bureaucrat::Widgets::Select

Inherits:
Widget
  • Object
show all
Defined in:
lib/bureaucrat/widgets.rb

Direct Known Subclasses

NullBooleanSelect, RadioSelect, SelectMultiple

Constant Summary

Constant Summary

Constants included from Utils

Utils::ESCAPES

Instance Attribute Summary (collapse)

Attributes inherited from Widget

#attrs, #is_required

Instance Method Summary (collapse)

Methods inherited from Widget

#build_attrs, #has_changed?, #hidden?, id_for_label, #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

- (Select) initialize(attrs = nil, choices = [])

Returns a new instance of Select



309
310
311
312
# File 'lib/bureaucrat/widgets.rb', line 309

def initialize(attrs=nil, choices=[])
  super(attrs)
  @choices = choices.collect
end

Instance Attribute Details

- (Object) choices

Returns the value of attribute choices



307
308
309
# File 'lib/bureaucrat/widgets.rb', line 307

def choices
  @choices
end

Instance Method Details

- (Object) render(name, value, attrs = nil, choices = [])



314
315
316
317
318
319
320
321
322
# File 'lib/bureaucrat/widgets.rb', line 314

def render(name, value, attrs=nil, choices=[])
  value = '' if value.nil?
  final_attrs = build_attrs(attrs, name: name)
  output = ["<select#{flatatt(final_attrs)}>"]
  options = render_options(choices, [value])
  output << options if options && !options.empty?
  output << '</select>'
  mark_safe(output.join("\n"))
end

- (Object) render_option(option_attributes, option_label, selected_choices)



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/bureaucrat/widgets.rb', line 344

def render_option(option_attributes, option_label, selected_choices)
  unless option_attributes.is_a?(Hash)
    option_attributes = { value: option_attributes.to_s }
  end

  if selected_choices.include?(option_attributes[:value])
    option_attributes[:selected] = "selected"
  end

  attributes = []

  option_attributes.each_pair do |attr_name, attr_value|
    attributes << %Q[#{attr_name.to_s}="#{escape(attr_value.to_s)}"]
  end

  "<option #{attributes.join(' ')}>#{conditional_escape(option_label.to_s)}</option>"
end

- (Object) render_options(choices, selected_choices)



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/bureaucrat/widgets.rb', line 324

def render_options(choices, selected_choices)
  selected_choices = selected_choices.map(&:to_s).uniq
  output = []
  (@choices.to_a + choices.to_a).each do |option_value, option_label|
      option_label ||= option_value
      if option_label.is_a?(Array)
        output << '<optgroup label="%s">' % escape(option_value.to_s)
        option_label.each do |option|
          val, label = option
          output << render_option(val, label, selected_choices)
        end
        output << '</optgroup>'
      else
        output << render_option(option_value, option_label,
                                selected_choices)
      end
    end
  output.join("\n")
end