Class: Bureaucrat::Widgets::MultipleHiddenInput

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

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 HiddenInput

#hidden?, #input_type

Methods inherited from Input

#input_type

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 = []) ⇒ MultipleHiddenInput

Returns a new instance of MultipleHiddenInput.



109
110
111
112
113
# File 'lib/bureaucrat/widgets.rb', line 109

def initialize(attrs=nil, choices=[])
  super(attrs)
  # choices can be any enumerable
  @choices = choices
end

Instance Attribute Details

#choicesObject

Used by choice fields



107
108
109
# File 'lib/bureaucrat/widgets.rb', line 107

def choices
  @choices
end

Instance Method Details

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bureaucrat/widgets.rb', line 115

def render(name, value, attrs=nil, choices=[])
  value ||= []
  final_attrs = build_attrs(attrs, type: input_type.to_s,
                            name: "#{name}[]")

  id = final_attrs[:id]
  inputs = []

  value.each.with_index do |v, i|
    input_attrs = final_attrs.merge(value: v.to_s)

    if id
      input_attrs[:id] = "#{id}_#{i}"
    end

    inputs << "<input#{flatatt(input_attrs)} />"
  end

  mark_safe(inputs.join("\n"))
end