Class: Superform::DOM
- Inherits:
-
Object
- Object
- Superform::DOM
- Defined in:
- lib/superform/dom.rb
Overview
Generates DOM IDs, names, etc. for a Field, Namespace, or Node based on norms that were established by Rails. These can be used outsidef or Rails in other Ruby web frameworks since it has now dependencies on Rails.
Constant Summary collapse
- DELIMITER =
"_"
Class Method Summary collapse
Instance Method Summary collapse
-
#array_name ⇒ Object
Returns the name with
[]appended for array/multiple value fields. -
#id ⇒ Object
Walks from the current node to the parent node, grabs the names, and seperates them with a
_for a DOM ID. -
#initialize(field:) ⇒ DOM
constructor
A new instance of DOM.
-
#inspect ⇒ Object
Emit the id, name, and value in an HTML tag-ish that doesnt have an element.
-
#name ⇒ Object
The
nameattribute of a node, which is influenced by Rails (not sure where Rails got it from). -
#value ⇒ Object
Converts the value of the field to a String, which is required to work with Phlex.
Constructor Details
#initialize(field:) ⇒ DOM
Returns a new instance of DOM.
12 13 14 |
# File 'lib/superform/dom.rb', line 12 def initialize(field:) @field = field end |
Class Method Details
.join(*segments) ⇒ Object
8 9 10 |
# File 'lib/superform/dom.rb', line 8 def self.join(*segments) segments.join(DELIMITER) end |
Instance Method Details
#array_name ⇒ Object
Returns the name with [] appended for array/multiple value fields.
Used by multiple selects, checkbox groups, etc.
40 41 42 |
# File 'lib/superform/dom.rb', line 40 def array_name "#{name}[]" end |
#id ⇒ Object
Walks from the current node to the parent node, grabs the names, and seperates
them with a _ for a DOM ID. One limitation of this approach is if multiple forms
exist on the same page, the ID may be duplicate.
25 26 27 |
# File 'lib/superform/dom.rb', line 25 def id self.class.join(*lineage.map(&:key)) end |
#inspect ⇒ Object
Emit the id, name, and value in an HTML tag-ish that doesnt have an element.
45 46 47 |
# File 'lib/superform/dom.rb', line 45 def inspect "<id=#{id.inspect} name=#{name.inspect} value=#{value.inspect}/>" end |
#name ⇒ Object
The name attribute of a node, which is influenced by Rails (not sure where Rails got
it from). All node names, except the parent node, are wrapped in a [] and collections
are left empty. For example, user[addresses][][street] would be created for a form with
data shaped like {user: {addresses: [{street: "Sesame Street"}]}}.
33 34 35 36 |
# File 'lib/superform/dom.rb', line 33 def name root, *names = keys names.map { |name| "[#{name}]" }.unshift(root).join end |
#value ⇒ Object
Converts the value of the field to a String, which is required to work
with Phlex. Assumes that Object#to_s emits a format suitable for the web form.
18 19 20 |
# File 'lib/superform/dom.rb', line 18 def value @field.value.to_s end |