Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Includes:
ActiveModelFormBuilder
Defined in:
lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/active_model_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.



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/action_view/helpers/form_helper.rb', line 1118

def initialize(object_name, object, template, options, proc)
  @nested_child_index = {}
  @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
  @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
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



1108
1109
1110
# File 'lib/action_view/helpers/form_helper.rb', line 1108

def object
  @object
end

#object_nameObject

Returns the value of attribute object_name.



1108
1109
1110
# File 'lib/action_view/helpers/form_helper.rb', line 1108

def object_name
  @object_name
end

#optionsObject

Returns the value of attribute options.



1108
1109
1110
# File 'lib/action_view/helpers/form_helper.rb', line 1108

def options
  @options
end

Class Method Details

.model_nameObject



1110
1111
1112
# File 'lib/action_view/helpers/form_helper.rb', line 1110

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



1182
1183
1184
# File 'lib/action_view/helpers/form_helper.rb', line 1182

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



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

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



968
969
970
# File 'lib/action_view/helpers/date_helper.rb', line 968

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



976
977
978
# File 'lib/action_view/helpers/date_helper.rb', line 976

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)


1228
1229
1230
# File 'lib/action_view/helpers/form_helper.rb', line 1228

def emitted_hidden_id?
  @emitted_hidden_id
end

#fields_for(record_or_name_or_array, *args, &block) ⇒ Object



1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/action_view/helpers/form_helper.rb', line 1143

def fields_for(record_or_name_or_array, *args, &block)
  if options.has_key?(:index)
    index = "[#{options[:index]}]"
  elsif defined?(@auto_index)
    self.object_name = @object_name.to_s.sub(/\[\]$/,"")
    index = "[#{@auto_index}]"
  else
    index = ""
  end

  if options[:builder]
    args << {} unless args.last.is_a?(Hash)
    args.last[:builder] ||= options[:builder]
  end

  case record_or_name_or_array
  when String, Symbol
    if nested_attributes_association?(record_or_name_or_array)
      return fields_for_with_nested_attributes(record_or_name_or_array, args, block)
    else
      name = "#{object_name}#{index}[#{record_or_name_or_array}]"
    end
  when Array
    object = record_or_name_or_array.last
    name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]"
    args.unshift(object)
  else
    object = record_or_name_or_array
    name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]"
    args.unshift(object)
  end

  @template.fields_for(name, *args, &block)
end

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



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

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



1190
1191
1192
1193
# File 'lib/action_view/helpers/form_helper.rb', line 1190

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



1178
1179
1180
# File 'lib/action_view/helpers/form_helper.rb', line 1178

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



1186
1187
1188
# File 'lib/action_view/helpers/form_helper.rb', line 1186

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



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

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}"


1222
1223
1224
1225
1226
# File 'lib/action_view/helpers/form_helper.rb', line 1222

def submit(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
end

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



972
973
974
# File 'lib/action_view/helpers/date_helper.rb', line 972

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



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

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



1114
1115
1116
# File 'lib/action_view/helpers/form_helper.rb', line 1114

def to_model
  self
end