Class: Trenni::Formatters::HTML::OptionSelect
- Inherits:
-
Object
- Object
- Trenni::Formatters::HTML::OptionSelect
- Defined in:
- lib/trenni/formatters/html/option_select.rb
Overview
Standard drop-down select box:
Class Method Summary collapse
Instance Method Summary collapse
- #call(options = {}, &block) ⇒ Object
- #group(options = {}, &block) ⇒ Object
- #group_attributes_for(options) ⇒ Object
-
#initialize(formatter, options, builder) ⇒ OptionSelect
constructor
A new instance of OptionSelect.
- #item(options = {}) ⇒ Object
- #name_for(options) ⇒ Object
- #option_attributes_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, options, builder) ⇒ OptionSelect
Returns a new instance of OptionSelect.
34 35 36 37 38 39 40 41 42 |
# File 'lib/trenni/formatters/html/option_select.rb', line 34 def initialize(formatter, , builder) @formatter = formatter @object = formatter.object @field = [:field] @options = @builder = builder end |
Class Method Details
.call(formatter, options, builder, &block) ⇒ Object
28 29 30 31 32 |
# File 'lib/trenni/formatters/html/option_select.rb', line 28 def self.call(formatter, , builder, &block) instance = self.new(formatter, , builder) instance.call(, &block) end |
Instance Method Details
#call(options = {}, &block) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/trenni/formatters/html/option_select.rb', line 119 def call( = {}, &block) Builder.fragment(@builder) do |builder| builder.tag :select, select_attributes_for() do if [:optional] item(:title => '', :value => '', :builder => builder) end builder.append Trenni::Template.capture(self, &block) end end end |
#group(options = {}, &block) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/trenni/formatters/html/option_select.rb', line 97 def group( = {}, &block) Builder.fragment(@builder) do |builder| builder.tag :optgroup, group_attributes_for() do if [:optional] item(:title => '', :value => '', :builder => builder) end builder.append Trenni::Template.capture(&block) end end end |
#group_attributes_for(options) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/trenni/formatters/html/option_select.rb', line 88 def group_attributes_for() return { :label => title_for(), :id => [:id], :class => [:class], :data => [:data], } end |
#item(options = {}) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/trenni/formatters/html/option_select.rb', line 80 def item( = {}) [:field] ||= 'id' Builder.fragment([:builder]) do |builder| builder.inline(:option, option_attributes_for()) { builder.text title_for() } end end |
#name_for(options) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/trenni/formatters/html/option_select.rb', line 44 def name_for() if name = @formatter.name_for() if [:multiple] name = "#{name}[]" end return name end end |
#option_attributes_for(options) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/trenni/formatters/html/option_select.rb', line 70 def option_attributes_for() return { :value => value_for(), :selected => .fetch(:selected){ raw_value == raw_value_for() }, :id => [:id], :class => [:class], :data => [:data], } end |
#raw_value ⇒ Object
58 59 60 |
# File 'lib/trenni/formatters/html/option_select.rb', line 58 def raw_value @raw_value ||= raw_value_for(@options) end |
#raw_value_for(options) ⇒ Object
54 55 56 |
# File 'lib/trenni/formatters/html/option_select.rb', line 54 def raw_value_for() @formatter.raw_value_for() end |
#select_attributes_for(options) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/trenni/formatters/html/option_select.rb', line 109 def select_attributes_for() return { :name => name_for(), :id => [:id], :class => [:class], :multiple => [:multiple], :data => [:data], } end |
#title_for(options) ⇒ Object
66 67 68 |
# File 'lib/trenni/formatters/html/option_select.rb', line 66 def title_for() @formatter.title_for() end |
#value_for(options) ⇒ Object
62 63 64 |
# File 'lib/trenni/formatters/html/option_select.rb', line 62 def value_for() @formatter.value_for() end |