A specialized Builder for dynamically building of forms. Provides extra support for forms backed by managed objects (entities).
- all_attributes
- all_relations
- attr
- attribute
- attributes
- control_for
- control_for_relation
- form_errors
- new
- rel
- relation
- relations
- select_file
- serializable_attributes
Returns a control for the given objects attribute.
[ show source ]
# File lib/raw/view/form.rb, line 60
60: def self.control_for(obj, a, anno, options)
61: raise "Invalid attribute '#{a}' for object '#{obj}'" if anno.nil?
62: name = anno[:control] || anno[:class].to_s.demodulize.underscore.to_sym
63: control_class = self.control_map.fetch(name, NoneControl)
64: return control_class.new(obj, a, options)
65: end
Returns a control for the given objects relation.
[ show source ]
# File lib/raw/view/form.rb, line 69
69: def self.control_for_relation(obj, rel, options)
70: name = rel[:control] || rel.class.to_s.demodulize.underscore.to_sym
71: control_class = self.control_map.fetch(name, NoneControl)
72: return control_class.new(obj, rel, options)
73: end
[ show source ]
# File lib/raw/view/form.rb, line 75
75: def initialize(buffer = '', options = {})
76: super
77: @obj = options[:object]
78: @errors = options[:errors]
79: end
Render controls for all attributes of the form object. It only considers serializable attributes.
[ show source ]
# File lib/raw/view/form.rb, line 97
97: def all_attributes(options = {})
98: for a in @obj.class.serializable_attributes
99: prop = @obj.class.ann(a)
100: unless options[:all]
101: next if a == @obj.class.primary_key or prop[:control] == :none or prop[:relation] or [options[:exclude]].flatten.include?(a)
102: end
103: attribute a, options
104: end
105: end
Render controls for all relations of the form object.
[ show source ]
# File lib/raw/view/form.rb, line 132
132: def all_relations(options = {})
133: for rel in @obj.class.relations
134: unless options[:all]
135: # Ignore polymorphic_marker relations.
136: #--
137: # gmosx: should revisit the handling of polymorphic
138: # relations, feels hacky.
139: #++
140: next if (rel[:control] == :none) or rel.polymorphic_marker?
141: end
142: relation rel, options
143: end
144: end
Alias for #attribute
Render a control+label for the given property of the form object.
[ show source ]
# File lib/raw/view/form.rb, line 84
84: def attribute(a, options = {})
85: if anno = @obj.class.ann(a)
86: control = self.class.control_for(@obj, a, anno, options)
87: print element(a, anno, control.render)
88: else
89: raise "Undefined attribute '#{a}' for class '#{@obj.class}'."
90: end
91: end
Alias for #all_attributes
If flash[:ERRORS] 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.
[ show source ]
# File lib/raw/view/form.rb, line 164
164: def form_errors
165: res = ''
166:
167: unless @errors.empty?
168: res << %{<div class="error">\n<ul>\n}
169: for err in @errors
170: if err.is_a? Array
171: res << "<li><strong>#{err[0].to_s.humanize}</strong>: #{err[1]}</li>\n"
172: else
173: res << "<li>#{err}</li>\n"
174: end
175: end
176: res << %{</ul>\n</div>\n}
177: end
178:
179: print(res)
180: end
Alias for #relation
Input
- rel = The relation name as symbol, or the actual relation object.
[ show source ]
# File lib/raw/view/form.rb, line 117
117: def relation(rel, options = {})
118: # If the relation name is passed, lookup the actual
119: # relation.
120:
121: if rel.is_a? Symbol
122: rel = @obj.class.relation(rel)
123: end
124:
125: control = self.class.control_for_relation(@obj, rel, options)
126: print element(rel[:symbol], rel, control.render)
127: end
Alias for #all_relations
Renders a control to select a file for upload.
[ show source ]
# File lib/raw/view/form.rb, line 149
149: def select_file(name, options = {})
150: print %|<input type="file" name="#{name}" />|
151: end
Alias for #all_attributes