Sha256: d66831f0deff1029a9d33116aa88cae746462e2420918e8fa726cc2dc6feeec4

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'glue/hash'

module Nitro

# A collection of useful helpers for creating and manipulating
# Forms.

module FormMixin
  
  # Render a standard form for the given Entity (ie an
  # object with attribute metadata).
  # If show_all is false then apply field filtering.
  #
  # Example:
  #
  #  <p>
  #    <form name="test">
  #      #{form_for entry}
  #    </form>
  #  </p>
  #--
  # TODO: get info, for example localization mode from session,
  # if this module is mixed in a Render.
  #++
  
  def form_for(obj, lc = nil, show_all = false)
    str = '<dl>'
    
    for p in obj.class.properties
      unless show_all
        next if :oid == p.symbol
      end
      
      if p.klass.ancestors.include?(Integer) or
          p.klass.ancestors.include?(Float) 
        str << %{
  <dt><label for="#{p.symbol}">#{p.symbol}</label></dt>
  <dd>
    <input type="text" id="#{p.symbol}" name="#{p.symbol}" value="#{obj.send(p.symbol)}" />
  </dd>
        }
      elsif p.klass.ancestors.include?(String)
        str << %{
  <dt><label for="#{p.symbol}">#{p.symbol}</label></dt>
  <dd>
        }
        val = obj.send(p.symbol)
        if :textarea == p.meta[:ui]
          str << %{
    <textarea id="#{p.symbol}" name="#{p.symbol}">#{val}</textarea>
          }
        else
          str << %{
    <input type="text" id="#{p.symbol}" name="#{p.symbol}" value="#{val}" />
          }        
        end
        str << %{
  </dd>
        }
      elsif p.klass.ancestors.include?(TrueClass)
        str << %{
  <dt><label for="#{p.symbol}">#{p.symbol}</label></dt>
  <dd>
    <input type="checkbox" id="#{p.symbol}" name="#{p.symbol}" />
  </dd>
        }
      elsif p.klass.ancestors.include?(Time)
        str << %{
  <dt><label for="#{p.symbol}">#{p.symbol}</label></dt>
  <dd>
    <input type="text" id="#{p.symbol}" name="#{p.symbol}" value="#{obj.send(p.symbol)}" />
  </dd>
        }
      end
    end

    str << %{
</dl>}
    
    self << str
  end
  alias_method :build_form, :form_for
  
end

end 

# * George Moschovitis

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nitro-0.21.0 lib/nitro/mixin/form.rb
nitro-0.21.2 lib/nitro/mixin/form.rb
nitro-0.22.0 lib/nitro/mixin/form.rb