Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/form_options_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options, proc) ⇒ FormBuilder

Returns a new instance of FormBuilder.



1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
# File 'lib/action_view/helpers/form_helper.rb', line 1234

def initialize(object_name, object, template, options, proc)
  @nested_child_index = {}
  @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
  @parent_builder = options[:parent_builder]
  @default_options = @options ? @options.slice(:index) : {}
  if @object_name.to_s.match(/\[\]$/)
    if object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
      @auto_index = object.to_param
    else
      raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
    end
  end
  @multipart = nil
end

Instance Attribute Details

#multipartObject Also known as: multipart?

Returns the value of attribute multipart.



1218
1219
1220
# File 'lib/action_view/helpers/form_helper.rb', line 1218

def multipart
  @multipart
end

#objectObject

Returns the value of attribute object.



1216
1217
1218
# File 'lib/action_view/helpers/form_helper.rb', line 1216

def object
  @object
end

#object_nameObject

Returns the value of attribute object_name.



1216
1217
1218
# File 'lib/action_view/helpers/form_helper.rb', line 1216

def object_name
  @object_name
end

#optionsObject

Returns the value of attribute options.



1216
1217
1218
# File 'lib/action_view/helpers/form_helper.rb', line 1216

def options
  @options
end

#parent_builderObject (readonly)

Returns the value of attribute parent_builder.



1218
1219
1220
# File 'lib/action_view/helpers/form_helper.rb', line 1218

def parent_builder
  @parent_builder
end

Class Method Details

.model_nameObject



1226
1227
1228
# File 'lib/action_view/helpers/form_helper.rb', line 1226

def self.model_name
  @model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, ''))
end

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



1291
1292
1293
# File 'lib/action_view/helpers/form_helper.rb', line 1291

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



613
614
615
# File 'lib/action_view/helpers/form_options_helper.rb', line 613

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
end

#date_select(method, options = {}, html_options = {}) ⇒ Object



1026
1027
1028
# File 'lib/action_view/helpers/date_helper.rb', line 1026

def date_select(method, options = {}, html_options = {})
  @template.date_select(@object_name, method, objectify_options(options), html_options)
end

#datetime_select(method, options = {}, html_options = {}) ⇒ Object



1034
1035
1036
# File 'lib/action_view/helpers/date_helper.rb', line 1034

def datetime_select(method, options = {}, html_options = {})
  @template.datetime_select(@object_name, method, objectify_options(options), html_options)
end

#emitted_hidden_id?Boolean

Returns:

  • (Boolean)


1342
1343
1344
# File 'lib/action_view/helpers/form_helper.rb', line 1342

def emitted_hidden_id?
  @emitted_hidden_id ||= nil
end

#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/action_view/helpers/form_helper.rb', line 1261

def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
  fields_options[:builder] ||= options[:builder]
  fields_options[:parent_builder] = self

  case record_name
  when String, Symbol
    if nested_attributes_association?(record_name)
      return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
    end
  else
    record_object = record_name.is_a?(Array) ? record_name.last : record_name
    record_name   = ActiveModel::Naming.param_key(record_object)
  end

  index = if options.has_key?(:index)
    "[#{options[:index]}]"
  elsif defined?(@auto_index)
    self.object_name = @object_name.to_s.sub(/\[\]$/,"")
    "[#{@auto_index}]"
  end
  record_name = "#{object_name}#{index}[#{record_name}]"

  @template.fields_for(record_name, record_object, fields_options, &block)
end

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



1304
1305
1306
1307
# File 'lib/action_view/helpers/form_helper.rb', line 1304

def file_field(method, options = {})
  self.multipart = true
  @template.file_field(@object_name, method, objectify_options(options))
end

#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



617
618
619
# File 'lib/action_view/helpers/form_options_helper.rb', line 617

def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  @template.grouped_collection_select(@object_name, method, collection, group_method, group_label_method, option_key_method, option_value_method, objectify_options(options), @default_options.merge(html_options))
end

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



1299
1300
1301
1302
# File 'lib/action_view/helpers/form_helper.rb', line 1299

def hidden_field(method, options = {})
  @emitted_hidden_id = true if method == :id
  @template.hidden_field(@object_name, method, objectify_options(options))
end

#label(method, text = nil, options = {}, &block) ⇒ Object



1287
1288
1289
# File 'lib/action_view/helpers/form_helper.rb', line 1287

def label(method, text = nil, options = {}, &block)
  @template.label(@object_name, method, text, objectify_options(options), &block)
end

#radio_button(method, tag_value, options = {}) ⇒ Object



1295
1296
1297
# File 'lib/action_view/helpers/form_helper.rb', line 1295

def radio_button(method, tag_value, options = {})
  @template.radio_button(@object_name, method, tag_value, objectify_options(options))
end

#select(method, choices, options = {}, html_options = {}) ⇒ Object



609
610
611
# File 'lib/action_view/helpers/form_options_helper.rb', line 609

def select(method, choices, options = {}, html_options = {})
  @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
end

#submit(value = nil, options = {}) ⇒ Object

Add the submit button for the given form. When no value is given, it checks if the object is a new resource or not to create the proper label:

<%= form_for @post do |f| %>
  <%= f.submit %>
<% end %>

In the example above, if @post is a new record, it will use “Create Post” as submit button label, otherwise, it uses “Update Post”.

Those labels can be customized using I18n, under the helpers.submit key and accept the %model as translation interpolation:

en:
  helpers:
    submit:
      create: "Create a %{model}"
      update: "Confirm changes to %{model}"

It also searches for a key specific for the given object:

en:
  helpers:
    submit:
      post:
        create: "Add %{model}"


1336
1337
1338
1339
1340
# File 'lib/action_view/helpers/form_helper.rb', line 1336

def submit(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  @template.submit_tag(value, options)
end

#time_select(method, options = {}, html_options = {}) ⇒ Object



1030
1031
1032
# File 'lib/action_view/helpers/date_helper.rb', line 1030

def time_select(method, options = {}, html_options = {})
  @template.time_select(@object_name, method, objectify_options(options), html_options)
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



621
622
623
# File 'lib/action_view/helpers/form_options_helper.rb', line 621

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  @template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options))
end

#to_modelObject



1230
1231
1232
# File 'lib/action_view/helpers/form_helper.rb', line 1230

def to_model
  self
end