Class: ExpressTemplates::Components::Forms::Radio

Inherits:
FormComponent show all
Includes:
OptionSupport
Defined in:
lib/express_templates/components/forms/radio.rb

Instance Attribute Summary

Attributes inherited from Expander

#handlers, #locals, #stack, #template

Instance Method Summary collapse

Methods included from OptionSupport

#belongs_to_association, #related_collection

Methods inherited from FormComponent

#compile, #field_name, #field_name_attribute, #field_wrapper_class, #label_name, #label_text, #parent_form, #resource_class, #resource_name, #resource_var

Methods included from Capabilities::Adoptable

included

Methods included from Capabilities::Configurable

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

Methods inherited from Expander

#expand, #initialize, #initialize_expander, #method_missing, #process_children!, register_macros_for

Constructor Details

This class inherits a constructor from ExpressTemplates::Expander

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander

Instance Method Details

#collection_from_associationObject



62
63
64
# File 'lib/express_templates/components/forms/radio.rb', line 62

def collection_from_association
  related_collection or raise "No association collection for: #{resource_name}.#{field_name}"
end

#field_optionsObject



66
67
68
69
70
71
72
73
74
# File 'lib/express_templates/components/forms/radio.rb', line 66

def field_options
  # If field_otions is omitted the Expander will be
  # in last or 3rd position and we don't want that
  if @args.size > 3 && @args[2].is_a?(Hash)
    @args[2]
  else
    {}
  end
end

#generate_options_from_specified_valuesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/express_templates/components/forms/radio.rb', line 41

def generate_options_from_specified_values
  case
  when option_collection.kind_of?(Array)
    option_collection.each_with_index do |option, index|
      label(class: wrapper_class) {
        radio_button(resource_var, field_name.to_sym, option, class: 'radio')
        null_wrap { option }
      }
    end
  when option_collection.kind_of?(Hash)
    option_collection.each_pair do |key, value|
      label(class: wrapper_class) {
        radio_button(resource_var, field_name.to_sym, key, class: 'radio')
        null_wrap { value }
      }
    end
  else
    raise "Radio collection should be Array or Hash: #{option_collection.inspect}"
  end
end

#html_optionsObject

TODO: implement



77
78
79
# File 'lib/express_templates/components/forms/radio.rb', line 77

def html_options
  {}
end

#option_collectionObject



33
34
35
# File 'lib/express_templates/components/forms/radio.rb', line 33

def option_collection
  @args.second
end

#option_values_specified?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/express_templates/components/forms/radio.rb', line 29

def option_values_specified?
  [Array, Hash].include?(option_collection.class)
end

#use_options_from_collection_radio_buttons_helperObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/express_templates/components/forms/radio.rb', line 18

def use_options_from_collection_radio_buttons_helper
  # Note {{ }} will get stripped.  This prevents the collection finder string being passed as string.
  collection_radio_buttons(resource_var, field_name.to_sym, "{{#{collection_from_association}}}",
                           option_value_method, option_name_method,
                           field_options, html_options) do |b|
    b.label(class: "radio") {
      b.radio_button + b.text
    }
  end
end

#wrapper_classObject



37
38
39
# File 'lib/express_templates/components/forms/radio.rb', line 37

def wrapper_class
  @config[:wrapper_class] || 'wrapper-class'
end