Class: Radical::Form
- Inherits:
-
Object
- Object
- Radical::Form
- Defined in:
- lib/radical/form.rb
Constant Summary collapse
- SELF_CLOSING_TAGS =
%w[ area base br col embed hr img input keygen link meta param source track wbr ].freeze
Instance Method Summary collapse
- #button(attrs = {}, &block) ⇒ Object
- #close_tag ⇒ Object
- #csrf_tag ⇒ Object
-
#initialize(options, controller) ⇒ Form
constructor
A new instance of Form.
- #number(name, attrs = {}) ⇒ Object
- #open_tag ⇒ Object
- #rack_override_tag ⇒ Object
- #submit(value_or_attrs = {}) ⇒ Object
- #text(name, attrs = {}) ⇒ Object
Constructor Details
#initialize(options, controller) ⇒ Form
Returns a new instance of Form.
25 26 27 28 29 30 31 |
# File 'lib/radical/form.rb', line 25 def initialize(, controller) @model = [:model] @controller = controller @override_method = [:method]&.upcase || (@model&.saved? ? 'PATCH' : 'POST') @method = %w[GET POST].include?(@override_method) ? @override_method : 'POST' @action = [:action] || action_from(model: @model, controller: controller) end |
Instance Method Details
#button(attrs = {}, &block) ⇒ Object
45 46 47 |
# File 'lib/radical/form.rb', line 45 def (attrs = {}, &block) tag 'button', attrs, &block end |
#close_tag ⇒ Object
76 77 78 |
# File 'lib/radical/form.rb', line 76 def close_tag '</form>' end |
#csrf_tag ⇒ Object
66 67 68 |
# File 'lib/radical/form.rb', line 66 def csrf_tag Rack::Csrf.tag(@controller.request.env) end |
#number(name, attrs = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/radical/form.rb', line 39 def number(name, attrs = {}) attrs.merge!(type: 'number', name: name, value: @model&.public_send(name)) tag 'input', attrs end |
#open_tag ⇒ Object
62 63 64 |
# File 'lib/radical/form.rb', line 62 def open_tag "<form #{html_attributes(action: @action, method: @method)}>" end |
#rack_override_tag ⇒ Object
70 71 72 73 74 |
# File 'lib/radical/form.rb', line 70 def rack_override_tag attrs = { value: @override_method, type: 'hidden', name: '_method' } tag('input', attrs) unless %w[GET POST].include?(@override_method) end |
#submit(value_or_attrs = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/radical/form.rb', line 49 def submit(value_or_attrs = {}) attrs = {} case value_or_attrs when String attrs[:value] = value_or_attrs when Hash attrs = value_or_attrs || {} end tag 'input', attrs.merge('type' => 'submit') end |
#text(name, attrs = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/radical/form.rb', line 33 def text(name, attrs = {}) attrs.merge!(type: 'text', name: name, value: @model&.public_send(name)) tag 'input', attrs end |