Skip to content

Instantly share code, notes, and snippets.

@ihoka
Created November 11, 2009 08:05
Show Gist options
  • Save ihoka/231774 to your computer and use it in GitHub Desktop.
Save ihoka/231774 to your computer and use it in GitHub Desktop.
module FormtasticHelpers
# Verifies that date elements (year, month, day) exist on the current page,
# for a form built with Formtastic.
def select_formtastic_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)
label = options[:from]
doc = Nokogiri::HTML(response.body)
# Formtastic renders a span with class label for date fields.
# /li/fieldset/legend/span
results = doc.xpath(%Q{//span[contains(text(), "#{label}")][@class="label"]})
result = results.first
# Grab the ID of the parent LI.
date_select_inputs_id = result.parent.parent.parent["id"]
id_prefix = date_select_inputs_id.gsub(/_input$/, '')
select date.year, :from => "#{id_prefix}_#{Webrat::Scope::DATE_TIME_SUFFIXES[:year]}"
select date.strftime('%B'), :from => "#{id_prefix}_#{Webrat::Scope::DATE_TIME_SUFFIXES[:month]}"
select date.day, :from => "#{id_prefix}_#{Webrat::Scope::DATE_TIME_SUFFIXES[:day]}"
end
end
World(FormtasticHelpers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment