ihoka (owner)

Revisions

  • 8dcdf9 ihoka Wed Nov 11 00:05:13 -0800 2009
gist: 231774 Download_button fork
public
Public Clone URL: git://gist.github.com/231774.git
Embed All Files: show embed
formtastic_helpers.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)