Permalink
Browse files

Fixed that form helpers would treat string and symbol keys differentl…

…y in html_options (and possibly create duplicate entries) #112 [bitsweat]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@833 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent db41d2d commit dfac1cea3d851000116a23ab14c2b1ae981f7a12 @dhh dhh committed Mar 6, 2005
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that form helpers would treat string and symbol keys differently in html_options (and possibly create duplicate entries) #112 [bitsweat]
+
* Fixed that broken pipe errors (clients disconnecting in mid-request) could bring down a fcgi process
* Added the original exception message to session recall errors (so you can see which class wasnt required)
@@ -15,7 +15,7 @@ module Helpers
module ActiveRecordHelper
# Returns a default input tag for the type of object returned by the method. Example
# (title is a VARCHAR column and holds "Hello World"):
- # input("post", "title") =>
+ # input("post", "title") =>
# <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
def input(record_name, method)
InstanceTag.new(record_name, method, self).to_tag
@@ -41,7 +41,7 @@ def input(record_name, method)
# It's possible to specialize the form builder by using a different action name and by supplying another
# block renderer. Example (entry is a new record that has a message attribute using VARCHAR):
#
- # form("entry", :action => "sign", :input_block =>
+ # form("entry", :action => "sign", :input_block =>
# Proc.new { |record, column| "#{column.human_name}: #{input(record, column.name)}<br />" }) =>
#
# <form action='/post/sign' method='post'>
@@ -56,24 +56,25 @@ def input(record_name, method)
# form << content_tag("b", "Department")
# form << collection_select("department", "id", @departments, "id", "name")
# end
- def form(record_name, options = {})
- record = instance_eval("@#{record_name}")
+ def form(record_name, options = nil)
+ options = (options || {}).symbolize_keys
+ record = instance_eval("@#{record_name}")
options[:action] ||= record.new_record? ? "create" : "update"
- action = url_for(:action => options[:action])
+ action = url_for(:action => options[:action])
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
-
+
id_field = record.new_record? ? "" : InstanceTag.new(record_name, "id", self).to_input_field_tag("hidden")
-
+
formtag = %(<form action="#{action}" method="post">#{id_field}) + all_input_tags(record, record_name, options)
yield formtag if block_given?
formtag + %(<input type="submit" value="#{submit_value}" /></form>)
end
# Returns a string containing the error message attached to the +method+ on the +object+, if one exists.
# This error message is wrapped in a DIV tag, which can be specialized to include both a +prepend_text+ and +append_text+
- # to properly introduce the error and a +css_class+ to style it accordingly. Examples (post has an error message
+ # to properly introduce the error and a +css_class+ to style it accordingly. Examples (post has an error message
# "can't be empty" on the title attribute):
#
# <%= error_message_on "post", "title" %> =>
@@ -86,19 +87,20 @@ def error_message_on(object, method, prepend_text = "", append_text = "", css_cl
"<div class=\"#{css_class}\">#{prepend_text + (errors.is_a?(Array) ? errors.first : errors) + append_text}</div>"
end
end
-
+
# Returns a string with a div containing all the error messages for the object located as an instance variable by the name
# of <tt>object_name</tt>. This div can be tailored by the following options:
#
# * <tt>header_tag</tt> - Used for the header of the error div (default: h2)
# * <tt>id</tt> - The id of the error div (default: errorExplanation)
# * <tt>class</tt> - The class of the error div (default: errorExplanation)
- def error_messages_for(object_name, options={})
+ def error_messages_for(object_name, options = {})
+ options = options.symbolize_keys
object = instance_eval "@#{object_name}"
unless object.errors.empty?
content_tag("div",
content_tag(
- options[:header_tag] || "h2",
+ options[:header_tag] || "h2",
"#{pluralize(object.errors.count, "error")} prohibited this #{object_name.gsub("_", " ")} from being saved"
) +
content_tag("p", "There were problems with the following fields:") +
@@ -107,7 +109,7 @@ def error_messages_for(object_name, options={})
)
end
end
-
+
private
def all_input_tags(record, record_name, options)
input_block = options[:input_block] || default_input_block
@@ -116,7 +118,7 @@ def all_input_tags(record, record_name, options)
def default_input_block
Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br />#{input(record, column.name)}</p>" }
- end
+ end
end
class InstanceTag #:nodoc:
@@ -10,15 +10,15 @@ module AssetTagHelper
# either be <tt>:rss</tt> (default) or <tt>:atom</tt> and the +options+ follow the url_for style of declaring a link target.
#
# Examples:
- # auto_discovery_link_tag # =>
+ # auto_discovery_link_tag # =>
# <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/controller/action" />
- # auto_discovery_link_tag(:atom) # =>
+ # auto_discovery_link_tag(:atom) # =>
# <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/action" />
- # auto_discovery_link_tag(:rss, :action => "feed") # =>
+ # auto_discovery_link_tag(:rss, :action => "feed") # =>
# <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/feed" />
def auto_discovery_link_tag(type = :rss, options = {})
tag(
- "link", "rel" => "alternate", "type" => "application/#{type}+xml", "title" => type.to_s.upcase,
+ "link", "rel" => "alternate", "type" => "application/#{type}+xml", "title" => type.to_s.upcase,
"href" => url_for(options.merge(:only_path => false))
)
end
@@ -38,7 +38,7 @@ def javascript_include_tag(*sources)
content_tag("script", "", "language" => "JavaScript", "type" => "text/javascript", "src" => source)
}.join("\n")
end
-
+
# Returns a css link tag per source given as argument. Examples:
#
# stylesheet_link_tag "style" # =>
@@ -48,7 +48,7 @@ def javascript_include_tag(*sources)
# <link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />
# <link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />
def stylesheet_link_tag(*sources)
- sources.collect { |source|
+ sources.collect { |source|
source = "/stylesheets/#{source}" unless source.include?("/")
source = "#{source}.css" unless source.include?(".")
tag("link", "rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "href" => source)
@@ -5,19 +5,19 @@ module Helpers
# The Date Helper primarily creates select/option tags for different kinds of dates and date elements. All of the select-type methods
# share a number of common options that are as follows:
#
- # * <tt>:prefix</tt> - overwrites the default prefix of "date" used for the select names. So specifying "birthday" would give
+ # * <tt>:prefix</tt> - overwrites the default prefix of "date" used for the select names. So specifying "birthday" would give
# birthday[month] instead of date[month] if passed to the select_month method.
# * <tt>:include_blank</tt> - set to true if it should be possible to set an empty date.
# * <tt>:discard_type</tt> - set to true if you want to discard the type part of the select name. If set to true, the select_month
# method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead of "date[month]".
module DateHelper
DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX")
- # Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return
+ # Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return
# "about 1 hour". See the source for the complete wording list.
def distance_of_time_in_words(from_time, to_time)
distance_in_minutes = ((to_time - from_time) / 60).round
-
+
case distance_in_minutes
when 0 then "less than a minute"
when 1 then "1 minute"
@@ -28,7 +28,7 @@ def distance_of_time_in_words(from_time, to_time)
else "#{(distance_in_minutes / 1440).round} days"
end
end
-
+
# Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
def distance_of_time_in_words_to_now(from_time)
distance_of_time_in_words(from_time, Time.now)
@@ -48,7 +48,7 @@ def distance_of_time_in_words_to_now(from_time)
#
# date_select("post", "written_on")
# date_select("post", "written_on", :start_year => 1995)
- # date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
+ # date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
# :discard_day => true, :include_blank => true)
# date_select("post", "written_on", :order => [:day, :month, :year])
# date_select("user", "birthday", :order => [:month, :day])
@@ -107,7 +107,7 @@ def select_minute(datetime, options = {})
0.upto(59) do |minute|
minute_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.min) == minute ?
- "<option selected=\"selected\">#{leading_zero_on_single_digits(minute)}</option>\n" :
+ "<option selected=\"selected\">#{leading_zero_on_single_digits(minute)}</option>\n" :
"<option>#{leading_zero_on_single_digits(minute)}</option>\n"
)
end
@@ -122,7 +122,7 @@ def select_hour(datetime, options = {})
0.upto(23) do |hour|
hour_options << ((datetime.kind_of?(Fixnum) ? datetime : datetime.hour) == hour ?
- "<option selected=\"selected\">#{leading_zero_on_single_digits(hour)}</option>\n" :
+ "<option selected=\"selected\">#{leading_zero_on_single_digits(hour)}</option>\n" :
"<option>#{leading_zero_on_single_digits(hour)}</option>\n"
)
end
@@ -137,18 +137,18 @@ def select_day(date, options = {})
1.upto(31) do |day|
day_options << ((date.kind_of?(Fixnum) ? date : date.day) == day ?
- "<option selected=\"selected\">#{day}</option>\n" :
+ "<option selected=\"selected\">#{day}</option>\n" :
"<option>#{day}</option>\n"
)
end
select_html("day", day_options, options[:prefix], options[:include_blank], options[:discard_type])
end
-
+
# Returns a select tag with options for each of the months January through December with the current month selected.
# The month names are presented as keys (what's shown to the user) and the month numbers (1-12) are used as values
# (what's submitted to the server). It's also possible to use month numbers for the presentation instead of names --
- # set the <tt>:use_month_numbers</tt> key in +options+ to true for this to happen. If you want both numbers and names,
+ # set the <tt>:use_month_numbers</tt> key in +options+ to true for this to happen. If you want both numbers and names,
# set the <tt>:add_month_numbers</tt> key in +options+ to true. Examples:
#
# select_month(Date.today) # Will use keys like "January", "March"
@@ -158,7 +158,7 @@ def select_month(date, options = {})
month_options = []
1.upto(12) do |month_number|
- month_name = if options[:use_month_numbers]
+ month_name = if options[:use_month_numbers]
month_number
elsif options[:add_month_numbers]
month_number.to_s + " - " + Date::MONTHNAMES[month_number]
@@ -167,16 +167,16 @@ def select_month(date, options = {})
end
month_options << ((date.kind_of?(Fixnum) ? date : date.month) == month_number ?
- %(<option value="#{month_number}" selected="selected">#{month_name}</option>\n) :
+ %(<option value="#{month_number}" selected="selected">#{month_name}</option>\n) :
%(<option value="#{month_number}">#{month_name}</option>\n)
)
end
select_html("month", month_options, options[:prefix], options[:include_blank], options[:discard_type])
end
-
+
# Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius
- # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. The <tt>date</tt> can also be substituted
+ # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. The <tt>date</tt> can also be substituted
# for a year given as a number. Example:
#
# select_year(Date.today, :start_year => 1992, :end_year => 2007)
@@ -187,14 +187,14 @@ def select_year(date, options = {})
(options[:start_year] || default_start_year).upto(options[:end_year] || default_end_year) do |year|
year_options << ((date.kind_of?(Fixnum) ? date : date.year) == year ?
- "<option selected=\"selected\">#{year}</option>\n" :
+ "<option selected=\"selected\">#{year}</option>\n" :
"<option>#{year}</option>\n"
)
end
select_html("year", year_options, options[:prefix], options[:include_blank], options[:discard_type])
end
-
+
private
def select_html(type, options, prefix = nil, include_blank = false, discard_type = false)
select_html = %(<select name="#{prefix || DEFAULT_PREFIX})
@@ -206,7 +206,7 @@ def select_html(type, options, prefix = nil, include_blank = false, discard_type
return select_html
end
-
+
def leading_zero_on_single_digits(number)
number > 9 ? number : "0#{number}"
end
@@ -217,40 +217,40 @@ class InstanceTag #:nodoc:
def to_date_select_tag(options = {})
defaults = { :discard_type => true }
- options = defaults.merge(options)
- options_with_prefix = Proc.new { |position| options.update({ :prefix => "#{@object_name}[#{@method_name}(#{position}i)]" }) }
+ options = defaults.merge(options)
+ options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
date = options[:include_blank] ? (value || 0) : (value || Date.today)
- date_select = ""
+ date_select = ""
options[:order] = [:month, :year, :day] if options[:month_before_year] # For backwards compatibility
options[:order] ||= [:year, :month, :day]
position = {:year => 1, :month => 2, :day => 3}
-
+
discard = {}
discard[:year] = true if options[:discard_year]
discard[:month] = true if options[:discard_month]
discard[:day] = true if options[:discard_day] or options[:discard_month]
-
+
options[:order].each do |param|
date_select << self.send("select_#{param}", date, options_with_prefix.call(position[param])) unless discard[param]
end
return date_select
end
-
+
def to_datetime_select_tag(options = {})
defaults = { :discard_type => true }
- options = defaults.merge(options)
- options_with_prefix = Proc.new { |position| options.update({ :prefix => "#{@object_name}[#{@method_name}(#{position}i)]" }) }
+ options = defaults.merge(options)
+ options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
datetime = options[:include_blank] ? (value || 0) : (value || Time.now)
datetime_select = select_year(datetime, options_with_prefix.call(1))
datetime_select << select_month(datetime, options_with_prefix.call(2)) unless options[:discard_month]
datetime_select << select_day(datetime, options_with_prefix.call(3)) unless options[:discard_day] || options[:discard_month]
datetime_select << " &mdash; " + select_hour(datetime, options_with_prefix.call(4)) unless options[:discard_hour]
datetime_select << " : " + select_minute(datetime, options_with_prefix.call(5)) unless options[:discard_minute] || options[:discard_hour]
-
+
return datetime_select
end
end
Oops, something went wrong.

0 comments on commit dfac1ce

Please sign in to comment.