class ActionView::Helpers::FormBuilder
def self.model_name
def self.model_name @model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, '')) end
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
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
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
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
def date_select(method, options = {}, html_options = {})
def date_select(method, options = {}, html_options = {}) @template.date_select(@object_name, method, objectify_options(options), html_options) end
def datetime_select(method, options = {}, html_options = {})
def datetime_select(method, options = {}, html_options = {}) @template.datetime_select(@object_name, method, objectify_options(options), html_options) end
def emitted_hidden_id?
def emitted_hidden_id? @emitted_hidden_id end
def fields_for(record_or_name_or_array, *args, &block)
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}[#{ActionController::RecordIdentifier.singular_class_name(object)}]" args.unshift(object) else object = record_or_name_or_array name = "#{object_name}#{index}[#{ActionController::RecordIdentifier.singular_class_name(object)}]" args.unshift(object) end @template.fields_for(name, *args, &block) end
def fields_for_nested_model(name, object, options, block)
def fields_for_nested_model(name, object, options, block) object = object.to_model if object.respond_to?(:to_model) if object.persisted? @template.fields_for(name, object, options) do |builder| block.call(builder) @template.concat builder.hidden_field(:id) unless builder.emitted_hidden_id? end else @template.fields_for(name, object, options, &block) end end
def fields_for_with_nested_attributes(association_name, args, block)
def fields_for_with_nested_attributes(association_name, args, block) name = "#{object_name}[#{association_name}_attributes]" options = args.extract_options! association = args.shift association = association.to_model if association.respond_to?(:to_model) if association.respond_to?(:persisted?) association = [association] if @object.send(association_name).is_a?(Array) elsif !association.is_a?(Array) association = @object.send(association_name) end if association.is_a?(Array) explicit_child_index = options[:child_index] output = ActiveSupport::SafeBuffer.new association.each do |child| output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block) end output elsif association fields_for_nested_model(name, association, options, block) end end
def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
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
def hidden_field(method, options = {})
def hidden_field(method, options = {}) @emitted_hidden_id = true if method == :id @template.hidden_field(@object_name, method, objectify_options(options)) end
def initialize(object_name, object, template, options, proc)
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
def label(method, text = nil, options = {})
def label(method, text = nil, options = {}) @template.label(@object_name, method, text, objectify_options(options)) end
def nested_attributes_association?(association_name)
def nested_attributes_association?(association_name) @object.respond_to?("#{association_name}_attributes=") end
def nested_child_index(name)
def nested_child_index(name) @nested_child_index[name] ||= -1 @nested_child_index[name] += 1 end
def objectify_options(options)
def objectify_options(options) @default_options.merge(options.merge(:object => @object)) end
def radio_button(method, tag_value, options = {})
def radio_button(method, tag_value, options = {}) @template.radio_button(@object_name, method, tag_value, objectify_options(options)) end
def select(method, choices, options = {}, html_options = {})
def select(method, choices, options = {}, html_options = {}) @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options)) end
def submit(value=nil, options={})
create: "Add {{model}}"
post:
submit:
helpers:
en:
It also searches for a key specific for the given object:
update: "Confirm changes to {{model}}"
create: "Create a {{model}}"
submit:
helpers:
en:
the {{model}} as translation interpolation:
Those labels can be customized using I18n, under the helpers.submit key and accept
submit button label, otherwise, it uses "Update Post".
In the example above, if @post is a new record, it will use "Create Post" as
<% end %>
<%= f.submit %>
<%= form_for @post do |f| %>
if the object is a new resource or not to create the proper label:
Add the submit button for the given form. When no value is given, it checks
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
def submit_default_value
def submit_default_value object = @object.respond_to?(:to_model) ? @object.to_model : @object key = object ? (object.persisted? ? :update : :create) : :submit model = if object.class.respond_to?(:model_name) object.class.model_name.human else @object_name.to_s.humanize end defaults = [] defaults << :"helpers.submit.#{object_name}.#{key}" defaults << :"helpers.submit.#{key}" defaults << "#{key.to_s.humanize} #{model}" I18n.t(defaults.shift, :model => model, :default => defaults) end
def time_select(method, options = {}, html_options = {})
def time_select(method, options = {}, html_options = {}) @template.time_select(@object_name, method, objectify_options(options), html_options) end
def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
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
def to_model
def to_model self end