Module: Trenni::Formatters::HTML::FormFormatter
- Included in:
- DefinitionListForm, LabelForm
- Defined in:
- lib/trenni/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.
212 213 214 215 216 217 218 219 220 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 212 def (**) = @options.merge(**) Builder.fragment do |builder| builder.inline :button, (**) do builder.text (**) end end end |
#button_attributes_for(**options) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 189 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
201 202 203 204 205 206 207 208 209 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 201 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 138 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)
35 36 37 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 35 def details_for(**) [:details] end |
#field_for(**options) ⇒ Object
39 40 41 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 39 def field_for(**) [:field] end |
#fieldset(**options, &block) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 222 def fieldset(**, &block) = @options.merge(**) buffer = Trenni::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.
181 182 183 184 185 186 187 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 181 def hidden(**) = @options.merge(**) Builder.fragment do |builder| builder.tag :input, hidden_attributes_for(**) end end |
#hidden_attributes_for(**options) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 169 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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 84 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.
30 31 32 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 30 def new_record? object.new_record? end |
#object_value_for(**options) ⇒ Object
56 57 58 59 60 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 56 def object_value_for(**) if object = [:object] and field = field_for(**) object.send(field) end end |
#output_attributes_for(**options) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 109 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
76 77 78 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 76 def pattern_for(**) [:pattern] end |
#placeholder_for(**options) ⇒ Object
80 81 82 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 80 def placeholder_for(**) [:placeholder] end |
#raw_value_for(**options) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 62 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
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 153 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
165 166 167 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 165 def submit_title_for(**) title_for(**) || (new_record? ? 'Create' : 'Update') end |
#textarea_attributes_for(**options) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 122 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:
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 44 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.
70 71 72 73 74 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 70 def value_for(**) if value = raw_value_for(**) self.format(value, **) end end |