formtastic/formtastic

Custom inputs no longer automatically found in Spork environment

rmm5t opened this issue · 6 comments

After upgrading from Formtastic 2.0.x to 2.2.0, my custom inputs were no longer automatically found during my controller tests ("functional" tests). Upon further review, I think this is because of the new behavior surrounding the cache_classes config while searching for input classes and because I use Spork in my test environment.

My current workaround is to require all the classes in app/inputs inside Spork's prefork block:

Spork.prefork do
  # ...

  # Load all files under app/inputs. Neccesary to get Formtastic's custom inputs
  # to work in a Spork environment while config.cache_classes is set to true
  Dir["app/inputs/*_input.rb"].each { |f| require File.basename(f) }
end

I'm opening this up here in hopes of feedback, a more elegant fix, and/or helping someone else with a similar problem. If this warrants a README update, I'd be happy to write the addition. If not, feel free to close.

Thanks rmm5t, we've run into the same problem with our project and your patch works great here too.

+1, had the same problem and the workaround works ;-)

Thanks rmm5t, +1 from me too. Definitely worth adding a note to the README.

The wiki on the spork github project also has a bunch of spec_helper example snippets showing how to avoid problems with common gems (devise, factory_girl etc.). Formtastic is clearly worth adding to that list too.

This is also an issue in spring.

Spring is setting the loading mechanism to :load which essentially overrides the functionality of Rails.application.config.cache_classes (but not really).

This patch solves the issue for Spring (and it appears to have the same general effect as the current code): https://gist.github.com/rjackson/5096208.

+1, we had the same problem with our project. We're using the surveyor gem which relies on formtastic.

Thanks rmm5t, your workaround worked for us too, after modifying it to work on the surveyor gem's app/inputs folder:

  surveyor_path = Gem.loaded_specs['surveyor'].full_gem_path
  Dir["#{surveyor_path}/app/inputs/*_input.rb"].each { |f| require File.basename(f) }

Closing, as the work-around is documented and there's been a lack of activity here.