Class: AnyView::Helpers::FormBuilder::AbstractFormBuilder

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

Direct Known Subclasses

StandardFormBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, object) ⇒ AbstractFormBuilder

Returns a new instance of AbstractFormBuilder.


7
8
9
10
11
12
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 7

def initialize(view_context, object)
  @view_context = view_context
  @object   = object
  raise "FormBuilder view_context must be initialized!" unless view_context
  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


5
6
7
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 5

def object
  @object
end

#view_contextObject

Returns the value of attribute view_context


5
6
7
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 5

def view_context
  @view_context
end

Instance Method Details

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

f.check_box :remember_me, :value => 'true', :uncheck_value => '0'


57
58
59
60
61
62
63
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 57

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 = @view_context.check_box_tag field_name(field), options
  html << hidden_field(field, :value => unchecked_value, :id => nil)
end

#error_messages(options = {}) ⇒ Object

f.error_messages


15
16
17
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 15

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

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

f.file_field :photo, :class => 'avatar'


73
74
75
76
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 73

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

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

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


26
27
28
29
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 26

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

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

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


84
85
86
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 84

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

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

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


20
21
22
23
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 20

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

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

f.password_field :password, :id => 'password'


44
45
46
47
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 44

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

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

f.radio_button :gender, :value => 'male'


66
67
68
69
70
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 66

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])
  @view_context.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]


51
52
53
54
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 51

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

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

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


79
80
81
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 79

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

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

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


38
39
40
41
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 38

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

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

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


32
33
34
35
# File 'lib/any_view/form_builder/abstract_form_builder.rb', line 32

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