Module: Formalist::Element::ClassInterface
- Included in:
- Formalist::Element
- Defined in:
- lib/formalist/element/class_interface.rb
Overview
Class-level API for form elements.
Class Method Summary collapse
-
.attribute(name, type, default: nil) ⇒ Object
Define a form element attribute.
-
.attributes_schema ⇒ Hash<Symbol, Hash>
Returns the attributes schema for the form element.
-
.permitted_children(*args) ⇒ Object
Sets or fetches the policy for the element’s permitted child form elements.
-
.type ⇒ Symbol
Returns the element’s type, which is a symbolized, camlized representation of the element’s class name.
Class Method Details
.attribute(name, type, default: nil) ⇒ Object
Define a form element attribute.
Form element attributes can be set when the element is defined, and can be further populated by the form element object itself, when the form is being built, using both the user input and any dependencies passed to the element via the form.
Attributes are the way to ensure the form renderer has all the information it needs to render the element appropriately. Attributes are type-checked in order to ensure they’re being passed appropriate values. Attributes can hold any type of value as long as it can be reduced to an abstract syntax tree representation by ‘Form::Element::Attributes#to_ast`.
51 52 53 |
# File 'lib/formalist/element/class_interface.rb', line 51 def attribute(name, type, default: nil) attributes(name => {type: type, default: default}) end |
.attributes_schema ⇒ Hash<Symbol, Hash>
Returns the attributes schema for the form element.
Each item in the schema includes a type definition and a default value (nil if none specified).
69 70 71 72 |
# File 'lib/formalist/element/class_interface.rb', line 69 def attributes_schema super_schema = superclass.respond_to?(:attributes_schema) ? superclass.attributes_schema : {} super_schema.merge(@attributes_schema || {}) end |
.permitted_children(policy) ⇒ Object .permitted_children(element_type, ...) ⇒ Object .permitted_children ⇒ #permitted?
Sets or fetches the policy for the element’s permitted child form elements.
111 112 113 114 115 116 117 118 119 |
# File 'lib/formalist/element/class_interface.rb', line 111 def permitted_children(*args) return @permitted_children ||= PermittedChildren.all if args.empty? @permitted_children = if %i[all none].include?(args.first) PermittedChildren.send(args.first) else PermittedChildren[args] end end |
.type ⇒ Symbol
Returns the element’s type, which is a symbolized, camlized representation of the element’s class name.
This is a critical hook for customising form rendering when using custom form elements, since the type in this case will be based on the name of form element’s sublass.
26 27 28 |
# File 'lib/formalist/element/class_interface.rb', line 26 def type Inflecto.underscore(Inflecto.demodulize(name)).to_sym end |