Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an ordinal to cardinal function in ruby or rails?

I'm trying to find a better way in expressing my cucumbers so I am looking for an ordinal to cardinal function that converts this:

When I fill up the first passenger field
Then I should see the passenger list update with the first passenger details
When I follow "Add Another Passenger"
Then I should see a second passenger field
When I fill up the second passenger field
Then I should see the passenger list update with the second passenger details

into something more dynamic(instead of creating separate steps for each line)

here's a sample of my web steps

When /^I fill up the first passenger field$/ do
  fill_in("booking_passengers_attributes_0_first_name", :with => "Blah")
  fill_in("booking_passengers_attributes_0_last_name", :with => "blah")
  select("5' to 6'", :from => "booking_passengers_attributes_0_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_0_weight")
end

When /^I fill up the second passenger field$/ do
  fill_in("booking_passengers_attributes_1_first_name", :with => "Wee")
  fill_in("booking_passengers_attributes_1_last_name", :with => "Sir")
  select("5' to 6'", :from => "booking_passengers_attributes_1_height")
  select("150 to 200lbs", :from => "booking_passengers_attributes_1_weight")
end

See that 0 and 1? I wish to convert "first" to a cardinal number so i can just substitute. You can also just suggest a better way to declare the cukes :)

UPDATED ANSWER I am in the middle of refactoring but basically I used 1st instead of first and used to_i on that.

When /^I fill up the "([^"]*)" passenger field$/ do |id|
  input_id = id.to_i - 1
  fill_in("booking_passengers_attributes_#{input_id}_first_name", :with => id)
  fill_in("booking_passengers_attributes_#{input_id}_last_name", :with => "Passenger")
  select("5' to 6'", :from => "booking_passengers_attributes_#{input_id}_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_#{input_id}_weight")  
end
like image 261
corroded Avatar asked May 06 '11 05:05

corroded


1 Answers

i dont really completely understand,exactly what you want to do, but you can do something like this with active support:

 1.ordinalize    # => "1st"
  2.ordinalize    # => "2nd"
  1002.ordinalize # => "1002nd"

and there is a action view helper number_in_words to get "first" , "second" etc

i don't know much about cukes sorry,

like image 115
loosecannon Avatar answered Sep 26 '22 02:09

loosecannon