Class: AbstractFormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb

Direct Known Subclasses

StandardFormBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, object) ⇒ AbstractFormBuilder

Returns a new instance of AbstractFormBuilder.



4
5
6
7
8
9
# File 'lib/sinatra_more/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

#objectObject

Returns the value of attribute object.



2
3
4
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 2

def object
  @object
end

#templateObject

Returns the value of attribute template.



2
3
4
# File 'lib/sinatra_more/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_more/markup_plugin/form_builder/abstract_form_builder.rb', line 54

def check_box(field, options={})
  unchecked_value = options.delete(:uncheck_value) || '0'
  options.reverse_merge!(:id => field_id(field), :value => '1')
  options.merge!(:checked => true) if values_matches_field?(field, options[:value])
  html = hidden_field(field, :value => unchecked_value, :id => nil)
  html << @template.check_box_tag(field_name(field), options)
end

#error_messages(options = {}) ⇒ Object

f.error_messages



12
13
14
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 12

def error_messages(options={})
  @template.error_messages_for(@object, options)
end

#file_field(field, options = {}) ⇒ Object

f.file_field :photo, :class => ‘avatar’



70
71
72
73
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 70

def file_field(field, options={})
  options.reverse_merge!(:id => field_id(field))
  @template.file_field_tag field_name(field), options
end

#hidden_field(field, options = {}) ⇒ Object

f.hidden_field :session_id, :value => “45”



23
24
25
26
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 23

def hidden_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.hidden_field_tag field_name(field), options
end

#image_submit(source, options = {}) ⇒ Object

f.simage_submitubmit “buttons/submit.png”, :class => ‘large’



81
82
83
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 81

def image_submit(source, options={})
  @template.image_submit_tag source, options
end

#label(field, options = {}) ⇒ Object

f.label :username, :caption => “Nickname”



17
18
19
20
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 17

def label(field, options={})
  options.reverse_merge!(:caption => "#{field.to_s.titleize}: ")
  @template.label_tag(field_id(field), options)
end

#password_field(field, options = {}) ⇒ Object

f.password_field :password, :id => ‘password’



41
42
43
44
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 41

def password_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.password_field_tag field_name(field), options
end

#radio_button(field, options = {}) ⇒ Object

f.radio_button :gender, :value => ‘male’



63
64
65
66
67
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 63

def radio_button(field, options={})
  options.reverse_merge!(:id => field_id(field, options[:value]))
  options.merge!(:checked => true) if values_matches_field?(field, options[:value])
  @template.radio_button_tag field_name(field), options
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_more/markup_plugin/form_builder/abstract_form_builder.rb', line 48

def select(field, options={})
  options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
  @template.select_tag field_name(field), options
end

#submit(caption = "Submit", options = {}) ⇒ Object

f.submit “Update”, :class => ‘large’



76
77
78
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 76

def submit(caption="Submit", options={})
  @template.submit_tag caption, options
end

#text_area(field, options = {}) ⇒ Object

f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’



35
36
37
38
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 35

def text_area(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.text_area_tag field_name(field), options
end

#text_field(field, options = {}) ⇒ Object

f.text_field :username, :value => “(blank)”, :id => ‘username’



29
30
31
32
# File 'lib/sinatra_more/markup_plugin/form_builder/abstract_form_builder.rb', line 29

def text_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.text_field_tag field_name(field), options
end