Skip to content

Instantly share code, notes, and snippets.

@linojon
Created August 14, 2009 15:12
Show Gist options
  • Save linojon/167886 to your computer and use it in GitHub Desktop.
Save linojon/167886 to your computer and use it in GitHub Desktop.
## steps file
def fill_in_upload( resource, field, value )
# adds a resource in the name
# when you specify "#{fieldname} upload: files/foo.ext" we convert value into an upload (assumes no field named foo_upload)
# when field name is dotted, assumes its an association using *_attributes (eg user.email => [user_attributes][email])
# otherwise works like fills_in
# when fills_in fails, tries it as a
# - radio select
# - dropdown select (TODO)
if field.include?('_upload')
field = field.gsub( '_upload', '')
# kludge file type for time being
ftype = case value.split('.').last
when 'jpg': 'image/jpg'
when 'pdf': 'application/pdf'
else
nil
end
attach_file "#{resource}[#{field}]", FIXTURE_FILE_PATH+value, ftype
elsif field.include?('.')
names = field.split('.')
param = resource
last = names.delete(names.last)
names.each {|n| param << "[#{n}_attributes]" }
param << "[#{last}]"
fill_in param, :with => value
elsif !resource.blank?
begin
fill_in "#{resource}[#{field}]", :with => value
rescue
begin
# radio?
choose "#{resource.normalize}_#{field.normalize}_#{value.normalize}"
rescue
# dropdown?
select value, :from => "#{resource.normalize}[#{field.normalize}]"
end
end
else
fill_in field, :with => value
end
end
##
When /^(?:I )?fill in the (.*) "(.*)" field with "(.*)"$/ do |resource, field, value|
fill_in_upload resource.normalize, field.normalize, value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment