Class: AbstractFormBuilder
- Inherits:
-
Object
- Object
- AbstractFormBuilder
- Defined in:
- lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
-
#check_box(field, options = {}) ⇒ Object
f.check_box :remember_me, :value => ‘true’, :uncheck_value => ‘0’.
-
#error_messages(options = {}) ⇒ Object
f.error_messages.
-
#file_field(field, options = {}) ⇒ Object
f.file_field :photo, :class => ‘avatar’.
-
#hidden_field(field, options = {}) ⇒ Object
f.hidden_field :session_id, :value => “45”.
-
#image_submit(source, options = {}) ⇒ Object
f.simage_submitubmit “buttons/submit.png”, :class => ‘large’.
-
#initialize(template, object) ⇒ AbstractFormBuilder
constructor
A new instance of AbstractFormBuilder.
-
#label(field, options = {}) ⇒ Object
f.label :username, :caption => “Nickname”.
-
#password_field(field, options = {}) ⇒ Object
f.password_field :password, :id => ‘password’.
-
#radio_button(field, options = {}) ⇒ Object
f.radio_button :gender, :value => ‘male’.
-
#select(field, options = {}) ⇒ Object
f.select :color, :options => [‘red’, ‘green’], :include_blank => true f.select :color, :collection => @colors, :fields => [:name, :id].
-
#submit(caption = "Submit", options = {}) ⇒ Object
f.submit “Update”, :class => ‘large’.
-
#text_area(field, options = {}) ⇒ Object
f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’.
-
#text_field(field, options = {}) ⇒ Object
f.text_field :username, :value => “(blank)”, :id => ‘username’.
Constructor Details
#initialize(template, object) ⇒ AbstractFormBuilder
Returns a new instance of AbstractFormBuilder.
4 5 6 7 8 9 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 4 def initialize(template, object) @template = template @object = build_object(object) raise "FormBuilder template must be initialized!" unless template raise "FormBuilder object must be not be nil value. If there's no object, use a symbol instead! (i.e :user)" unless object end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
2 3 4 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 2 def object @object end |
#template ⇒ Object
Returns the value of attribute template.
2 3 4 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 2 def template @template end |
Instance Method Details
#check_box(field, options = {}) ⇒ Object
f.check_box :remember_me, :value => ‘true’, :uncheck_value => ‘0’
54 55 56 57 58 59 60 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 54 def check_box(field, ={}) unchecked_value = .delete(:uncheck_value) || '0' .reverse_merge!(:id => field_id(field), :value => '1') .merge!(:checked => true) if values_matches_field?(field, [:value]) html = hidden_field(field, :value => unchecked_value, :id => nil) html << @template.check_box_tag(field_name(field), ) end |
#error_messages(options = {}) ⇒ Object
f.error_messages
12 13 14 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 12 def (={}) @template.(@object, ) end |
#file_field(field, options = {}) ⇒ Object
f.file_field :photo, :class => ‘avatar’
70 71 72 73 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 70 def file_field(field, ={}) .reverse_merge!(:id => field_id(field)) @template.file_field_tag field_name(field), end |
#hidden_field(field, options = {}) ⇒ Object
f.hidden_field :session_id, :value => “45”
23 24 25 26 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 23 def hidden_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) @template.hidden_field_tag field_name(field), end |
#image_submit(source, options = {}) ⇒ Object
f.simage_submitubmit “buttons/submit.png”, :class => ‘large’
81 82 83 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 81 def image_submit(source, ={}) @template.image_submit_tag source, end |
#label(field, options = {}) ⇒ Object
f.label :username, :caption => “Nickname”
17 18 19 20 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 17 def label(field, ={}) .reverse_merge!(:caption => "#{field.to_s.titleize}: ") @template.label_tag(field_id(field), ) end |
#password_field(field, options = {}) ⇒ Object
f.password_field :password, :id => ‘password’
41 42 43 44 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 41 def password_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) @template.password_field_tag field_name(field), end |
#radio_button(field, options = {}) ⇒ Object
f.radio_button :gender, :value => ‘male’
63 64 65 66 67 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 63 def (field, ={}) .reverse_merge!(:id => field_id(field, [:value])) .merge!(:checked => true) if values_matches_field?(field, [:value]) @template. field_name(field), end |
#select(field, options = {}) ⇒ Object
f.select :color, :options => [‘red’, ‘green’], :include_blank => true f.select :color, :collection => @colors, :fields => [:name, :id]
48 49 50 51 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 48 def select(field, ={}) .reverse_merge!(:id => field_id(field), :selected => field_value(field)) @template.select_tag field_name(field), end |
#submit(caption = "Submit", options = {}) ⇒ Object
f.submit “Update”, :class => ‘large’
76 77 78 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 76 def submit(="Submit", ={}) @template.submit_tag , end |
#text_area(field, options = {}) ⇒ Object
f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’
35 36 37 38 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 35 def text_area(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) @template.text_area_tag field_name(field), end |
#text_field(field, options = {}) ⇒ Object
f.text_field :username, :value => “(blank)”, :id => ‘username’
29 30 31 32 |
# File 'lib/sinatra/markup_plugin/form_builder/abstract_form_builder.rb', line 29 def text_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) @template.text_field_tag field_name(field), end |