Ruby on Rails testing with Select2

I posted a while back about using the select2-rails gem together with the select2-capistrano gem. I recently upgraded an app and found that my tests that were using select2 were all failing. I did some googling and it seems that the select2-capistrano gem is no longer active.

Well, I went about it another way then.

First off, I do not add the class to each drop-down select but rather I do it globally using a collection_select_input.rb file in app/inputs/ like so:

[code language="ruby"]
class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
def input_html_classes
super.push('chosen-select')
end
end
[/code]

So when I started to have issues with capybara not being able to use the select2 drop-down my fix was to only use the global push if not in test environment:

[code language="ruby"]
class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
unless Rails.env.test?
def input_html_classes
super.push('chosen-select')
end
end
end
[/code]

So now I have good old normal drop downs in test environment and the ever awesome select2 drop downs in production.

comments powered by Disqus