Module: Nitro::FormHelper
- Included in:
- Render::Emitter
- Defined in:
- lib/nitro/helper/form.rb,
lib/nitro/helper/form/builder.rb
Overview
A collection of useful helpers for creating and manipulating Forms. – This helper may by applied at run time so try to optimize this. ++
Defined Under Namespace
Modules: FormBuilder Classes: FormXmlBuilder
Class Method Summary collapse
Instance Method Summary collapse
-
#controls_for(obj, options = {}) ⇒ Object
Render the controls for the given object.
-
#controls_for_properties(str, obj, options = {}) ⇒ Object
Render the controls for the properties of the given object.
-
#controls_for_relations(str, obj, options = {}) ⇒ Object
Render the controls for the relations of the given object.
-
#form(options = {}) {|b| ... } ⇒ Object
A sophisticated form generation helper method.
-
#form_for(obj, options = {}) ⇒ Object
Render a standard form for the given object.
Class Method Details
.included(base) ⇒ Object
17 18 19 20 |
# File 'lib/nitro/helper/form.rb', line 17 def self.included(base) super base.send :include, XhtmlHelper end |
Instance Method Details
#controls_for(obj, options = {}) ⇒ Object
Render the controls for the given object.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nitro/helper/form.rb', line 108 def controls_for(obj, = {}) str = FormBuilder.prologue controls_for_properties(str, obj, ) unless [:no_relations] controls_for_relations(str, obj, ) end str << FormBuilder.epilogue return str end |
#controls_for_properties(str, obj, options = {}) ⇒ Object
Render the controls for the properties of the given object.
124 125 126 127 128 129 130 131 132 |
# File 'lib/nitro/helper/form.rb', line 124 def controls_for_properties(str, obj, = {}) for prop in obj.class.properties.values unless [:all] next if prop.symbol == obj.class.primary_key.symbol or prop[:control] == :none or prop[:relation] end control = Form::Control.fetch(obj, prop, ).render str << FormBuilder.element(prop, control) end end |
#controls_for_relations(str, obj, options = {}) ⇒ Object
Render the controls for the relations of the given object.
136 137 138 139 140 141 142 143 144 |
# File 'lib/nitro/helper/form.rb', line 136 def controls_for_relations(str, obj, = {}) for rel in obj.class.relations unless [:all] next if rel[:control] == :none end control = Form::Control.fetch(obj, rel, ).render str << FormBuilder.element(rel, control) end end |
#form(options = {}) {|b| ... } ⇒ Object
A sophisticated form generation helper method.
Options
-
:object, :entity, :class = The object that acts as model for this form. If you pass a class an empty object is instantiated.
-
:action = The action of this form. The parameter is passed through the R operator (encode_url) to support advanced url encoding.
Example
#=> @owner, :action => :save_profile) do |f|
f.property :name, :editable => false
f.property :password
f.br
f.submit 'Update'
end
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/nitro/helper/form/builder.rb', line 106 def form = {}, &block obj = ([:object] ||= [:entity] || [:class]) # If the passed obj is a Class instantiate an empty object # of this class. if obj.is_a? Class obj = [:object] = obj.allocate end # Convert virtual :multipart method to method="post", # enctype="multipart/form-data" if [:method] == :multipart [:method] = :post [:enctype] = 'multipart/form-data' end b = FormXmlBuilder.new('', ) b << '<form' b << %| action="#{R options[:action]}"| if [:action] b << %| method="#{options[:method]}"| if [:method] b << %| enctype="#{options[:enctype]}"| if [:enctype] b << '>' b.hidden(:name => 'oid', :value => obj.oid) if obj and obj.saved? yield b b << '</form>' return b end |
#form_for(obj, options = {}) ⇒ Object
Render a standard form for the given object. – RETHINK this method. ++
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/nitro/helper/form.rb', line 69 def form_for(obj, = {}) str = '' name = obj.class.name.underscore method = .fetch(:method, 'post') action = .fetch(:action, "save_#{obj.class.name.underscore}") submit = .fetch(:submit, 'Save') cancel = .fetch(:cancel, "#@base/#{Scaffolding.class_to_list(obj.class)}") if enctype = .fetch(:enctype, nil) enctype_attribute = " enctype=\"#{enctype}\"" else enctype_attribute = '' for prop in obj.class.properties.values if prop.klass.ancestors.include? Og::Blob enctype_attribute = ' enctype="multipart/form-data"' end end end if action == :self str << %{<form method="post"#{enctype_attribute}>} else action = "#{@base}/#{action}" unless action =~ /\// str << %{<form action="#{action}" method="post"#{enctype_attribute}>} end str << %{<input type="hidden" name="oid" value="#{obj.oid}" />} if obj.oid str << controls_for(obj, ) str << %{<input type="submit" value="#{submit}" />} str << %{ or <a href="#{cancel}">Cancel</a>} if cancel str << %{</form>} return str end |