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

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

Overview

Base class for Padrino Form Builder

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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def initialize(template, object, options={})
  @template = template
  fail "FormBuilder template must be initialized" unless template
  @object = object.kind_of?(Symbol) ? build_object(object) : object
  fail "FormBuilder object must be present. If there's no object, use a symbol instead (i.e. :user)" unless object
  @options = options
  @namespace = options[:namespace]
  @model_name = options[:as] || Inflections.underscore(@object.class).tr('/', '_')
  nested = options[:nested]
  if @is_nested = nested && (nested_parent = nested[:parent]) && nested_parent.respond_to?(:object)
    @parent_form = nested_parent
    @nested_index = nested[:index]
    @attributes_name = "#{nested[:association]}_attributes"
  end
end

Instance Attribute Details

#attributes_nameObject (readonly)

Returns the value of attribute attributes_name.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def attributes_name
  @attributes_name
end

#is_nestedObject (readonly)

Returns the value of attribute is_nested.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def is_nested
  @is_nested
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def model_name
  @model_name
end

#multipartObject

Returns the value of attribute multipart.



6
7
8
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6

def multipart
  @multipart
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def namespace
  @namespace
end

#nested_indexObject (readonly)

Returns the value of attribute nested_index.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def nested_index
  @nested_index
end

#objectObject

Returns the value of attribute object.



6
7
8
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6

def object
  @object
end

#parent_formObject (readonly)

Returns the value of attribute parent_form.



7
8
9
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7

def parent_form
  @parent_form
end

#templateObject

Returns the value of attribute template.



6
7
8
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6

def template
  @template
end

Instance Method Details

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



90
91
92
93
94
95
96
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 90

def check_box(field, options={})
  options = default_options(field, options, :value => '1')
  options[:checked] = true if is_checked?(field, options)
  name = field_name(field)
  html = @template.hidden_field_tag(name, :value => options.delete(:uncheck_value) || '0')
  html << @template.check_box_tag(name, options)
end

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



78
79
80
81
82
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 78

def check_box_group(field, options={})
  labeled_group(field, options) do |attributes|
    @template.check_box_tag(field_name(field)+'[]', attributes)
  end
end

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



142
143
144
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 142

def color_field(field, options={})
  @template.color_field_tag field_name(field), default_options(field, options)
end

#csrf_token_fieldObject



164
165
166
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 164

def csrf_token_field
  @template.csrf_token_field
end

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



126
127
128
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 126

def date_field(field, options={})
  @template.date_field_tag field_name(field), default_options(field, options)
end

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



118
119
120
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 118

def datetime_field(field, options={})
  @template.datetime_field_tag field_name(field), default_options(field, options)
end

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



122
123
124
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 122

def datetime_local_field(field, options={})
  @template.datetime_local_field_tag field_name(field), default_options(field, options)
end

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



54
55
56
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 54

def email_field(field, options={})
  @template.email_field_tag field_name(field), default_options(field, options)
end

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



29
30
31
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 29

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

#error_messages(*params) ⇒ Object



25
26
27
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 25

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

#fields_for(child_association, collection = nil, options = {}, &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 f.fields_for :addresses, address, index: i



152
153
154
155
156
157
158
159
160
161
162
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 152

def fields_for(child_association, collection=nil, options={}, &block)
  default_collection = self.object.send(child_association)
  collection ||= default_collection
  include_index = default_collection.respond_to?(:each)

  nested_options = { :parent => self, :association => child_association }
  Array(collection).each_with_index.inject(SafeBuffer.new) do |all,(child_instance,index)|
    nested_options[:index] = options[:index] || (include_index ? index : nil)
    all << @template.fields_for(child_instance,  { :nested => nested_options, :builder => self.class }, &block) << "\n"
  end
end

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



105
106
107
108
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 105

def file_field(field, options={})
  self.multipart = true
  @template.file_field_tag field_name(field), default_options(field, options).reject{ |key, _| key == :value }
end

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



37
38
39
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 37

def hidden_field(field, options={})
  @template.hidden_field_tag field_name(field), default_options(field, options)
end

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



114
115
116
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 114

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

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



33
34
35
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 33

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

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



130
131
132
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 130

def month_field(field, options={})
  @template.month_field_tag field_name(field), default_options(field, options)
end

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



45
46
47
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 45

def number_field(field, options={})
  @template.number_field_tag field_name(field), default_options(field, options)
end

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



70
71
72
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 70

def password_field(field, options={})
  @template.password_field_tag field_name(field), default_options(field, options)
end

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



98
99
100
101
102
103
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 98

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

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



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

def radio_button_group(field, options={})
  labeled_group(field, options) do |attributes|
    @template.radio_button_tag(field_name(field), attributes)
  end
end

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



58
59
60
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 58

def search_field(field, options={})
  @template.search_field_tag field_name(field), default_options(field, options)
end

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



74
75
76
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 74

def select(field, options={})
  @template.select_tag field_name(field), default_options(field, options)
end

#submit(*args) ⇒ Object



110
111
112
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 110

def submit(*args)
  @template.submit_tag(*args)
end

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



49
50
51
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 49

def telephone_field(field, options={})
  @template.telephone_field_tag field_name(field), default_options(field, options)
end

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



66
67
68
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 66

def text_area(field, options={})
  @template.text_area_tag field_name(field), default_options(field, options)
end

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



41
42
43
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 41

def text_field(field, options={})
  @template.text_field_tag field_name(field), default_options(field, options)
end

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



138
139
140
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 138

def time_field(field, options={})
  @template.time_field_tag field_name(field), default_options(field, options)
end

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



62
63
64
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 62

def url_field(field, options={})
  @template.url_field_tag field_name(field), default_options(field, options)
end

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



134
135
136
# File 'padrino-helpers/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 134

def week_field(field, options={})
  @template.week_field_tag field_name(field), default_options(field, options)
end