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

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, options = {})
  str = FormBuilder.prologue
      
  controls_for_properties(str, obj, options)
  
  unless options[:no_relations]
   controls_for_relations(str, obj, options)
  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, options = {})
  for prop in obj.class.properties.values
    unless options[:all]
      next if prop.symbol == obj.class.primary_key.symbol or prop[:control] == :none or prop[:relation]
    end
    control = Form::Control.fetch(obj, prop, options).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, options = {})
  for rel in obj.class.relations        
    unless options[:all]
      next if rel[:control] == :none
    end
    control = Form::Control.fetch(obj, rel, options).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

Yields:

  • (b)


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 options = {}, &block
  obj = (options[:object] ||= options[:entity] || options[:class])
  
  # If the passed obj is a Class instantiate an empty object
  # of this class.
  
  if obj.is_a? Class
    obj = options[:object] = obj.allocate
  end

  # Convert virtual :multipart method to method="post",
  # enctype="multipart/form-data"
  
  if options[:method] == :multipart
    options[:method] = :post
    options[:enctype] = 'multipart/form-data'
  end    
      
  b = FormXmlBuilder.new('', options)

  b << '<form'
  b << %| action="#{R options[:action]}"| if options[:action]
  b << %| method="#{options[:method]}"| if options[:method]
  b << %| enctype="#{options[:enctype]}"| if options[: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, options = {})
  str = ''
  
  name = obj.class.name.underscore

  method = options.fetch(:method, 'post')
  action = options.fetch(:action, "save_#{obj.class.name.underscore}")
  submit = options.fetch(:submit, 'Save')
  cancel = options.fetch(:cancel, "#@base/#{Scaffolding.class_to_list(obj.class)}")  
  
  if enctype = options.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, options)
  str << %{<input type="submit" value="#{submit}" />}
  str << %{ or <a href="#{cancel}">Cancel</a>} if cancel
  str << %{</form>}
  
  return str  
end