Class: XRB::Formatters::HTML::OptionSelect
- Inherits:
-
Object
- Object
- XRB::Formatters::HTML::OptionSelect
- Defined in:
- lib/xrb/formatters/html/option_select.rb
Overview
Standard drop-down select box:
Class Method Summary collapse
Instance Method Summary collapse
- #call(&block) ⇒ Object
- #group(**options, &block) ⇒ Object
- #group_attributes_for(**options) ⇒ Object
-
#initialize(formatter, builder, **options) ⇒ OptionSelect
constructor
A new instance of OptionSelect.
- #item(**options, &block) ⇒ Object
- #name_for(**options) ⇒ Object
- #option_attributes_for(**options) ⇒ Object
- #optional? ⇒ Boolean
- #optional_title_for(**options) ⇒ Object
- #raw_value ⇒ Object
- #raw_value_for(**options) ⇒ Object
- #select_attributes_for(**options) ⇒ Object
- #title_for(**options) ⇒ Object
- #value_for(**options) ⇒ Object
Constructor Details
#initialize(formatter, builder, **options) ⇒ OptionSelect
Returns a new instance of OptionSelect.
19 20 21 22 23 |
# File 'lib/xrb/formatters/html/option_select.rb', line 19 def initialize(formatter, builder, **) @formatter = formatter @builder = builder @options = end |
Class Method Details
.call(formatter, builder, **options, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/xrb/formatters/html/option_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
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/xrb/formatters/html/option_select.rb', line 117 def call(&block) Builder.fragment(@builder) do |builder| builder.tag :select, select_attributes_for(**@options) do if self.optional? builder << item(title: optional_title_for(**@options), value: nil) end builder.capture(self, &block) end end end |
#group(**options, &block) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/xrb/formatters/html/option_select.rb', line 92 def group(**, &block) @builder.tag :optgroup, group_attributes_for(**) do if [:optional] item(title: optional_title_for(**), value: nil) end @builder.capture(&block) end end |
#group_attributes_for(**options) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/xrb/formatters/html/option_select.rb', line 83 def group_attributes_for(**) return { :label => title_for(**), :id => [:id], :class => [:class], :data => [:data], } end |
#item(**options, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/xrb/formatters/html/option_select.rb', line 61 def item(**, &block) [:field] ||= "id" Builder.fragment(block&.binding || @builder) do |builder| builder.inline(:option, option_attributes_for(**)) do if block_given? builder.capture(self, &block) else builder.text title_for(**) end end end end |
#name_for(**options) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/xrb/formatters/html/option_select.rb', line 25 def name_for(**) if name = @formatter.name_for(**) if [:multiple] name = "#{name}[]" end return name end end |
#option_attributes_for(**options) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/xrb/formatters/html/option_select.rb', line 51 def option_attributes_for(**) return { :value => value_for(**), :selected => .fetch(:selected) {raw_value == raw_value_for(**)}, :id => [:id], :class => [:class], :data => [:data], } end |
#optional? ⇒ Boolean
113 114 115 |
# File 'lib/xrb/formatters/html/option_select.rb', line 113 def optional? @options[:optional] end |
#optional_title_for(**options) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/xrb/formatters/html/option_select.rb', line 75 def optional_title_for(**) if [:optional] == true [:blank] || "" else [:optional] end end |
#raw_value ⇒ Object
39 40 41 |
# File 'lib/xrb/formatters/html/option_select.rb', line 39 def raw_value @raw_value ||= raw_value_for(**@options) end |
#raw_value_for(**options) ⇒ Object
35 36 37 |
# File 'lib/xrb/formatters/html/option_select.rb', line 35 def raw_value_for(**) @formatter.raw_value_for(**) end |
#select_attributes_for(**options) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/xrb/formatters/html/option_select.rb', line 102 def select_attributes_for(**) return { :name => name_for(**), :id => [:id], :class => [:class], :multiple => [:multiple], :data => [:data], :required => [:required], } end |
#title_for(**options) ⇒ Object
47 48 49 |
# File 'lib/xrb/formatters/html/option_select.rb', line 47 def title_for(**) @formatter.title_for(**) end |
#value_for(**options) ⇒ Object
43 44 45 |
# File 'lib/xrb/formatters/html/option_select.rb', line 43 def value_for(**) @formatter.value_for(**) end |