Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Date select helpers should have a :prompt option

#561

It should be possible to include a custom :prompt with the date select helpers in the same way you can include one with collection_select.

Including a prompt as the first select option is common practice when using date selects. Without this feature it's tricky to use the date select helpers if you need a custom prompt.

I've included a patch with tests and documentation. With this patch you can pass custom prompts as a hash, or pass true for a generic prompt (Day, Month, Year, Hour, Minute, Seconds).

Example:

date_select("post", "written_on", :prompt=>true)
date_select("post", "written_on", :prompt=>{:day=>'Select day', :month=>'Select month', :year=>'Select year'})

http://github.com/samoli/rails/c...

Reported by Sam Oliver · July 7th, 2008 @ 01:13 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Joff Williams
    Joff Williams

    +1 from me. date select is a great helper but lacks this prompt option.

    July 7th, 2008 @ 02:09 PM

  2. Clemens Kofler
  3. josh
    josh
    • State changed from new to stale

    Interesting, I will have to go in after 2.2 though. Lets relook at it then.

    October 28th, 2008 @ 04:22 PM

  4. grant
    grant

    Thanks! I'm glad I searched before writing that myself. Here's a version of the patch that applies cleanly to 2.1.2.

    November 3rd, 2008 @ 11:06 PM

  5. Jatinder Singh
    Jatinder Singh

    +1 We should probably have a similar approach to assign different CSS class name to each select generated. or is that something possible already?

    November 13th, 2008 @ 10:01 PM

  6. Martijn Vos
    Martijn Vos

    +1

    Why exactly is this stale, and not already in Rails?

    Also +1 on doing something similar for class name, although I think that should be a separate ticket.

    December 16th, 2008 @ 11:10 AM

  7. josh
    josh
    • State changed from stale to open

    December 16th, 2008 @ 04:15 PM

  8. Pratik
    Pratik
    • Assigned user set to Pratik

    I like the feature/patch. I couldn't apply it cleanly on the lastest edge, could you please upload a new patch and I'll commit it.

    Thanks.

    December 21st, 2008 @ 04:56 PM

  9. Sam Oliver
    Sam Oliver

    This new patch applies cleanly on to the latest edge

    December 21st, 2008 @ 07:51 PM

  10. Repository
    Repository
    • State changed from open to resolved

    (from [389534c38c3baaa63ce5cc2ba3bd169415419167]) Added prompt options to date helpers [#561 Date select helpers should have a :prompt option state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    December 22nd, 2008 @ 12:14 AM

  11. Sam Oliver
    Sam Oliver

    This new patch uses I18n for the text in the default prompts and can be cleanly applied to edge.

    December 22nd, 2008 @ 11:49 AM

  12. Pratik
    Pratik

    We need tests for I18 as well.

    December 22nd, 2008 @ 12:10 PM

  13. Sven Fuchs
    Sven Fuchs

    the i18n patch looks good to me :)

    December 22nd, 2008 @ 12:46 PM

  14. Sven Fuchs
    Sven Fuchs

    ... probably should test that the translations can be looked up though

    December 22nd, 2008 @ 12:50 PM

  15. Sam Oliver
  16. Pratik
    Pratik
    • State changed from resolved to open

    December 22nd, 2008 @ 03:03 PM

  17. Pratik
    Pratik
    • State changed from open to resolved

    December 22nd, 2008 @ 04:38 PM

  18. Martijn Vos
    Martijn Vos

    add_prompts_to_date_helpers_2.3.0.diff doesn't entirely fix this issue when applied to 2.2.2. It still uses today as default value instead of nil.

    In date_helper.rb, in class InstanceTag

      private
        def default_datetime(options)
          return if options[:include_blank]
    
    

    should be changed to

      private
        def default_datetime(options)
          return if options[:include_blank] || options[:prompt]
    
    

    I haven't tested against the latest edge yet, but I'd be surprised if it works properly there.

    December 24th, 2008 @ 12:37 PM

  19. Sam Oliver
    Sam Oliver

    This new patch applies the suggested change from Martijn and updates the tests to cover this issue.

    January 2nd, 2009 @ 03:40 PM

  20. Martijn Vos
    Martijn Vos

    Another minor but ugly issue that remains is that when I use the "year" select box, it starts at dates in the distant past. I want those to be available, but they're not the obvious choices.

    I'm not sure what the best fix would be here, though. Perhaps reversing the order of the years? Or putting the prompt (with nil value) at the bottom instead of the top? How much control does a developer currently have over such things?

    January 9th, 2009 @ 08:54 AM

  21. Sam Oliver
    Sam Oliver

    This is a good point but I think that it might be taking it a bit too far. You can always create your own helper to get absolute control. The :prompt option is fairly fundamental and is used in other select helpers so it's a natural inclusion. Maybe you could open a new ticket for this suggestion.

    January 9th, 2009 @ 11:20 AM

  22. Sam Oliver
    Sam Oliver

    Could this ticket be re-opened as it's not quite working in edge (see my last patch)?

    January 9th, 2009 @ 11:26 AM

  23. Pratik
    Pratik
    • State changed from resolved to open

    January 31st, 2009 @ 01:46 PM

  24. jonathan
  25. Joe F.
    Joe F.

    +1 - we are currently running on a patched version of Rails for this fix. We are required to use custom prompts in our date selects. Is there any way we could this fix in 2.3?

    Thanks!

    February 12th, 2009 @ 03:26 PM

  26. Pratik
  27. Sam Oliver
    Sam Oliver

    I tested my last patch again on edge today - it still applies cleanly and the tests pass.

    http://rails.lighthouseapp.com/a...

    It's a small change that will hopefully close this ticket

    February 12th, 2009 @ 09:08 PM

  28. Repository
    Repository
    • State changed from open to resolved

    (from [f04346d8b999476113d5e5a30661e07899e3ff80]) Stops date select helpers from defaulting the selected date to today if :prompt option has been used

    Signed-off-by: Michael Koziarski michael@koziarski.com [#561 Date select helpers should have a :prompt option state:resolved] http://github.com/rails/rails/co...

    February 13th, 2009 @ 09:32 AM