Module: Waves::Helpers::Form

Defined in:
lib/waves/helpers/form.rb

Overview

Form helpers are used in generating forms. Since Hoshi already provides Ruby methods for basic form generation, the focus of this helper is on providing methods to handle things that go beyond the basics.

Example:

editor( @blog.entry, :heading => ‘Edit Blog Entry’, :method => :put)

property @blog.entry, :name => :title, :type => :text
property @blog.entry, :name => :content, :type => :text, :size => large
property @blog.entry, :name => :published?, :type => :boolean

Instance Method Summary collapse

Instance Method Details

#boolean_field(instance, options) ⇒ Object



64
65
66
67
68
# File 'lib/waves/helpers/form.rb', line 64

def boolean_field( instance, options )
  on = instance.send( options[:name] )
  radio_button( 'Yes', "#{instance.class.basename.snake_case}.#{options[:name]}", 't', on )
  radio_button( 'No', "#{instance.class.basename.snake_case}.#{options[:name]}", 'f', !on )
end

#cancel(label = "Cancel") ⇒ Object



85
86
87
# File 'lib/waves/helpers/form.rb', line 85

def cancel( label = "Cancel" )
  a label, :href => 'javascript:window.history.back()'
end

#editor(instance, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/waves/helpers/form.rb', line 20

def editor( instance, options = {}) 
  options = { :method => :put, :buttons => true, 
    :heading => instance.class.name.in_words.title_case }.merge( options )
  h1 options[:heading] if options[:heading]
  form( :method => :post, :action => paths.put( instance.key ) ) {
    input( :type => :hidden, :name => :_method, :value => :put ) if options[:method] == :put
    div.properties { yield }
    p( :class => 'button panel' ) { submit; cancel } if options[:buttons]
  }
end

#file_field(instance, options) ⇒ Object



77
78
79
# File 'lib/waves/helpers/form.rb', line 77

def file_field( instance, options )
  input( :type => :file, :name => "#{instance.class.basename.snake_case}.#{options[:name]}" )
end

#float_field(instance, options) ⇒ Object



59
60
61
62
# File 'lib/waves/helpers/form.rb', line 59

def float_field( instance, options )
  input( :name => "#{instance.class.basename.snake_case}.#{options[:name]}", 
    :type => :text, :value => instance.send( options[:name] ) )
end

#integer_field(instance, options) ⇒ Object



54
55
56
57
# File 'lib/waves/helpers/form.rb', line 54

def integer_field( instance, options )
  input( :name => "#{instance.class.basename.snake_case}.#{options[:name]}", 
    :type => :text, :value => instance.send( options[:name] ) )
end

#properties(&block) ⇒ Object



31
32
33
34
35
# File 'lib/waves/helpers/form.rb', line 31

def properties(&block)
  fieldset do
    yield
  end
end

#property(instance, options) ⇒ Object



37
38
39
40
41
42
# File 'lib/waves/helpers/form.rb', line 37

def property( instance, options )
  div( :class => "property #{options[:type]} #{options[:size]}") {
    label options[:name].capitalize
    div( :class => 'control' ) { self.send( "#{options[:type]}_field", instance, options ) }
  }
end

#radio_button(label, name, value, on) ⇒ Object



70
71
72
73
74
75
# File 'lib/waves/helpers/form.rb', line 70

def radio_button( label, name, value, on )
  # can't use hoshi input method here thx 2 checked 
  span label
  raw( "<input type='radio' name='#{name}' \
    value='#{value}' #{'checked' if on}/>" )
end

#submit(label = "Save") ⇒ Object



81
82
83
# File 'lib/waves/helpers/form.rb', line 81

def submit( label = "Save")
  input :type => :submit, :value => label
end

#text_field(instance, options) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/waves/helpers/form.rb', line 44

def text_field( instance, options )
  if options[:size] == :large
    textarea( instance.send( options[:name] ), 
      :name => "#{instance.class.basename.snake_case}.#{options[:name]}" )
  else
    input( :name => "#{instance.class.basename.snake_case}.#{options[:name]}", 
      :type => :text, :value => instance.send( options[:name] ) )
  end
end