Hi all,
In a cucumber scenario I am using pickle like so:
Then the following users should exist:
| name | expired |
| Owen | false |
| Aiden | true |
using the built pickle step definition for finding models using a cucumber table.
Normally, this works well except when I’m trying to find records with a boolean attribute. Instead of finding expired: true, the cucumber table returns everything as a string so that pickle receives it as expired: “true”. There’s no problem with creating records using tables because ActiveRecord performs the typecasting when a new record is created. This is not the case when you find a record. For example:
u = User.new(:expired => “true”)
u.expired #=> true
User.find(:all, :conditions => { :expired => “true”}) #=> empty array (none found)
This is also a problem when using floats.
I considered possibly using cucumber transforms (but don’t want potentially want all trues and falses converted to booleans since I often deal with legacy data where columns are strings of true and false) and I considered modifying the pickle step definition to modify the boolean strings to actual booleans but I was hoping for a better solution.
I looked at modifying pickle to automatically convert these strings to their appropriate values according to the attribute’s type and this is what I got:
def convert_models_to_attributes(ar_class, attrs)
conditions = {}
attrs.each do |key, val|
if ((defined?(ActiveRecord::Base) && val.is_a?(ActiveRecord::Base)) ||
(defined?(DataMapper::Model) && val.is_a?(DataMapper::Model))) &&
Pickle::Adapter.column_names(ar_class).include?("#{key}_id")
conditions["#{key}_id"] =
val.id elsif defined?(ActiveRecord::Base) &&
Pickle::Adapter.column_hash(ar_class)[key.to_s].type == :boolean
conditions[key] = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(val) ? true : false
else
conditions[key] = val
end
end
conditions
end
I’m not sure whether this is the right approach and has hoping for some feedback and advice.
Thanks,
Michael.