Class: Bureaucrat::Widgets::Select
- Defined in:
- lib/bureaucrat/widgets.rb
Direct Known Subclasses
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#choices ⇒ Object
Returns the value of attribute choices.
Attributes inherited from Widget
Instance Method Summary collapse
-
#initialize(attrs = nil, choices = []) ⇒ Select
constructor
A new instance of Select.
- #render(name, value, attrs = nil, choices = []) ⇒ Object
- #render_option(option_attributes, option_label, selected_choices) ⇒ Object
- #render_options(choices, selected_choices) ⇒ Object
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
#initialize(attrs = nil, choices = []) ⇒ Select
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
#choices ⇒ Object
Returns the value of attribute choices.
307 308 309 |
# File 'lib/bureaucrat/widgets.rb', line 307 def choices @choices end |
Instance Method Details
#render(name, value, attrs = nil, choices = []) ⇒ Object
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)}>"] = (choices, [value]) output << if && !.empty? output << '</select>' mark_safe(output.join("\n")) end |
#render_option(option_attributes, option_label, selected_choices) ⇒ Object
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 |
#render_options(choices, selected_choices) ⇒ Object
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 (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 |