Skip to content

Instantly share code, notes, and snippets.

@kule
Created June 11, 2009 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kule/127776 to your computer and use it in GitHub Desktop.
Save kule/127776 to your computer and use it in GitHub Desktop.
webrat: scope.rb#select_date
# Verifies that date elements (year, month, day) exist on the current page
# with the specified values. You can optionally restrict the search to a specific
# date's elements by assigning <tt>options[:from]</tt> the value of the date's
# label. Selects all the date elements with date provided. The date provided may
# be a string or a Date/Time object.
#
# Rail's convention is used for detecting the date elements. All elements
# are assumed to have a shared prefix. You may also specify the prefix
# by assigning <tt>options[:id_prefix]</tt>.
#
# Examples:
# select_date "January 23, 2004"
# select_date "April 26, 1982", :from => "Birthday"
# select_date Date.parse("December 25, 2000"), :from => "Event"
# select_date "April 26, 1982", :id_prefix => 'birthday'
# select_date "4/26/1982", :use_month_numbers => true
# select_date "Apr 26, 1982", :use_short_month => true
# select_date "4/26/1982", :add_month_numbers => true
# select_date 1.year.from_now, :discard_day => true
# select_date Time.now, :discard_month => true
# select_date 1.month.ago, :discard_year => true
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
month = if options[:use_month_numbers] then
date.month
elsif options[:use_short_month] then
date.strftime('%b')
else
date.strftime('%B')
end
month = "#{date.month} - #{month}" if options[:add_month_number] && !options[:use_month_numbers]
select date.year, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" unless options[:discard_year]
select month, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}" unless options[:discard_month]
select date.day, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}" unless options[:discard_day] || options[:discard_month]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment