Ticket #8036: date_helper_doc.diff
| File date_helper_doc.diff, 22.0 kB (added by jeremymcanally, 1 year ago) |
|---|
-
actionpack/lib/action_view/helpers/date_helper.rb
old new 37 37 # 40-59 secs # => less than a minute 38 38 # 60-89 secs # => 1 minute 39 39 # 40 # Examples: 41 # 40 # ==== Examples 42 41 # from_time = Time.now 43 # distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour 44 # distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute 45 # distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds 42 # distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour 43 # distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour 44 # distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute 45 # distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds 46 # distance_of_time_in_words(from_time, 3.years.from_now) # => over 3 years 47 # distance_of_time_in_words(from_time, from_time + 60.hours) # => about 3 days 48 # distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute 49 # distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute 50 # distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute 51 # distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 years 52 # distance_of_time_in_words(from_time, from_time + 4.years + 15.days + 30.minutes + 5.seconds) # => over 4 years 46 53 # 54 # to_time = Time.now + 6.years + 19.days 55 # distance_of_time_in_words(from_time, to_time, true) # => over 6 years 56 # distance_of_time_in_words(to_time, from_time, true) # => over 6 years 57 # distance_of_time_in_words(Time.now, Time.now) # => less than a minute 58 # 47 59 # Note: Rails calculates one year as 365.25 days. 48 60 def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) 49 61 from_time = from_time.to_time if from_time.respond_to?(:to_time) … … 76 88 end 77 89 78 90 # Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>. 91 # 92 # ==== Examples 93 # time_ago_in_words(3.minutes.from_now) # => 3 minutes 94 # time_ago_in_words(Time.now - 15.hours) # => 15 hours 95 # time_ago_in_words(Time.now) # => less than a minute 96 # 97 # from_time = Time.now - 3.days - 14.minutes - 25.seconds # => 3 days 79 98 def time_ago_in_words(from_time, include_seconds = false) 80 99 distance_of_time_in_words(from_time, Time.now, include_seconds) 81 100 end … … 96 115 # 97 116 # NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed. 98 117 # 99 # Examples: 118 # ==== Examples 119 # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute 120 # date_select("post", "written_on") 100 121 # 101 # date_select("post", "written_on") 122 # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute, 123 # # with the year in the year drop down box starting at 1995. 102 124 # date_select("post", "written_on", :start_year => 1995) 125 # 126 # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute, 127 # # with the year in the year drop down box starting at 1995, numbers used for months instead of words, 128 # # and without a day select box. 103 129 # date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true, 104 130 # :discard_day => true, :include_blank => true) 131 # 132 # # Generates a date select that when POSTed is stored in the post variable, in the written_on attribute 133 # # with the fields ordered as day, month, year rather than month, day, year. 105 134 # date_select("post", "written_on", :order => [:day, :month, :year]) 106 # date_select("user", "birthday", :order => [:month, :day])107 135 # 136 # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute 137 # # lacking a year field. 138 # date_select("user", "birthday", :order => [:month, :day]) 139 # 140 # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute 141 # # which is initially set to the date 3 days from the current date 108 142 # date_select("post", "written_on", :default => 3.days.from_now) 143 # 144 # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute 145 # # that will have a default day of 20. 109 146 # date_select("credit_card", "bill_due", :default => { :day => 20 }) 110 147 # 111 148 # The selects are prepared for multi-parameter assignment to an Active Record object. … … 116 153 # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified 117 154 # time-based attribute (identified by +method+) on an object assigned to the template (identified by +object+). 118 155 # You can include the seconds with <tt>:include_seconds</tt>. 119 # Examples: 156 # 157 # ==== Examples 158 # # Creates a time select tag that, when POSTed, will be stored in the post variable in the sunrise attribute 159 # time_select("post", "sunrise") 120 160 # 121 # time_select("post", "sunrise") 161 # # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute 162 # time_select("order", "submitted") 163 # 164 # # Creates a time select tag that, when POSTed, will be stored in the mail variable in the sent_at attribute 165 # time_select("mail", "sent_at") 166 # 167 # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the post variables in 168 # # the sunrise attribute. 122 169 # time_select("post", "start_time", :include_seconds => true) 123 170 # 171 # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the entry variables in 172 # # the submission_time attribute. 173 # time_select("entry", "submission_time", :include_seconds => true) 174 # 124 175 # The selects are prepared for multi-parameter assignment to an Active Record object. 125 176 def time_select(object_name, method, options = {}) 126 177 InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_time_select_tag(options) … … 129 180 # Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based 130 181 # attribute (identified by +method+) on an object assigned to the template (identified by +object+). Examples: 131 182 # 183 # ==== Examples 184 # # Generates a datetime select that, when POSTed, will be stored in the post variable in the written_on attribute 132 185 # datetime_select("post", "written_on") 186 # 187 # # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the 188 # # post variable in the written_on attribute. 133 189 # datetime_select("post", "written_on", :start_year => 1995) 134 190 # 191 # # Generates a datetime select with a default value of 3 days from the current time that, when POSTed, will be stored in the 192 # # trip variable in the departing attribute. 193 # datetime_select("trip", "departing", :default => 3.days.from_now) 194 # 195 # # Generates a datetime select that discards the type that, when POSTed, will be stored in the post variable as the written_on 196 # # attribute. 197 # datetime_select("post", "written_on", :discard_type => true) 198 # 135 199 # The selects are prepared for multi-parameter assignment to an Active Record object. 136 200 def datetime_select(object_name, method, options = {}) 137 201 InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_datetime_select_tag(options) … … 142 206 # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it 143 207 # will be appened onto the <tt>:order</tt> passed in. You can also add <tt>:date_separator</tt> and <tt>:time_separator</tt> 144 208 # keys to the +options+ to control visual display of the elements. 145 def select_datetime(datetime = Time.now, options = {}) 209 # 210 # ==== Examples 211 # my_date_time = Time.now + 4.days 212 # 213 # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) 214 # select_datetime(my_date_time) 215 # 216 # # Generates a datetime select that defaults to today (no specified datetime) 217 # select_datetime() 218 # 219 # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) 220 # # with the fields ordered year, month, day rather then month, day, year. 221 # select_datetime(my_date_time, :order => [:year, :month, :day]) 222 # 223 # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) 224 # # with a '/' between each date field. 225 # select_datetime(my_date_time, :date_separator => '/') 226 # 227 # # Generates a datetime select that discards the type of the field and defaults to the datetime in 228 # # my_date_time (four days after today) 229 # select_datetime(my_date_time, :discard_type => true) 230 # 231 # # Generates a datetime select that defaults to the datetime in my_date_time (four days after today) 232 # # prefixed with 'payday' rather than 'date' 233 # select_datetime(my_date_time, :prefix => 'payday') 234 # 235 def select_datetime(datetime = Time.now, options = {}) 146 236 separator = options[:datetime_separator] || '' 147 237 select_date(datetime, options) + separator + select_time(datetime, options) 148 238 end … … 151 241 # It's possible to explicitly set the order of the tags using the <tt>:order</tt> option with an array of 152 242 # symbols <tt>:year</tt>, <tt>:month</tt> and <tt>:day</tt> in the desired order. If you do not supply a Symbol, it 153 243 # will be appened onto the <tt>:order</tt> passed in. 244 # 245 # ==== Examples 246 # my_date = Time.today + 6.days 247 # 248 # # Generates a date select that defaults to the date in my_date (six days after today) 249 # select_date(my_date) 250 # 251 # # Generates a date select that defaults to today (no specified date) 252 # select_date() 253 # 254 # # Generates a date select that defaults to the date in my_date (six days after today) 255 # # with the fields ordered year, month, day rather then month, day, year. 256 # select_date(my_date, :order => [:year, :month, :day]) 257 # 258 # # Generates a date select that discards the type of the field and defaults to the date in 259 # # my_date (six days after today) 260 # select_datetime(my_date_time, :discard_type => true) 261 # 262 # # Generates a date select that defaults to the datetime in my_date (six days after today) 263 # # prefixed with 'payday' rather than 'date' 264 # select_datetime(my_date_time, :prefix => 'payday') 265 # 154 266 def select_date(date = Date.today, options = {}) 155 267 options[:order] ||= [] 156 268 [:year, :month, :day].each { |o| options[:order].push(o) unless options[:order].include?(o) } … … 163 275 end 164 276 165 277 # Returns a set of html select-tags (one for hour and minute) 166 # You can set <tt>:add_separator</tt> key to format the output. 278 # You can set <tt>:time_separator</tt> key to format the output, and 279 # the <tt>:include_seconds</tt> option to include an input for seconds. 280 # 281 # ==== Examples 282 # my_time = Time.now + 5.days + 7.hours + 3.minutes + 14.seconds 283 # 284 # # Generates a time select that defaults to the time in my_time 285 # select_time(my_time) 286 # 287 # # Generates a time select that defaults to the current time (no specified time) 288 # select_time() 289 # 290 # # Generates a time select that defaults to the time in my_time, 291 # # which has fields separated by ':' 292 # select_time(my_time, :time_separator => ':') 293 # 294 # # Generates a time select that defaults to the time in my_time, 295 # # that also includes an input for seconds 296 # select_time(my_time, :include_seconds => true) 297 # 298 # # Generates a time select that defaults to the time in my_time, that has fields 299 # # separated by ':' and includes an input for seconds 300 # select_time(my_time, :time_separator => ':', :include_seconds => true) 301 # 167 302 def select_time(datetime = Time.now, options = {}) 168 303 separator = options[:time_separator] || '' 169 304 select_hour(datetime, options) + separator + select_minute(datetime, options) + (options[:include_seconds] ? separator + select_second(datetime, options) : '') … … 172 307 # Returns a select tag with options for each of the seconds 0 through 59 with the current second selected. 173 308 # The <tt>second</tt> can also be substituted for a second number. 174 309 # Override the field name using the <tt>:field_name</tt> option, 'second' by default. 310 # 311 # ==== Examples 312 # my_time = Time.now + 16.minutes 313 # 314 # # Generates a select field for seconds that defaults to the seconds for the time in my_time 315 # select_second(my_time) 316 # 317 # # Generates a select field for seconds that defaults to the number given 318 # select_second(33) 319 # 320 # # Generates a select field for seconds that defaults to the seconds for the time in my_time 321 # # that is named 'interval' rather than 'second' 322 # select_second(my_time, :field_name => 'interval') 323 # 175 324 def select_second(datetime, options = {}) 176 325 val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.sec) : '' 177 326 if options[:use_hidden] … … 192 341 # Also can return a select tag with options by <tt>minute_step</tt> from 0 through 59 with the 00 minute selected 193 342 # The <tt>minute</tt> can also be substituted for a minute number. 194 343 # Override the field name using the <tt>:field_name</tt> option, 'minute' by default. 344 # 345 # ==== Examples 346 # my_time = Time.now + 6.hours 347 # 348 # # Generates a select field for minutes that defaults to the minutes for the time in my_time 349 # select_minute(my_time) 350 # 351 # # Generates a select field for minutes that defaults to the number given 352 # select_minute(14) 353 # 354 # # Generates a select field for minutes that defaults to the minutes for the time in my_time 355 # # that is named 'stride' rather than 'second' 356 # select_minute(my_time, :field_name => 'stride') 357 # 195 358 def select_minute(datetime, options = {}) 196 359 val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.min) : '' 197 360 if options[:use_hidden] … … 211 374 # Returns a select tag with options for each of the hours 0 through 23 with the current hour selected. 212 375 # The <tt>hour</tt> can also be substituted for a hour number. 213 376 # Override the field name using the <tt>:field_name</tt> option, 'hour' by default. 377 # 378 # ==== Examples 379 # my_time = Time.now + 6.hours 380 # 381 # # Generates a select field for minutes that defaults to the minutes for the time in my_time 382 # select_minute(my_time) 383 # 384 # # Generates a select field for minutes that defaults to the number given 385 # select_minute(14) 386 # 387 # # Generates a select field for minutes that defaults to the minutes for the time in my_time 388 # # that is named 'stride' rather than 'second' 389 # select_minute(my_time, :field_name => 'stride') 390 # 214 391 def select_hour(datetime, options = {}) 215 392 val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : '' 216 393 if options[:use_hidden] … … 230 407 # Returns a select tag with options for each of the days 1 through 31 with the current day selected. 231 408 # The <tt>date</tt> can also be substituted for a hour number. 232 409 # Override the field name using the <tt>:field_name</tt> option, 'day' by default. 410 # 411 # ==== Examples 412 # my_date = Time.today + 2.days 413 # 414 # # Generates a select field for days that defaults to the day for the date in my_date 415 # select_day(my_time) 416 # 417 # # Generates a select field for days that defaults to the number given 418 # select_day(5) 419 # 420 # # Generates a select field for days that defaults to the day for the date in my_date 421 # # that is named 'due' rather than 'day' 422 # select_day(my_time, :field_name => 'due') 423 # 233 424 def select_day(date, options = {}) 234 425 val = date ? (date.kind_of?(Fixnum) ? date : date.day) : '' 235 426 if options[:use_hidden] … … 252 443 # set the <tt>:use_month_numbers</tt> key in +options+ to true for this to happen. If you want both numbers and names, 253 444 # set the <tt>:add_month_numbers</tt> key in +options+ to true. If you would prefer to show month names as abbreviations, 254 445 # set the <tt>:use_short_month</tt> key in +options+ to true. If you want to use your own month names, set the 255 # <tt>:use_month_names</tt> key in +options+ to an array of 12 month names. 446 # <tt>:use_month_names</tt> key in +options+ to an array of 12 month names. Override the field name using the 447 # <tt>:field_name</tt> option, 'month' by default. 256 448 # 257 # Examples: 449 # ==== Examples 450 # # Generates a select field for months that defaults to the current month that 451 # # will use keys like "January", "March". 452 # select_month(Date.today) 258 453 # 259 # select_month(Date.today) # Will use keys like "January", "March" 260 # select_month(Date.today, :use_month_numbers => true) # Will use keys like "1", "3" 261 # select_month(Date.today, :add_month_numbers => true) # Will use keys like "1 - January", "3 - March" 262 # select_month(Date.today, :use_short_month => true) # Will use keys like "Jan", "Mar" 263 # select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...)) # Will use keys like "Januar", "Marts" 454 # # Generates a select field for months that defaults to the current month that 455 # # is named "start" rather than "month" 456 # select_month(Date.today, :field_name => 'start') 264 457 # 265 # Override the field name using the <tt>:field_name</tt> option, 'month' by default. 458 # # Generates a select field for months that defaults to the current month that 459 # # will use keys like "1", "3". 460 # select_month(Date.today, :use_month_numbers => true) 461 # 462 # # Generates a select field for months that defaults to the current month that 463 # # will use keys like "1 - January", "3 - March". 464 # select_month(Date.today, :add_month_numbers => true) 465 # 466 # # Generates a select field for months that defaults to the current month that 467 # # will use keys like "Jan", "Mar". 468 # select_month(Date.today, :use_short_month => true) 469 # 470 # # Generates a select field for months that defaults to the current month that 471 # # will use keys like "Januar", "Marts." 472 # select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...)) 473 # 266 474 def select_month(date, options = {}) 267 475 val = date ? (date.kind_of?(Fixnum) ? date : date.month) : '' 268 476 if options[:use_hidden] … … 292 500 # 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 293 501 # can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the +options+. Both ascending and descending year 294 502 # lists are supported by making <tt>:start_year</tt> less than or greater than <tt>:end_year</tt>. The <tt>date</tt> can also be 295 # substituted for a year given as a number. Example:503 # substituted for a year given as a number. Override the field name using the <tt>:field_name</tt> option, 'year' by default. 296 504 # 297 # select_year(Date.today, :start_year => 1992, :end_year => 2007) # ascending year values 298 # select_year(Date.today, :start_year => 2005, :end_year => 1900) # descending year values 505 # ==== Examples 506 # # Generates a select field for years that defaults to the current year that 507 # # has ascending year values 508 # select_year(Date.today, :start_year => 1992, :end_year => 2007) 509 # 510 # # Generates a select field for years that defaults to the current year that 511 # # is named 'birth' rather than 'year' 512 # select_year(Date.today, :field_name => 'birth') 513 # 514 # # Generates a select field for years that defaults to the current year that 515 # # has descending year values 516 # select_year(Date.today, :start_year => 2005, :end_year => 1900) 517 # 518 # # Generates a select field for years that defaults to the year 2006 that 519 # # has ascending year values 299 520 # select_year(2006, :start_year => 2000, :end_year => 2010) 300 521 # 301 # Override the field name using the <tt>:field_name</tt> option, 'year' by default.302 522 def select_year(date, options = {}) 303 523 val = date ? (date.kind_of?(Fixnum) ? date : date.year) : '' 304 524 if options[:use_hidden]