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
- #boolean_field(instance, options) ⇒ Object
- #cancel(label = "Cancel") ⇒ Object
- #editor(instance, options = {}) ⇒ Object
- #file_field(instance, options) ⇒ Object
- #float_field(instance, options) ⇒ Object
- #integer_field(instance, options) ⇒ Object
- #properties(&block) ⇒ Object
- #property(instance, options) ⇒ Object
- #radio_button(label, name, value, on) ⇒ Object
- #submit(label = "Save") ⇒ Object
- #text_field(instance, options) ⇒ Object
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, ) on = instance.send( [:name] ) ( 'Yes', "#{instance.class.basename.snake_case}.#{[:name]}", 't', on ) ( 'No', "#{instance.class.basename.snake_case}.#{[: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, = {}) = { :method => :put, :buttons => true, :heading => instance.class.name.in_words.title_case }.merge( ) h1 [:heading] if [:heading] form( :method => :post, :action => paths.put( instance.key ) ) { input( :type => :hidden, :name => :_method, :value => :put ) if [:method] == :put div.properties { yield } p( :class => 'button panel' ) { submit; cancel } if [:buttons] } end |
#file_field(instance, options) ⇒ Object
77 78 79 |
# File 'lib/waves/helpers/form.rb', line 77 def file_field( instance, ) input( :type => :file, :name => "#{instance.class.basename.snake_case}.#{[:name]}" ) end |
#float_field(instance, options) ⇒ Object
59 60 61 62 |
# File 'lib/waves/helpers/form.rb', line 59 def float_field( instance, ) input( :name => "#{instance.class.basename.snake_case}.#{[:name]}", :type => :text, :value => instance.send( [:name] ) ) end |
#integer_field(instance, options) ⇒ Object
54 55 56 57 |
# File 'lib/waves/helpers/form.rb', line 54 def integer_field( instance, ) input( :name => "#{instance.class.basename.snake_case}.#{[:name]}", :type => :text, :value => instance.send( [: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, ) div( :class => "property #{[:type]} #{[:size]}") { label [:name].capitalize div( :class => 'control' ) { self.send( "#{[:type]}_field", instance, ) } } end |
#radio_button(label, name, value, on) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/waves/helpers/form.rb', line 70 def ( 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, ) if [:size] == :large textarea( instance.send( [:name] ), :name => "#{instance.class.basename.snake_case}.#{[:name]}" ) else input( :name => "#{instance.class.basename.snake_case}.#{[:name]}", :type => :text, :value => instance.send( [:name] ) ) end end |