Class: Nitro::FormHelper::FormXmlBuilder
- Inherits:
-
Glue::XmlBuilder
- Object
- Glue::XmlBuilder
- Nitro::FormHelper::FormXmlBuilder
- Defined in:
- lib/nitro/helper/form.rb
Overview
A specialized Builder for dynamically building of forms. Provides extra support for forms backed by managed objects (entities). – TODO: allow multiple objects per form. TODO: use more generalized controls. ++
Instance Attribute Summary
Attributes inherited from Glue::XmlBuilder
Class Method Summary collapse
-
.control_for(obj, a, anno, options) ⇒ Object
Returns a control for the given objects attribute.
-
.control_for_relation(obj, rel, options) ⇒ Object
Returns a control for the given objects relation.
Instance Method Summary collapse
-
#all_attributes(options = {}) ⇒ Object
(also: #attributes, #serializable_attributes)
Render controls for all attributes of the form object.
-
#all_relations(options = {}) ⇒ Object
(also: #relations)
Render controls for all relations of the form object.
-
#attribute(a, options = {}) ⇒ Object
(also: #attr)
Render a control+label for the given property of the form object.
-
#form_errors ⇒ Object
If flash is filled with errors structured as name/message pairs the method creates a div containing them, otherwise it returns an empty string.
-
#initialize(buffer = '', options = {}) ⇒ FormXmlBuilder
constructor
A new instance of FormXmlBuilder.
-
#relation(rel, options = {}) ⇒ Object
(also: #rel)
Input.
-
#select_file(name, options = {}) ⇒ Object
Renders a control to select a file for upload.
Methods inherited from Glue::XmlBuilder
Constructor Details
#initialize(buffer = '', options = {}) ⇒ FormXmlBuilder
Returns a new instance of FormXmlBuilder.
74 75 76 77 78 |
# File 'lib/nitro/helper/form.rb', line 74 def initialize(buffer = '', = {}) super @obj = [:object] @errors = [:errors] end |
Class Method Details
.control_for(obj, a, anno, options) ⇒ Object
Returns a control for the given objects attribute.
60 61 62 63 64 |
# File 'lib/nitro/helper/form.rb', line 60 def self.control_for(obj, a, anno, ) name = anno[:control] || anno[:class].to_s.demodulize.underscore.to_sym control_class = self.control_map.fetch(name, NoneControl) return control_class.new(obj, a, ) end |
.control_for_relation(obj, rel, options) ⇒ Object
Returns a control for the given objects relation.
68 69 70 71 72 |
# File 'lib/nitro/helper/form.rb', line 68 def self.control_for_relation(obj, rel, ) name = rel[:control] || rel.class.to_s.demodulize.underscore.to_sym control_class = self.control_map.fetch(name, NoneControl) return control_class.new(obj, rel, ) end |
Instance Method Details
#all_attributes(options = {}) ⇒ Object Also known as: attributes, serializable_attributes
Render controls for all attributes of the form object. It only considers serializable attributes.
96 97 98 99 100 101 102 103 104 |
# File 'lib/nitro/helper/form.rb', line 96 def all_attributes( = {}) for a in @obj.class.serializable_attributes prop = @obj.class.ann(a) unless [:all] next if a == @obj.class.primary_key or prop[:control] == :none or prop[:relation] or [[:exclude]].flatten.include?(a) end attribute a, end end |
#all_relations(options = {}) ⇒ Object Also known as: relations
Render controls for all relations of the form object.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/nitro/helper/form.rb', line 131 def all_relations( = {}) for rel in @obj.class.relations unless [:all] # Ignore polymorphic_marker relations. #-- # gmosx: should revisit the handling of polymorphic # relations, feels hacky. #++ next if (rel[:control] == :none) or rel.polymorphic_marker? end relation rel, end end |
#attribute(a, options = {}) ⇒ Object Also known as: attr
Render a control+label for the given property of the form object.
83 84 85 86 87 88 89 90 |
# File 'lib/nitro/helper/form.rb', line 83 def attribute(a, = {}) if anno = @obj.class.ann[a] control = self.class.control_for(@obj, a, anno, ) print element(a, anno, control.render) else raise "Undefined attribute '#{a}' for class '#{@obj.class}'." end end |
#form_errors ⇒ Object
If flash is filled with errors structured as name/message pairs the method creates a div containing them, otherwise it returns an empty string.
So you can write code like
#{form_errors}
<form>... </form>
and redirect the user to the form in case of errors, thus allowing him to see what was wrong.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/nitro/helper/form.rb', line 163 def form_errors res = '' unless @errors.empty? res << %{<div class="error">\n<ul>\n} for err in @errors if err.is_a? Array res << "<li><strong>#{err[0].to_s.humanize}</strong>: #{err[1]}</li>\n" else res << "<li>#{err}</li>\n" end end res << %{</ul>\n</div>\n} end print(res) end |
#relation(rel, options = {}) ⇒ Object Also known as: rel
Input
-
rel = The relation name as symbol, or the actual relation object.
– FIXME: Fix the mismatch with the attributes. ++
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/nitro/helper/form.rb', line 116 def relation(rel, = {}) # If the relation name is passed, lookup the actual # relation. if rel.is_a? Symbol rel = @obj.class.relation(rel) end control = self.class.control_for_relation(@obj, rel, ) print element(rel[:symbol], rel, control.render) end |
#select_file(name, options = {}) ⇒ Object
Renders a control to select a file for upload.
148 149 150 |
# File 'lib/nitro/helper/form.rb', line 148 def select_file(name, = {}) print %|<input type="file" name="#{name}" />| end |