# File lib/action_view/helpers/form_helper.rb, line 1163 def check_box_checked?(value, checked_value) case value when TrueClass, FalseClass value when NilClass false when Integer value != 0 when String value == checked_value when Array value.include?(checked_value) else value.to_i != 0 end end
# File lib/action_view/helpers/form_helper.rb, line 980 def initialize(object_name, method_name, template_object, object = nil) @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup @template_object = template_object @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]") @object = retrieve_object(object) @auto_index = retrieve_autoindex(Regexp.last_match.pre_match) if Regexp.last_match end
# File lib/action_view/helpers/form_helper.rb, line 1151 def value(object, method_name) object.send method_name if object end
# File lib/action_view/helpers/form_helper.rb, line 1155 def value_before_type_cast(object, method_name) unless object.nil? object.respond_to?(method_name + "_before_type_cast") ? object.send(method_name + "_before_type_cast") : object.send(method_name) end end
# File lib/action_view/helpers/form_helper.rb, line 1133 def retrieve_autoindex(pre_match) object = self.object || @template_object.instance_variable_get("@#{pre_match}") if object && object.respond_to?(:to_param) 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
# File lib/action_view/helpers/form_helper.rb, line 1122 def retrieve_object(object) if object object elsif @template_object.instance_variable_defined?("@#{@object_name}") @template_object.instance_variable_get("@#{@object_name}") end rescue NameError # As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil. nil end
# File lib/action_view/helpers/form_helper.rb, line 1105 def to_boolean_select_tag(options = {}) options = options.stringify_keys add_default_name_and_id(options) value = value(object) tag_text = "<select" tag_text << tag_options(options) tag_text << "><option value=\"false\"" tag_text << " selected" if value == false tag_text << ">False</option><option value=\"true\"" tag_text << " selected" if value tag_text << ">True</option></select>" end
# File lib/action_view/helpers/form_helper.rb, line 1083 def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0") options = options.stringify_keys options["type"] = "checkbox" options["value"] = checked_value if options.has_key?("checked") cv = options.delete "checked" checked = cv == true || cv == "checked" else checked = self.class.check_box_checked?(value(object), checked_value) end options["checked"] = "checked" if checked if options["multiple"] add_default_name_and_id_for_value(checked_value, options) options.delete("multiple") else add_default_name_and_id(options) end hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : "" checkbox = tag("input", options) (hidden + checkbox).html_safe end
# File lib/action_view/helpers/form_helper.rb, line 1118 def to_content_tag(tag_name, options = {}) content_tag(tag_name, value(object), options) end
# File lib/action_view/helpers/form_helper.rb, line 1033 def to_input_field_tag(field_type, options = {}) options = options.stringify_keys options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") options = DEFAULT_FIELD_OPTIONS.merge(options) if field_type == "hidden" options.delete("size") end options["type"] ||= field_type options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file" options["value"] &&= ERB::Util.html_escape(options["value"]) add_default_name_and_id(options) tag("input", options) end
# File lib/action_view/helpers/form_helper.rb, line 989 def to_label_tag(text = nil, options = {}, &block) options = options.stringify_keys tag_value = options.delete("value") name_and_id = options.dup if name_and_id["for"] name_and_id["id"] = name_and_id["for"] else name_and_id.delete("id") end add_default_name_and_id_for_value(tag_value, name_and_id) options.delete("index") options.delete("namespace") options["for"] ||= name_and_id["id"] if block_given? @template_object.label_tag(name_and_id["id"], options, &block) else content = if text.blank? object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') method_and_value = tag_value.present? ? "#{method_name}.#{tag_value}" : method_name if object.respond_to?(:to_model) key = object.class.model_name.i18n_key i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] end i18n_default ||= "" I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => "helpers.label").presence else text.to_s end content ||= if object && object.class.respond_to?(:human_attribute_name) object.class.human_attribute_name(method_name) end content ||= method_name.humanize label_tag(name_and_id["id"], content, options) end end
# File lib/action_view/helpers/form_helper.rb, line 1047 def to_number_field_tag(field_type, options = {}) options = options.stringify_keys options['size'] ||= nil if range = options.delete("in") || options.delete("within") options.update("min" => range.min, "max" => range.max) end to_input_field_tag(field_type, options) end
# File lib/action_view/helpers/form_helper.rb, line 1072 def to_text_area_tag(options = {}) options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys) add_default_name_and_id(options) if size = options.delete("size") options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split) end content_tag("textarea", options.delete('value') || value_before_type_cast(object), options) end
# File lib/action_view/helpers/form_helper.rb, line 1142 def value(object) self.class.value(object, @method_name) end
# File lib/action_view/helpers/form_helper.rb, line 1146 def value_before_type_cast(object) self.class.value_before_type_cast(object, @method_name) end
# File lib/action_view/helpers/form_helper.rb, line 1197 def add_default_name_and_id(options) if options.has_key?("index") options["name"] ||= tag_name_with_index(options["index"]) options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } options.delete("index") elsif defined?(@auto_index) options["name"] ||= tag_name_with_index(@auto_index) options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } else options["name"] ||= tag_name options["id"] = options.fetch("id"){ tag_id } end options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]") options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence end
# File lib/action_view/helpers/form_helper.rb, line 1186 def add_default_name_and_id_for_value(tag_value, options) unless tag_value.nil? pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase specified_id = options["id"] add_default_name_and_id(options) options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present? else add_default_name_and_id(options) end end
# File lib/action_view/helpers/form_helper.rb, line 1234 def sanitized_method_name @sanitized_method_name ||= @method_name.sub(/\?$/,"") end
# File lib/action_view/helpers/form_helper.rb, line 1230 def sanitized_object_name @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") end
# File lib/action_view/helpers/form_helper.rb, line 1222 def tag_id "#{sanitized_object_name}_#{sanitized_method_name}" end
# File lib/action_view/helpers/form_helper.rb, line 1226 def tag_id_with_index(index) "#{sanitized_object_name}_#{index}_#{sanitized_method_name}" end
# File lib/action_view/helpers/form_helper.rb, line 1214 def tag_name "#{@object_name}[#{sanitized_method_name}]" end
# File lib/action_view/helpers/form_helper.rb, line 1218 def tag_name_with_index(index) "#{@object_name}[#{index}][#{sanitized_method_name}]" end