Class: Bureaucrat::Widgets::CheckboxInput

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

Overview

DateInput DateTimeInput TimeInput

Constant Summary

Constants included from Utils

Utils::ESCAPES

Instance Attribute Summary

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?

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, check_test = nil) ⇒ CheckboxInput

Returns a new instance of CheckboxInput.



261
262
263
264
# File 'lib/bureaucrat/widgets.rb', line 261

def initialize(attrs=nil, check_test=nil)
  super(attrs)
  @check_test = check_test || lambda {|v| make_bool(v)}
end

Instance Method Details

#has_changed(initial, data) ⇒ Object



301
302
303
# File 'lib/bureaucrat/widgets.rb', line 301

def has_changed(initial, data)
  make_bool(initial) != make_bool(data)
end

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/bureaucrat/widgets.rb', line 266

def render(name, value, attrs=nil)
  final_attrs = build_attrs(attrs, type: 'checkbox', name: name.to_s)

  # FIXME: this is horrible, shouldn't just rescue everything
  result = @check_test.call(value) rescue false

  if result
    final_attrs[:checked] = 'checked'
  end

  unless ['', true, false, nil].include?(value)
    final_attrs[:value] = value.to_s
  end

  mark_safe("<input#{flatatt(final_attrs)} />")
end

#value_from_formdata(data, name) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/bureaucrat/widgets.rb', line 283

def value_from_formdata(data, name)
  if data.include?(name)
    value = data[name]

    if value.is_a?(String)
      case value.downcase
      when 'true' then true
      when 'false' then false
      else value
      end
    else
      value
    end
  else
    false
  end
end