Module: Formtastic::Inputs::Base::Choices

Included in:
CheckBoxesInput, RadioInput
Defined in:
lib/formtastic/inputs/base/choices.rb

Instance Method Summary collapse

Instance Method Details

#choice_html(choice) ⇒ Object



39
40
41
# File 'lib/formtastic/inputs/base/choices.rb', line 39

def choice_html(choice)        
  raise "choice_html() needs to be implemented when including Formtastic::Inputs::Base::Choices"
end

#choice_html_safe_value(choice) ⇒ Object



51
52
53
# File 'lib/formtastic/inputs/base/choices.rb', line 51

def choice_html_safe_value(choice)
  choice_value(choice).to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase
end

#choice_input_dom_id(choice) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/formtastic/inputs/base/choices.rb', line 55

def choice_input_dom_id(choice)
  [
    builder.custom_namespace,
    sanitized_object_name,
    association_primary_key || method,
    choice_html_safe_value(choice)
  ].compact.reject { |i| i.blank? }.join("_")
end

#choice_label(choice) ⇒ Object



43
44
45
# File 'lib/formtastic/inputs/base/choices.rb', line 43

def choice_label(choice)
  choice.is_a?(Array) ? choice.first : choice
end

#choice_value(choice) ⇒ Object



47
48
49
# File 'lib/formtastic/inputs/base/choices.rb', line 47

def choice_value(choice)
  choice.is_a?(Array) ? choice.last : choice
end

#choice_wrapping(html_options, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/formtastic/inputs/base/choices.rb', line 28

def choice_wrapping(html_options, &block)
  template.(:li, 
    template.capture(&block),
    html_options
  )
end

#choice_wrapping_html_options(choice) ⇒ Object



35
36
37
# File 'lib/formtastic/inputs/base/choices.rb', line 35

def choice_wrapping_html_options(choice)
  { :class => value_as_class? ? "#{sanitized_method_name.singularize}_#{choice_html_safe_value(choice)}" : '' }
end

#choices_group_wrapping(&block) ⇒ Object



17
18
19
20
21
22
# File 'lib/formtastic/inputs/base/choices.rb', line 17

def choices_group_wrapping(&block)
  template.(:ol, 
    template.capture(&block),
    choices_group_wrapping_html_options
  )
end

#choices_group_wrapping_html_optionsObject



24
25
26
# File 'lib/formtastic/inputs/base/choices.rb', line 24

def choices_group_wrapping_html_options
  {}
end

#choices_wrapping(&block) ⇒ Object



6
7
8
9
10
11
# File 'lib/formtastic/inputs/base/choices.rb', line 6

def choices_wrapping(&block)
  template.(:fieldset, 
    template.capture(&block),
    choices_wrapping_html_options
  )
end

#choices_wrapping_html_optionsObject



13
14
15
# File 'lib/formtastic/inputs/base/choices.rb', line 13

def choices_wrapping_html_options
  {}
end

#label_html_optionsObject

Override to remove the for attribute since this isn’t associated with any element, as it’s nested inside the legend.



81
82
83
# File 'lib/formtastic/inputs/base/choices.rb', line 81

def label_html_options
  super.merge(:for => nil)
end

#legend_htmlObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/formtastic/inputs/base/choices.rb', line 68

def legend_html
  if render_label?
    template.(:legend,
      template.(:label, label_text),
      label_html_options.merge(:class => "label")
    )
  else
    "".html_safe
  end
end

#value_as_class?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/formtastic/inputs/base/choices.rb', line 64

def value_as_class?
  options[:value_as_class]
end