Class: Redcarpet::Form::Extension::Block::Checkboxes

Inherits:
Object
  • Object
show all
Defined in:
lib/redcarpet/form/extension/block/checkboxes.rb

Overview

rubocop:disable Layout/LineLength A fieldset with checkboxes

Ex: []value=“bike” I have a bike [x]value=“car” I have a car [x]value=“boat” I have a boat <fieldset>

<label><input type="checkbox" name="123456[]" value="bike" /><span>I have a bike</span></label>
<label><input type="checkbox" name="123456[]" value="car" checked /><span>I have a car</span></label>
<label><input type="checkbox" name="123456[]" value="boat" checked /><span>I have a boat</span></label>

</fieldset> rubocop:enable Layout/LineLength

Class Method Summary collapse

Class Method Details

.default_attributesObject



22
23
24
# File 'lib/redcarpet/form/extension/block/checkboxes.rb', line 22

def self.default_attributes
  { 'type' => 'checkbox', 'name' => '', 'value' => '' }
end

.html(matches) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redcarpet/form/extension/block/checkboxes.rb', line 28

def self.html(matches)
  html = '<fieldset>'
  name = "#{Util.random_string}[]"

  matches.each_with_index do |item, index|
    checked = item[0]
    label = item[2]

    attrs = default_attributes.merge!(Util.parse_attributes(item[1]))
    attrs['name'] = name if attrs['name'].empty?
    attrs['value'] = "opt_#{index + 1}" if attrs['value'].empty?

    html += '<label>' unless label.empty?
    html += "<input #{Util.attributes_to_s(attrs)}"
    html += ' checked' if checked == 'x'
    html += ' />'
    html += "<span>#{label}</span></label>" unless label.empty?
  end

  html += '</fieldset>'
end

.patternObject



18
19
20
# File 'lib/redcarpet/form/extension/block/checkboxes.rb', line 18

def self.pattern
  /\[(x|\s)?\]([{][^}]*[}])?([^{}\]\[]*)/
end