Class: Padrino::Helpers::FormBuilder::AbstractFormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb

Direct Known Subclasses

StandardFormBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, object, options = {}) ⇒ AbstractFormBuilder

Returns a new instance of AbstractFormBuilder.



7
8
9
10
11
12
13
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def initialize(template, object, options={})
  @template = template
  @object   = build_object(object)
  @options  = options
  raise "FormBuilder template must be initialized!" unless template
  raise "FormBuilder object must not be a 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/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 5

def object
  @object
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 5

def template
  @template
end

Instance Method Details

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

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



124
125
126
127
128
129
130
131
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 124

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

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

f.check_box_group :color, :options => [‘red’, ‘green’, ‘blue’], :selected => [‘red’, ‘blue’] f.check_box_group :color, :collection => @colors, :fields => [:name, :id]



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 100

def check_box_group(field, options={})
  selected_values = Array(options[:selected] || field_value(field))
  if options[:collection]
    fields = options[:fields] || [:name, :id]
    # don't use map!, it will break some orms
    selected_values = selected_values.map{ |v| (v.respond_to?(fields[0]) ? v.send(fields[1]) : v).to_s }
  end
  labeled_group( field, options ) do |variant|
    @template.check_box_tag( field_name(field)+'[]', :value => variant[1], :id => variant[2], :checked => selected_values.include?(variant[1]) )
  end
end

#csrf_token_fieldObject



172
173
174
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 172

def csrf_token_field
  @template.csrf_token_field
end

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



58
59
60
61
62
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 58

def email_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.email_field_tag field_name(field), options
end

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

f.error_message_on(field)



22
23
24
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 22

def error_message_on(field, options={})
  @template.error_message_on(object, field, options)
end

#error_messages(*params) ⇒ Object

f.error_messages



16
17
18
19
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 16

def error_messages(*params)
  params.unshift object
  @template.error_messages_for(*params)
end

#fields_for(child_association, instance_or_collection = nil, &block) ⇒ Object

Supports nested fields for a child model within a form f.fields_for :addresses f.fields_for :addresses, address f.fields_for :addresses, @addresses



161
162
163
164
165
166
167
168
169
170
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 161

def fields_for(child_association, instance_or_collection=nil, &block)
  default_collection = self.object.send(child_association)
  include_index = default_collection.respond_to?(:each)
  nested_options = { :parent => self, :association => child_association }
  nested_objects = instance_or_collection ? Array(instance_or_collection) : Array(default_collection)
  result = nested_objects.each_with_index.map do |child_instance, index|
    nested_options[:index] = include_index ? index : nil
    @template.fields_for(child_instance,  { :nested => nested_options }, &block)
  end.join("\n").html_safe
end

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

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



141
142
143
144
145
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 141

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

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

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



33
34
35
36
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 33

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.image_submit “buttons/submit.png”, :class => ‘large’



153
154
155
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 153

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

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

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



27
28
29
30
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 27

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

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



45
46
47
48
49
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 45

def number_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.number_field_tag field_name(field), options
end

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

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



84
85
86
87
88
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 84

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

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

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



134
135
136
137
138
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 134

def radio_button(field, options={})
  options.reverse_merge!(:id => field_id(field, options[:value]))
  options.reverse_merge!(:checked => true) if values_matches_field?(field, options[:value])
  @template.radio_button_tag field_name(field), options
end

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

f.radio_button_group :color, :options => [‘red’, ‘green’] f.radio_button_group :color, :collection => @colors, :fields => [:name, :id], :selected => @colors.first



114
115
116
117
118
119
120
121
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 114

def radio_button_group(field, options={})
  fields = options[:fields] || [:name, :id]
  selected_value = options[:selected] || field_value(field)
  selected_value = selected_value.send(fields[1])  if selected_value.respond_to?(fields[0])
  labeled_group( field, options ) do |variant|
    @template.radio_button_tag( field_name(field), :value => variant[1], :id => variant[2], :checked => variant[1] == selected_value.to_s )
  end
end

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



64
65
66
67
68
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 64

def search_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.search_field_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]



92
93
94
95
96
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 92

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

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

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



148
149
150
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 148

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

#telephone_field(field, options = {}) ⇒ Object Also known as: phone_field



51
52
53
54
55
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 51

def telephone_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.telephone_field_tag field_name(field), options
end

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

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



77
78
79
80
81
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 77

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

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

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



39
40
41
42
43
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 39

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

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



70
71
72
73
74
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 70

def url_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.url_field_tag field_name(field), options
end