Module: XRB::Formatters::HTML::FormFormatter
- Included in:
- DefinitionListForm, LabelForm
- Defined in:
- lib/xrb/formatters/html/form_formatter.rb
Instance Method Summary collapse
-
#button(**options) ⇒ Object
A hidden field.
- #button_attributes_for(**options) ⇒ Object
- #button_title_for(**options) ⇒ Object
- #checkbox_attributes_for(**options) ⇒ Object
-
#details_for(**options) ⇒ Object
Any additional details relating to a field (e.g. explanation text).
- #field_for(**options) ⇒ Object
- #fieldset(**options, &block) ⇒ Object
-
#hidden(**options) ⇒ Object
A hidden field.
- #hidden_attributes_for(**options) ⇒ Object
- #input_attributes_for(**options) ⇒ Object
-
#new_record? ⇒ Boolean
Return true if the object is begin created or false if it is being updated.
- #object_value_for(**options) ⇒ Object
- #output_attributes_for(**options) ⇒ Object
- #pattern_for(**options) ⇒ Object
- #placeholder_for(**options) ⇒ Object
- #raw_value_for(**options) ⇒ Object
- #submit_attributes_for(**options) ⇒ Object
- #submit_title_for(**options) ⇒ Object
- #textarea_attributes_for(**options) ⇒ Object
-
#title_for(**options) ⇒ Object
A title is a text string that will be displayed next to or on top of the control to describe it or its value:.
-
#value_for(**options) ⇒ Object
The value of the field.
Instance Method Details
#button(**options) ⇒ Object
A hidden field.
195 196 197 198 199 200 201 202 203 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 195 def (**) = @options.merge(**) Builder.fragment do |builder| builder.inline :button, (**) do builder.text (**) end end end |
#button_attributes_for(**options) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 172 def (**) return { :type => [:type] || "submit", :name => name_for(**), :id => [:id], :class => [:class], :disabled => [:disabled], :value => value_for(**), :data => [:data], } end |
#button_title_for(**options) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 184 def (**) type = .fetch(:type, "submit").to_sym if type == :submit submit_title_for(**) else title_for(**) || Strings::to_title(type.to_s) end end |
#checkbox_attributes_for(**options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 121 def checkbox_attributes_for(**) return { :type => [:type] || "checkbox", :id => [:id], :class => [:class], :name => name_for(**), :value => "true", :checked => raw_value_for(**), :required => [:required], :disabled => [:disabled], :readonly => [:readonly], :data => [:data], } end |
#details_for(**options) ⇒ Object
Any additional details relating to a field (e.g. explanation text)
18 19 20 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 18 def details_for(**) [:details] end |
#field_for(**options) ⇒ Object
22 23 24 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 22 def field_for(**) [:field] end |
#fieldset(**options, &block) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 205 def fieldset(**, &block) = @options.merge(**) buffer = XRB::Template.buffer(block.binding) Builder.fragment(buffer) do |builder| builder.tag("fieldset") do builder.inline("legend") do builder.text title_for(**) end yield(builder) end end end |
#hidden(**options) ⇒ Object
A hidden field.
164 165 166 167 168 169 170 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 164 def hidden(**) = @options.merge(**) Builder.fragment do |builder| builder.tag :input, hidden_attributes_for(**) end end |
#hidden_attributes_for(**options) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 152 def hidden_attributes_for(**) return { :type => [:type] || "hidden", :id => [:id], :class => [:class], :name => name_for(**), :value => value_for(**), :data => [:data], } end |
#input_attributes_for(**options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 67 def input_attributes_for(**) attributes = { :type => [:type], :name => name_for(**), :id => [:id], :class => [:class], :value => value_for(**), :required => [:required], :disabled => [:disabled], :readonly => [:readonly], :pattern => pattern_for(**), :placeholder => placeholder_for(**), # for <input type="range|number"> :min => [:minimum] || [:min], :max => [:maximum] || [:max], :step => [:step], # for <input type="text"> :minlength => [:minimum] || [:minlength], :maxlength => [:maximum] || [:maxlength], :data => [:data], } return attributes end |
#new_record? ⇒ Boolean
Return true if the object is begin created or false if it is being updated.
13 14 15 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 13 def new_record? object.nil? or object.new_record? end |
#object_value_for(**options) ⇒ Object
39 40 41 42 43 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 39 def object_value_for(**) if object = [:object] and field = field_for(**) object.send(field) end end |
#output_attributes_for(**options) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 92 def output_attributes_for(**) attributes = { :name => name_for(**), :id => [:id], :class => [:class], :for => [:for], :form => [:form], :data => [:data], } return attributes end |
#pattern_for(**options) ⇒ Object
59 60 61 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 59 def pattern_for(**) [:pattern] end |
#placeholder_for(**options) ⇒ Object
63 64 65 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 63 def placeholder_for(**) [:placeholder] end |
#raw_value_for(**options) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 45 def raw_value_for(**) value = .fetch(:value) {object_value_for(**)} # Allow to specify a default value if the value given, usually from an object, is nil. value || [:default] end |
#submit_attributes_for(**options) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 136 def submit_attributes_for(**) return { :type => [:type] || "submit", :name => name_for(**), :id => [:id], :class => [:class], :disabled => [:disabled], :value => title_for(**), :data => [:data], } end |
#submit_title_for(**options) ⇒ Object
148 149 150 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 148 def submit_title_for(**) title_for(**) || (new_record? ? "Create" : "Update") end |
#textarea_attributes_for(**options) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 105 def textarea_attributes_for(**) return { :name => name_for(**), :id => [:id], :class => [:class], :required => [:required], :disabled => [:disabled], :readonly => [:readonly], :pattern => pattern_for(**), :placeholder => placeholder_for(**), :minlength => [:minlength], :maxlength => [:maxlength], :data => [:data], } end |
#title_for(**options) ⇒ Object
A title is a text string that will be displayed next to or on top of the control to describe it or its value:
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 27 def title_for(**) if title = [:title] return title end # Generate a title from a field name: if field_name = field_for(**) # Remove postfix "_id" or "_ids": return Strings::to_title(field_name.to_s.sub(/_ids?/, "")) end end |
#value_for(**options) ⇒ Object
The value of the field.
53 54 55 56 57 |
# File 'lib/xrb/formatters/html/form_formatter.rb', line 53 def value_for(**) if value = raw_value_for(**) self.format(value, **) end end |