Module: Snails::FormHelpers
- Defined in:
- lib/snails/app.rb
Instance Method Summary collapse
- #delete_link(options = {}) ⇒ Object
- #form_checkbox(object, field, options = {}) ⇒ Object
- #form_input(object, field, options = {}) ⇒ Object
- #form_password(object, field, options = {}) ⇒ Object
- #form_put ⇒ Object
- #form_select(object, field, option_list, options = {}) ⇒ Object
- #form_select_options(list, selected = nil) ⇒ Object
- #form_submit(text = 'Actualizar', classes = '') ⇒ Object
- #form_textarea(object, field, options = {}) ⇒ Object
- #post_button(text, path, opts = {}) ⇒ Object
Instance Method Details
#delete_link(options = {}) ⇒ Object
298 299 300 301 302 303 |
# File 'lib/snails/app.rb', line 298 def delete_link( = {}) ([:text], [:path], .merge({ input_html: "<input type='hidden' name='_method' value='delete' />", css_class: [:css_class] || 'danger' })) end |
#form_checkbox(object, field, options = {}) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/snails/app.rb', line 232 def form_checkbox(object, field, = {}) id, name, index, label = input_base(object, field, ) type = [:type] || :checkbox value = [:value] ? "value='#{[:value]}'" : '' if type.to_sym == :radio checked = object.send(field) == [:value] value += checked ? " selected='selected'" : '' else checked = object.send(field) value += checked ? " checked='true'" : '' end label + raw_input(index, type, id, name, [:placeholder], value) end |
#form_input(object, field, options = {}) ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'lib/snails/app.rb', line 219 def form_input(object, field, = {}) id, name, index, label = input_base(object, field, ) type = ([:type] || :text).to_sym value = [:value] ? "value=\"#{[:value]}\"" : (type == :password ? '' : "value=\"#{object.send(field)}\"") classes = object.errors[field].any? ? 'has-errors' : '' label + raw_input(index, type, id, name, value, [:placeholder], classes, [:required]) end |
#form_password(object, field, options = {}) ⇒ Object
228 229 230 |
# File 'lib/snails/app.rb', line 228 def form_password(object, field, = {}) form_input(object, field, {:type => 'password'}.merge()) end |
#form_put ⇒ Object
270 271 272 |
# File 'lib/snails/app.rb', line 270 def form_put "<input type='hidden' name='_method' value='put' />" end |
#form_select(object, field, option_list, options = {}) ⇒ Object
255 256 257 258 259 260 |
# File 'lib/snails/app.rb', line 255 def form_select(object, field, option_list, = {}) id, name, index, label = input_base(object, field, ) style = [:style] ? "style='#{[:style]}'" : '' = (option_list, object.send(field)) label + "<select #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{}</select>" end |
#form_select_options(list, selected = nil) ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/snails/app.rb', line 262 def (list, selected = nil) list.map do |name, val| val = name if val.nil? sel = val == selected ? 'selected="selected"' : '' "<option #{sel} value=\"#{val}\">#{name}</option>" end.join("\n") end |
#form_submit(text = 'Actualizar', classes = '') ⇒ Object
274 275 276 277 |
# File 'lib/snails/app.rb', line 274 def form_submit(text = 'Actualizar', classes = '') @tabindex = @tabindex ? @tabindex + 1 : 1 "<button tabindex='#{@tabindex}' class='primary #{classes}' type='submit'>#{text}</button>" end |
#form_textarea(object, field, options = {}) ⇒ Object
249 250 251 252 253 |
# File 'lib/snails/app.rb', line 249 def form_textarea(object, field, = {}) id, name, index, label = input_base(object, field, ) style = [:style] ? "style='#{[:style]}'" : '' label + "<textarea #{style} tabindex='#{index}' id='#{id}' name='#{name}'>#{object.send(field)}</textarea>" end |
#post_button(text, path, opts = {}) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/snails/app.rb', line 279 def (text, path, opts = {}) form_id = opts.delete(:form_id) || path.gsub(/[^a-z0-9]/, '_').gsub(/_+/, '_') css_class = opts.delete(:css_class) || '' input_html = opts.delete(:input_html) || '' submit_val = opts.delete(:value) || text style = "style='display:inline; #{opts[:style]}'" onsubmit = if str = opts[:onsubmit] "onsubmit=\"#{str}\"" else confirm_text = opts.delete(:confirm_text) || 'Seguro?' "onsubmit='return confirm(\"#{confirm_text}\");'" end form_tag = "<form id='#{form_id}' method='post' action='#{url(path)}' #{style} #{onsubmit}>" = "<button name='submit' type='submit' class='button #{css_class}' value='#{submit_val}'>#{text}</button>" form_tag + input_html + + '</form>' end |