Class: XRB::Formatters::HTML::RadioSelect
- Inherits:
-
Object
- Object
- XRB::Formatters::HTML::RadioSelect
- Defined in:
- lib/xrb/formatters/html/radio_select.rb
Overview
Table based select boxes using per-row checkboxes.
Class Method Summary collapse
Instance Method Summary collapse
- #call(&block) ⇒ Object
-
#initialize(formatter, builder, **options) ⇒ RadioSelect
constructor
A new instance of RadioSelect.
- #item(**options, &block) ⇒ Object
- #name_for(**options) ⇒ Object
- #optional? ⇒ Boolean
- #optional_title_for(**options) ⇒ Object
- #radio_attributes_for(**options) ⇒ Object
- #raw_value ⇒ Object
- #raw_value_for(**options) ⇒ Object
- #title_for(**options) ⇒ Object
- #value_for(**options) ⇒ Object
Constructor Details
#initialize(formatter, builder, **options) ⇒ RadioSelect
19 20 21 22 23 24 25 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 19 def initialize(formatter, builder, **) @formatter = formatter @builder = builder @options = @field = [:field] end |
Class Method Details
.call(formatter, builder, **options, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 13 def self.call(formatter, builder, **, &block) instance = self.new(formatter, builder, **) instance.call(&block) end |
Instance Method Details
#call(&block) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 88 def call(&block) Builder.fragment(@builder) do |builder| builder.tag :table do builder.tag :tbody do if self.optional? builder << item(title: optional_title_for(**@options), value: nil) end builder.capture(self, &block) end end end end |
#item(**options, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 58 def item(**, &block) Builder.fragment(block&.binding || @builder) do |builder| builder.tag :tr do builder.inline(:td, :class => :handle) do builder.tag :input, radio_attributes_for(**) end builder.inline(:td, :class => :item) do if block_given? builder.capture(self, &block) else builder.text title_for(**) end end end end end |
#name_for(**options) ⇒ Object
27 28 29 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 27 def name_for(**) @formatter.name_for(**) end |
#optional? ⇒ Boolean
84 85 86 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 84 def optional? @options[:optional] end |
#optional_title_for(**options) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 76 def optional_title_for(**) if [:optional] == true [:blank] || "" else [:optional] end end |
#radio_attributes_for(**options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 47 def radio_attributes_for(**) return { :type => :radio, :name => @field, # We set a default value to empty string, otherwise it becomes "on". :value => value_for(**) || "", :checked => .fetch(:selected) {raw_value == raw_value_for(**)}, :data => [:data], } end |
#raw_value ⇒ Object
35 36 37 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 35 def raw_value @raw_value ||= raw_value_for(**@options) end |
#raw_value_for(**options) ⇒ Object
31 32 33 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 31 def raw_value_for(**) @formatter.raw_value_for(**) end |
#title_for(**options) ⇒ Object
43 44 45 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 43 def title_for(**) @formatter.title_for(**) end |
#value_for(**options) ⇒ Object
39 40 41 |
# File 'lib/xrb/formatters/html/radio_select.rb', line 39 def value_for(**) @formatter.value_for(**) end |