#71 awaiting-merge
Peter Sumskas

selects_date needs to handle options similar to ActionView::Helpers::DateHelper#date_select

Reported by Peter Sumskas | November 23rd, 2008 @ 07:00 AM

I'm doing credit-card expiry date testing so I have only two fields displayed: year and month and the month is numeric, not text. That means my date_select call in my view looks like:


 f.date_select :expiry_date, :use_month_numbers => true, :start_year => Time.now.year, :discard_day => true

The current selects_date doesn't handle this. I made some changes in scope.rb and have a new version of the method:


    def selects_date(date_to_select, options ={})
      date = date_to_select.is_a?(Date) || date_to_select.is_a?(Time) ?
                date_to_select : Date.parse(date_to_select)

      id_prefix = locate_id_prefix(options) do
        year_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/)
        raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
        $1
      end

      month = date.strftime('%B')
      month = date.strftime('%b') if options[:use_short_month]
      month = "#{options[:use_month_names][date.month - 1]}" if options[:use_month_names]
      month = "#{date.month}" if options[:use_month_numbers]
      month = "#{date.month} - #{month}" if options[:add_month_number]

      selects date.year, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" unless options[:discard_year]
      selects month, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}" unless options[:discard_month]
      selects date.day, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}" unless options[:discard_day]
    end

I'm pretty new to this github stuff - so if the above is something you think might be useful to include in the official code please let me know if there is anything I can do to assist.

BTW - I just noticed the above will probably have problems still if :discard_year is used (when doing the find_field_with_id bit used to get the id_prefix).

Comments and changes to this ticket

  • gaffo

    gaffo January 6th, 2009 @ 07:06 PM

    • Tag set to featurerequest, patch

    It would be prefereable if you put this on a github branch forked from webrat master called lh_71_select date.

    Also some tests to show the issue will make it much more likely for someone else to come along and finish this for you.

  • Mike Breen

    Mike Breen April 12th, 2009 @ 11:24 AM

    • Tag changed from featurerequest, patch to featurerequest, patch, pull, select_date

    I've created a fork that allows you to use month number select_date 4/12/2009 :use_month_numbers => true and short months select_date Apr 12, 2009 :use_short_month => true.

    http://github.com/hardbap/webrat...

  • gaffo

    gaffo May 7th, 2009 @ 06:51 PM

    • Tag changed from featurerequest, patch, pull, select_date to featurerequest, patch, pull, select_date, verify
    • State changed from “new” to “awaiting-merge”

    Patch looks good but have not tried it yet.

  • Luke Pearce

    Luke Pearce June 11th, 2009 @ 05:06 AM

    • Assigned user changed from “Bryan Helmkamp” to “gaffo”

    I tried this but it didn't work for me as I was using add_month_number. I've updated the Mike's updates to select_date to include this as well as discard_day/month/year as per the other DateHelper options.

    http://gist.github.com/127776

    Near the end of a project at the moment so a bit busy :0) Can look into adding some tests & forking properly later if you would like.

    Cheers
    Luke

  • René

    René November 6th, 2009 @ 09:14 AM

    Hi, I am a little new to patching and the like, but I have a snippet here that I think resolves this issue... Should I post this to github somewhere...? And do I need to write tests for this patch?

    
    module Webrat
      class Scope
        def select_date(date_to_select, options ={})
          date = date_to_select.is_a?(Date) || date_to_select.is_a?(Time) ?
                    date_to_select : Date.parse(date_to_select)
    
          id_prefix = locate_id_prefix(options) do
            year_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate
            raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
            $1
          end
    
          select date.year, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" if select_field_present?(:month)
          select I18n.t('date.month_names')[date.mon], :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}" if select_field_present?(:month)
          select date.day, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}" if select_field_present?(:day)
        end
    
        private
          def select_field_present?(unit)
            FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[unit]}$/).locate.respond_to?(:options)
          end
      end
    end
    
  • René

    René November 6th, 2009 @ 09:31 AM

    big whoops, the year field should, of course be checked as such...

     select date.year, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" if select_field_present?(:year)
    
  • Peter Sumskas

    Peter Sumskas June 20th, 2010 @ 08:07 PM

    I've updated my original solution to be far more complete - with tests and all. Handles almost all options that date_select can take (at least the ones that are meaningful in the webrat context).

    See: http://github.com/brushbox/webrat

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

Pages